hydrus/hydrus/server/ServerFiles.py

83 lines
1.9 KiB
Python
Raw Normal View History

2020-07-29 20:52:44 +00:00
import os
2020-04-22 21:00:35 +00:00
from hydrus.core import HydrusData
from hydrus.core import HydrusExceptions
from hydrus.core import HydrusGlobals as HG
2023-04-19 20:38:13 +00:00
from hydrus.core import HydrusTime
2015-03-18 21:46:29 +00:00
2016-07-20 19:57:10 +00:00
def GetAllHashes( file_type ):
2019-01-09 22:59:03 +00:00
return { bytes.fromhex( os.path.split( path )[1] ) for path in IterateAllPaths( file_type ) }
2016-07-20 19:57:10 +00:00
2016-06-08 20:27:22 +00:00
def GetExpectedFilePath( hash ):
2015-03-18 21:46:29 +00:00
2017-05-10 21:33:58 +00:00
files_dir = HG.server_controller.GetFilesDir()
2016-10-12 21:52:50 +00:00
2019-01-09 22:59:03 +00:00
hash_encoded = hash.hex()
2016-06-08 20:27:22 +00:00
first_two_chars = hash_encoded[:2]
2016-10-12 21:52:50 +00:00
path = os.path.join( files_dir, first_two_chars, hash_encoded )
2016-06-08 20:27:22 +00:00
return path
def GetExpectedThumbnailPath( hash ):
2015-03-18 21:46:29 +00:00
2017-05-10 21:33:58 +00:00
files_dir = HG.server_controller.GetFilesDir()
2016-10-12 21:52:50 +00:00
2019-01-09 22:59:03 +00:00
hash_encoded = hash.hex()
2015-03-18 21:46:29 +00:00
first_two_chars = hash_encoded[:2]
2016-10-12 21:52:50 +00:00
path = os.path.join( files_dir, first_two_chars, hash_encoded + '.thumbnail' )
2015-03-18 21:46:29 +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 ):
2017-05-10 21:33:58 +00:00
files_dir = HG.server_controller.GetFilesDir()
2016-10-12 21:52:50 +00:00
2016-06-08 20:27:22 +00:00
for prefix in HydrusData.IterateHexPrefixes():
2015-03-18 21:46:29 +00:00
2016-10-12 21:52:50 +00:00
dir = os.path.join( 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
2017-03-02 02:14:56 +00:00