import ClientConstants as CC import ClientFiles import HydrusConstants as HC import HydrusData import HydrusExceptions import HydrusGlobals as HG import HydrusServerResources import os from twisted.web.static import File as FileResource local_booru_css = FileResource( os.path.join( HC.STATIC_DIR, 'local_booru_style.css' ), defaultType = 'text/css' ) class HydrusResourceBooru( HydrusServerResources.HydrusResource ): def _reportDataUsed( self, request, num_bytes ): self._service.ReportDataUsed( num_bytes ) def _reportRequestUsed( self, request ): self._service.ReportRequestUsed() def _checkService( self, request ): HydrusServerResources.HydrusResource._checkService( self, request ) if not self._service.BandwidthOK(): raise HydrusExceptions.BandwidthException( 'This service has run out of bandwidth. Please try again later.' ) class HydrusResourceBooruFile( HydrusResourceBooru ): def _threadDoGETJob( self, request ): share_key = request.hydrus_args[ 'share_key' ] hash = request.hydrus_args[ 'hash' ] HG.client_controller.local_booru_manager.CheckFileAuthorised( share_key, hash ) media_result = HG.client_controller.local_booru_manager.GetMediaResult( share_key, hash ) mime = media_result.GetMime() client_files_manager = HG.client_controller.client_files_manager path = client_files_manager.GetFilePath( hash, mime ) response_context = HydrusServerResources.ResponseContext( 200, mime = mime, path = path ) return response_context class HydrusResourceBooruGallery( HydrusResourceBooru ): def _threadDoGETJob( self, request ): # in future, make this a standard frame with a search key that'll load xml or yaml AJAX stuff # with file info included, so the page can sort and whatever share_key = request.hydrus_args[ 'share_key' ] local_booru_manager = HG.client_controller.local_booru_manager local_booru_manager.CheckShareAuthorised( share_key ) ( name, text, timeout, media_results ) = local_booru_manager.GetGalleryInfo( share_key ) body = ''' ''' if name == '': body += ''' hydrus network local booru share''' else: body += ''' ''' + name + '''''' body += ''' ''' ( thumbnail_width, thumbnail_height ) = HC.options[ 'thumbnail_dimensions' ] body += ''' ''' body += ''' ''' body += '''
This share ''' + HydrusData.ConvertTimestampToPrettyExpires( timeout ) + '''.
''' if name != '': body += '''

''' + name + '''

''' if text != '': newline = '''

''' body += '''

''' + text.replace( os.linesep, newline ).replace( '\n', newline ) + '''

''' body+= '''
''' for media_result in media_results: hash = media_result.GetHash() mime = media_result.GetMime() # if mime in flash or pdf or whatever, get other thumbnail body += ''' ''' body += '''
''' response_context = HydrusServerResources.ResponseContext( 200, mime = HC.TEXT_HTML, body = body ) return response_context class HydrusResourceBooruPage( HydrusResourceBooru ): def _threadDoGETJob( self, request ): share_key = request.hydrus_args[ 'share_key' ] hash = request.hydrus_args[ 'hash' ] local_booru_manager = HG.client_controller.local_booru_manager local_booru_manager.CheckFileAuthorised( share_key, hash ) ( name, text, timeout, media_result ) = local_booru_manager.GetPageInfo( share_key, hash ) body = ''' ''' if name == '': body += ''' hydrus network local booru share''' else: body += ''' ''' + name + '''''' body += ''' ''' body += ''' ''' body += '''
This share ''' + HydrusData.ConvertTimestampToPrettyExpires( timeout ) + '''.
''' if name != '': body += '''

''' + name + '''

''' if text != '': newline = '''

''' body += '''

''' + text.replace( os.linesep, newline ).replace( '\n', newline ) + '''

''' body+= '''
''' mime = media_result.GetMime() if mime in HC.IMAGES: ( width, height ) = media_result.GetResolution() body += ''' ''' elif mime in HC.VIDEO: ( width, height ) = media_result.GetResolution() body += '''
''' response_context = HydrusServerResources.ResponseContext( 200, mime = HC.TEXT_HTML, body = body ) return response_context class HydrusResourceBooruThumbnail( HydrusResourceBooru ): def _threadDoGETJob( self, request ): share_key = request.hydrus_args[ 'share_key' ] hash = request.hydrus_args[ 'hash' ] local_booru_manager = HG.client_controller.local_booru_manager local_booru_manager.CheckFileAuthorised( share_key, hash ) media_result = local_booru_manager.GetMediaResult( share_key, hash ) mime = media_result.GetMime() response_context_mime = HC.IMAGE_PNG if mime in HC.MIMES_WITH_THUMBNAILS: client_files_manager = HG.client_controller.client_files_manager path = client_files_manager.GetFullSizeThumbnailPath( hash ) response_context_mime = HC.APPLICATION_UNKNOWN elif mime in HC.AUDIO: path = os.path.join( HC.STATIC_DIR, 'audio.png' ) elif mime == HC.APPLICATION_PDF: path = os.path.join( HC.STATIC_DIR, 'pdf.png' ) else: path = os.path.join( HC.STATIC_DIR, 'hydrus.png' ) response_context = HydrusServerResources.ResponseContext( 200, mime = response_context_mime, path = path ) return response_context class HydrusResourceLocalFile( HydrusServerResources.HydrusResource ): def _threadDoGETJob( self, request ): hash = request.hydrus_args[ 'hash' ] client_files_manager = HG.client_controller.client_files_manager path = client_files_manager.GetFilePath( hash ) response_context = HydrusServerResources.ResponseContext( 200, mime = HC.APPLICATION_UNKNOWN, path = path ) return response_context class HydrusResourceLocalThumbnail( HydrusServerResources.HydrusResource ): def _threadDoGETJob( self, request ): hash = request.hydrus_args[ 'hash' ] client_files_manager = HG.client_controller.client_files_manager path = client_files_manager.GetFullSizeThumbnailPath( hash ) response_context = HydrusServerResources.ResponseContext( 200, mime = HC.APPLICATION_UNKNOWN, path = path ) return response_context