hydrus/include/ServerFiles.py

111 lines
2.8 KiB
Python
Raw Normal View History

2015-03-18 21:46:29 +00:00
import HydrusConstants as HC
2016-06-08 20:27:22 +00:00
import HydrusData
2015-03-18 21:46:29 +00:00
import HydrusExceptions
import itertools
import os
def GetAllHashes( file_type ): return { os.path.split( path )[1].decode( 'hex' ) for path in IterateAllPaths( file_type ) }
2016-06-08 20:27:22 +00:00
def GetExpectedFilePath( hash ):
2015-03-18 21:46:29 +00:00
2016-06-08 20:27:22 +00:00
hash_encoded = hash.encode( 'hex' )
first_two_chars = hash_encoded[:2]
path = os.path.join( HC.SERVER_FILES_DIR, first_two_chars, hash_encoded )
return path
def GetExpectedThumbnailPath( hash ):
2015-03-18 21:46:29 +00:00
hash_encoded = hash.encode( 'hex' )
first_two_chars = hash_encoded[:2]
2016-06-08 20:27:22 +00:00
path = os.path.join( HC.SERVER_FILES_DIR, first_two_chars, hash_encoded + '.thumbnail' )
2015-03-18 21:46:29 +00:00
return path
2015-06-03 21:05:13 +00:00
def GetExpectedContentUpdatePackagePath( service_key, begin, subindex ):
2015-03-18 21:46:29 +00:00
2015-11-04 22:30:28 +00:00
path = os.path.join( GetExpectedUpdateDir( service_key ), str( int( begin ) ) + '_' + str( subindex ) )
2015-06-03 21:05:13 +00:00
return path
def GetExpectedServiceUpdatePackagePath( service_key, begin ):
2015-11-04 22:30:28 +00:00
path = os.path.join( GetExpectedUpdateDir( service_key ), str( int( begin ) ) + '_metadata' )
2015-03-18 21:46:29 +00:00
return path
2015-11-04 22:30:28 +00:00
def GetExpectedUpdateDir( service_key ):
return os.path.join( HC.SERVER_UPDATES_DIR, service_key.encode( 'hex' ) )
2016-06-08 20:27:22 +00:00
def GetContentUpdatePackagePath( service_key, begin, subindex ):
2015-03-18 21:46:29 +00:00
2016-06-08 20:27:22 +00:00
path = GetExpectedContentUpdatePackagePath( service_key, begin, subindex )
2015-03-18 21:46:29 +00:00
2015-11-04 22:30:28 +00:00
if not os.path.exists( path ):
2016-06-08 20:27:22 +00:00
raise HydrusExceptions.NotFoundException( 'Update not found!' )
2015-11-04 22:30:28 +00:00
2015-03-18 21:46:29 +00:00
return path
2016-06-08 20:27:22 +00:00
def GetServiceUpdatePackagePath( service_key, begin ):
2015-06-03 21:05:13 +00:00
2016-06-08 20:27:22 +00:00
path = GetExpectedServiceUpdatePackagePath( service_key, begin )
2015-06-03 21:05:13 +00:00
2015-11-04 22:30:28 +00:00
if not os.path.exists( path ):
raise HydrusExceptions.NotFoundException( 'Update not found!' )
2015-06-03 21:05:13 +00:00
return path
2016-06-08 20:27:22 +00:00
def GetFilePath( hash ):
2015-03-18 21:46:29 +00:00
2016-06-08 20:27:22 +00:00
path = GetExpectedFilePath( hash )
2015-03-18 21:46:29 +00:00
2015-11-04 22:30:28 +00:00
if not os.path.exists( path ):
2015-06-03 21:05:13 +00:00
2016-06-08 20:27:22 +00:00
raise HydrusExceptions.NotFoundException( 'File not found!' )
2015-06-03 21:05:13 +00:00
2015-11-04 22:30:28 +00:00
return path
2015-06-03 21:05:13 +00:00
2016-06-08 20:27:22 +00:00
def GetThumbnailPath( hash ):
2015-03-18 21:46:29 +00:00
2016-06-08 20:27:22 +00:00
path = GetExpectedThumbnailPath( hash )
2015-03-18 21:46:29 +00:00
2016-06-08 20:27:22 +00:00
if not os.path.exists( path ):
raise HydrusExceptions.NotFoundException( 'Thumbnail not found!' )
return path
2015-03-18 21:46:29 +00:00
2016-06-08 20:27:22 +00:00
def IterateAllPaths( file_type ):
for prefix in HydrusData.IterateHexPrefixes():
2015-03-18 21:46:29 +00:00
2016-06-08 20:27:22 +00:00
dir = os.path.join( HC.SERVER_FILES_DIR, prefix )
2015-03-18 21:46:29 +00:00
2016-06-08 20:27:22 +00:00
filenames = os.listdir( dir )
2015-03-18 21:46:29 +00:00
2016-06-08 20:27:22 +00:00
for filename in filenames:
if file_type == 'file' and filename.endswith( '.thumbnail' ):
continue
elif file_type == 'thumbnail' and not filename.endswith( '.thumbnail' ):
continue
2015-11-04 22:30:28 +00:00
2016-06-08 20:27:22 +00:00
yield os.path.join( dir, filename )
2015-11-04 22:30:28 +00:00
2015-03-18 21:46:29 +00:00