[Enhance] Add an option about file hash pre-calc

- Add an option to disable song file hash pre-calculation
This commit is contained in:
Lost-MSth
2023-01-14 18:32:53 +08:00
parent af03a48134
commit 9c90d6ef89
5 changed files with 21 additions and 13 deletions

5
.gitignore vendored
View File

@@ -17,4 +17,7 @@ __pycache__/
# setting/config files
latest version/config/
latest version/config.py
latest version/config.py
# song data
latest version/database/songs/

View File

@@ -10,6 +10,8 @@ class Config:
USE_PROXY_FIX = False
USE_CORS = False
SONG_FILE_HASH_PRE_CALCULATE = True
GAME_API_PREFIX = '/join/21'
ALLOW_APPVERSION = [] # list[str]

View File

@@ -5,6 +5,7 @@ from time import time
from flask import url_for
from .config_manager import Config
from .constant import Constant
from .error import NoAccess
from .limiter import ArcLimiter
@@ -184,10 +185,11 @@ class DownloadList(UserDownload):
def initialize_cache(cls) -> None:
'''初始化歌曲数据缓存包括md5、文件目录遍历、解析songlist'''
SonglistParser()
x = cls()
x.url_flag = False
x.add_songs()
del x
if Config.SONG_FILE_HASH_PRE_CALCULATE:
x = cls()
x.url_flag = False
x.add_songs()
del x
@staticmethod
def clear_all_cache() -> None:
@@ -212,9 +214,10 @@ class DownloadList(UserDownload):
def get_one_song_file_names(song_id: str) -> list:
'''获取一个歌曲文件夹下的所有合法文件名有lru缓存'''
r = []
for i in os.listdir(os.path.join(Constant.SONG_FILE_FOLDER_PATH, song_id)):
if os.path.isfile(os.path.join(Constant.SONG_FILE_FOLDER_PATH, song_id, i)) and SonglistParser.is_available_file(song_id, i):
r.append(i)
for i in os.scandir(os.path.join(Constant.SONG_FILE_FOLDER_PATH, song_id)):
file_name = i.name
if i.is_file() and SonglistParser.is_available_file(song_id, file_name):
r.append(file_name)
return r
def add_one_song(self, song_id: str) -> None:
@@ -265,7 +268,7 @@ class DownloadList(UserDownload):
@lru_cache()
def get_all_song_ids() -> list:
'''获取全歌曲文件夹列表有lru缓存'''
return list(filter(lambda x: os.path.isdir(os.path.join(Constant.SONG_FILE_FOLDER_PATH, x)), os.listdir(Constant.SONG_FILE_FOLDER_PATH)))
return [i.name for i in os.scandir(Constant.SONG_FILE_FOLDER_PATH) if i.is_dir()]
def add_songs(self, song_ids: list = None) -> None:
'''添加一个或多个歌曲到下载列表,若`song_ids`为空,则添加所有歌曲'''

View File

@@ -286,6 +286,8 @@ class FileChecker:
self.logger.info("Start to initialize song data...")
try:
DownloadList.initialize_cache()
if not Config.SONG_FILE_HASH_PRE_CALCULATE:
self.logger.info('Song file hash pre-calculate is disabled.')
self.logger.info('Complete!')
except Exception as e:
self.logger.error(format_exc())

View File

@@ -13,13 +13,11 @@ def md5(code: str) -> str:
def get_file_md5(file_path: str) -> str:
'''计算文件MD5'''
if not os.path.isfile(file_path):
return None
'''计算文件MD5,假设是文件'''
myhash = hashlib.md5()
with open(file_path, 'rb') as f:
while True:
b = f.read(8096)
b = f.read(8192)
if not b:
break
myhash.update(b)