hydrus/include/ClientLocalServerResources.py

303 lines
10 KiB
Python
Raw Normal View History

2015-08-05 18:42:35 +00:00
import ClientConstants as CC
import ClientFiles
import HydrusConstants as HC
import HydrusData
2017-03-02 02:14:56 +00:00
import HydrusExceptions
2017-05-10 21:33:58 +00:00
import HydrusGlobals as HG
2015-08-05 18:42:35 +00:00
import HydrusServerResources
import os
2015-12-02 22:32:18 +00:00
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' )
2015-08-05 18:42:35 +00:00
2017-03-02 02:14:56 +00:00
class HydrusResourceBooru( HydrusServerResources.HydrusResource ):
2015-08-05 18:42:35 +00:00
2017-06-21 21:15:59 +00:00
def _reportDataUsed( self, request, num_bytes ):
2015-08-05 18:42:35 +00:00
2017-06-21 21:15:59 +00:00
self._service.ReportDataUsed( num_bytes )
2015-08-05 18:42:35 +00:00
2017-06-21 21:15:59 +00:00
def _reportRequestUsed( self, request ):
2015-08-05 18:42:35 +00:00
2017-06-21 21:15:59 +00:00
self._service.ReportRequestUsed()
2015-08-05 18:42:35 +00:00
2017-03-02 02:14:56 +00:00
def _checkService( self, request ):
2015-08-05 18:42:35 +00:00
2017-03-02 02:14:56 +00:00
HydrusServerResources.HydrusResource._checkService( self, request )
2015-08-05 18:42:35 +00:00
2017-06-07 22:05:15 +00:00
if not self._service.BandwidthOK():
2017-03-02 02:14:56 +00:00
raise HydrusExceptions.BandwidthException( 'This service has run out of bandwidth. Please try again later.' )
2015-08-05 18:42:35 +00:00
2017-03-02 02:14:56 +00:00
class HydrusResourceBooruFile( HydrusResourceBooru ):
2015-08-05 18:42:35 +00:00
def _threadDoGETJob( self, request ):
share_key = request.hydrus_args[ 'share_key' ]
hash = request.hydrus_args[ 'hash' ]
2017-12-06 22:06:56 +00:00
HG.client_controller.local_booru_manager.CheckFileAuthorised( share_key, hash )
2015-08-05 18:42:35 +00:00
2018-03-07 22:48:29 +00:00
media_result = HG.client_controller.local_booru_manager.GetMediaResult( share_key, hash )
mime = media_result.GetMime()
2017-06-28 20:23:21 +00:00
client_files_manager = HG.client_controller.client_files_manager
2015-12-02 22:32:18 +00:00
2018-03-07 22:48:29 +00:00
path = client_files_manager.GetFilePath( hash, mime )
2015-08-05 18:42:35 +00:00
2018-03-07 22:48:29 +00:00
response_context = HydrusServerResources.ResponseContext( 200, mime = mime, path = path )
2015-08-05 18:42:35 +00:00
return response_context
2017-03-02 02:14:56 +00:00
class HydrusResourceBooruGallery( HydrusResourceBooru ):
2015-08-05 18:42:35 +00:00
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' ]
2017-12-06 22:06:56 +00:00
local_booru_manager = HG.client_controller.local_booru_manager
2015-08-05 18:42:35 +00:00
local_booru_manager.CheckShareAuthorised( share_key )
( name, text, timeout, media_results ) = local_booru_manager.GetGalleryInfo( share_key )
body = '''<html>
<head>'''
if name == '': body += '''
<title>hydrus network local booru share</title>'''
else: body += '''
<title>''' + name + '''</title>'''
body += '''
<link href="hydrus.ico" rel="shortcut icon" />
<link href="style.css" rel="stylesheet" type="text/css" />'''
( thumbnail_width, thumbnail_height ) = HC.options[ 'thumbnail_dimensions' ]
body += '''
<style>
.thumbnail_container { width: ''' + str( thumbnail_width ) + '''px; height: ''' + str( thumbnail_height ) + '''px; }
</style>'''
body += '''
</head>
<body>'''
body += '''
<div class="timeout">This share ''' + HydrusData.ConvertTimestampToPrettyExpires( timeout ) + '''.</div>'''
if name != '': body += '''
<h3>''' + name + '''</h3>'''
if text != '':
newline = '''</p>
<p>'''
body += '''
<p>''' + text.replace( os.linesep, newline ).replace( '\n', newline ) + '''</p>'''
body+= '''
<div class="media">'''
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 += '''
<span class="thumbnail">
<span class="thumbnail_container">
<a href="page?share_key=''' + share_key.encode( 'hex' ) + '''&hash=''' + hash.encode( 'hex' ) + '''">
<img src="thumbnail?share_key=''' + share_key.encode( 'hex' ) + '''&hash=''' + hash.encode( 'hex' ) + '''" />
</a>
</span>
</span>'''
body += '''
</div>
2015-10-21 21:53:10 +00:00
<div class="footer"><a href="https://hydrusnetwork.github.io/hydrus/">hydrus network</a></div>
2015-08-05 18:42:35 +00:00
</body>
</html>'''
response_context = HydrusServerResources.ResponseContext( 200, mime = HC.TEXT_HTML, body = body )
return response_context
2017-03-02 02:14:56 +00:00
class HydrusResourceBooruPage( HydrusResourceBooru ):
2015-08-05 18:42:35 +00:00
def _threadDoGETJob( self, request ):
share_key = request.hydrus_args[ 'share_key' ]
hash = request.hydrus_args[ 'hash' ]
2017-12-06 22:06:56 +00:00
local_booru_manager = HG.client_controller.local_booru_manager
2015-08-05 18:42:35 +00:00
local_booru_manager.CheckFileAuthorised( share_key, hash )
( name, text, timeout, media_result ) = local_booru_manager.GetPageInfo( share_key, hash )
body = '''<html>
<head>'''
if name == '': body += '''
<title>hydrus network local booru share</title>'''
else: body += '''
<title>''' + name + '''</title>'''
body += '''
<link href="hydrus.ico" rel="shortcut icon" />
<link href="style.css" rel="stylesheet" type="text/css" />'''
body += '''
</head>
<body>'''
body += '''
<div class="timeout">This share ''' + HydrusData.ConvertTimestampToPrettyExpires( timeout ) + '''.</div>'''
if name != '': body += '''
<h3>''' + name + '''</h3>'''
if text != '':
newline = '''</p>
<p>'''
body += '''
<p>''' + text.replace( os.linesep, newline ).replace( '\n', newline ) + '''</p>'''
body+= '''
<div class="media">'''
mime = media_result.GetMime()
if mime in HC.IMAGES:
( width, height ) = media_result.GetResolution()
body += '''
<img width="''' + str( width ) + '''" height="''' + str( height ) + '''" src="file?share_key=''' + share_key.encode( 'hex' ) + '''&hash=''' + hash.encode( 'hex' ) + '''" />'''
elif mime in HC.VIDEO:
( width, height ) = media_result.GetResolution()
body += '''
<video width="''' + str( width ) + '''" height="''' + str( height ) + '''" controls="" loop="" autoplay="" src="file?share_key=''' + share_key.encode( 'hex' ) + '''&hash=''' + hash.encode( 'hex' ) + '''" />
<p><a href="file?share_key=''' + share_key.encode( 'hex' ) + '''&hash=''' + hash.encode( 'hex' ) + '''">link to ''' + HC.mime_string_lookup[ mime ] + ''' file</a></p>'''
elif mime == HC.APPLICATION_FLASH:
( width, height ) = media_result.GetResolution()
body += '''
<embed width="''' + str( width ) + '''" height="''' + str( height ) + '''" src="file?share_key=''' + share_key.encode( 'hex' ) + '''&hash=''' + hash.encode( 'hex' ) + '''" />
<p><a href="file?share_key=''' + share_key.encode( 'hex' ) + '''&hash=''' + hash.encode( 'hex' ) + '''">link to ''' + HC.mime_string_lookup[ mime ] + ''' file</a></p>'''
else:
body += '''
<p><a href="file?share_key=''' + share_key.encode( 'hex' ) + '''&hash=''' + hash.encode( 'hex' ) + '''">link to ''' + HC.mime_string_lookup[ mime ] + ''' file</a></p>'''
body += '''
</div>
2015-10-21 21:53:10 +00:00
<div class="footer"><a href="https://hydrusnetwork.github.io/hydrus/">hydrus network</a></div>
2015-08-05 18:42:35 +00:00
</body>
</html>'''
response_context = HydrusServerResources.ResponseContext( 200, mime = HC.TEXT_HTML, body = body )
return response_context
2017-03-02 02:14:56 +00:00
class HydrusResourceBooruThumbnail( HydrusResourceBooru ):
2015-08-05 18:42:35 +00:00
def _threadDoGETJob( self, request ):
share_key = request.hydrus_args[ 'share_key' ]
hash = request.hydrus_args[ 'hash' ]
2017-12-06 22:06:56 +00:00
local_booru_manager = HG.client_controller.local_booru_manager
2015-08-05 18:42:35 +00:00
local_booru_manager.CheckFileAuthorised( share_key, hash )
media_result = local_booru_manager.GetMediaResult( share_key, hash )
mime = media_result.GetMime()
2017-03-02 02:14:56 +00:00
response_context_mime = HC.IMAGE_PNG
2016-06-08 20:27:22 +00:00
if mime in HC.MIMES_WITH_THUMBNAILS:
2017-06-28 20:23:21 +00:00
client_files_manager = HG.client_controller.client_files_manager
2016-06-08 20:27:22 +00:00
2016-07-27 21:53:34 +00:00
path = client_files_manager.GetFullSizeThumbnailPath( hash )
2016-06-08 20:27:22 +00:00
2017-03-02 02:14:56 +00:00
response_context_mime = HC.APPLICATION_UNKNOWN
2018-03-07 22:48:29 +00:00
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' )
2015-08-05 18:42:35 +00:00
2017-03-02 02:14:56 +00:00
response_context = HydrusServerResources.ResponseContext( 200, mime = response_context_mime, path = path )
2015-08-05 18:42:35 +00:00
return response_context
2017-03-02 02:14:56 +00:00
class HydrusResourceLocalFile( HydrusServerResources.HydrusResource ):
2015-08-05 18:42:35 +00:00
def _threadDoGETJob( self, request ):
hash = request.hydrus_args[ 'hash' ]
2017-06-28 20:23:21 +00:00
client_files_manager = HG.client_controller.client_files_manager
2015-12-02 22:32:18 +00:00
path = client_files_manager.GetFilePath( hash )
2015-08-05 18:42:35 +00:00
2017-03-02 02:14:56 +00:00
response_context = HydrusServerResources.ResponseContext( 200, mime = HC.APPLICATION_UNKNOWN, path = path )
2015-08-05 18:42:35 +00:00
return response_context
2017-03-02 02:14:56 +00:00
class HydrusResourceLocalThumbnail( HydrusServerResources.HydrusResource ):
2015-08-05 18:42:35 +00:00
def _threadDoGETJob( self, request ):
hash = request.hydrus_args[ 'hash' ]
2017-06-28 20:23:21 +00:00
client_files_manager = HG.client_controller.client_files_manager
2016-06-08 20:27:22 +00:00
2016-07-27 21:53:34 +00:00
path = client_files_manager.GetFullSizeThumbnailPath( hash )
2015-08-05 18:42:35 +00:00
2017-03-02 02:14:56 +00:00
response_context = HydrusServerResources.ResponseContext( 200, mime = HC.APPLICATION_UNKNOWN, path = path )
2015-08-05 18:42:35 +00:00
return response_context
2017-03-02 02:14:56 +00:00