mirror of
https://github.com/Lost-MSth/Arcaea-server.git
synced 2026-02-13 03:27:26 +08:00
[Enhance] Add an option about file hash pre-calc
- Add an option to disable song file hash pre-calculation
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -18,3 +18,6 @@ __pycache__/
|
|||||||
# setting/config files
|
# setting/config files
|
||||||
latest version/config/
|
latest version/config/
|
||||||
latest version/config.py
|
latest version/config.py
|
||||||
|
|
||||||
|
# song data
|
||||||
|
latest version/database/songs/
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ class Config:
|
|||||||
USE_PROXY_FIX = False
|
USE_PROXY_FIX = False
|
||||||
USE_CORS = False
|
USE_CORS = False
|
||||||
|
|
||||||
|
SONG_FILE_HASH_PRE_CALCULATE = True
|
||||||
|
|
||||||
GAME_API_PREFIX = '/join/21'
|
GAME_API_PREFIX = '/join/21'
|
||||||
|
|
||||||
ALLOW_APPVERSION = [] # list[str]
|
ALLOW_APPVERSION = [] # list[str]
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from time import time
|
|||||||
|
|
||||||
from flask import url_for
|
from flask import url_for
|
||||||
|
|
||||||
|
from .config_manager import Config
|
||||||
from .constant import Constant
|
from .constant import Constant
|
||||||
from .error import NoAccess
|
from .error import NoAccess
|
||||||
from .limiter import ArcLimiter
|
from .limiter import ArcLimiter
|
||||||
@@ -184,10 +185,11 @@ class DownloadList(UserDownload):
|
|||||||
def initialize_cache(cls) -> None:
|
def initialize_cache(cls) -> None:
|
||||||
'''初始化歌曲数据缓存,包括md5、文件目录遍历、解析songlist'''
|
'''初始化歌曲数据缓存,包括md5、文件目录遍历、解析songlist'''
|
||||||
SonglistParser()
|
SonglistParser()
|
||||||
x = cls()
|
if Config.SONG_FILE_HASH_PRE_CALCULATE:
|
||||||
x.url_flag = False
|
x = cls()
|
||||||
x.add_songs()
|
x.url_flag = False
|
||||||
del x
|
x.add_songs()
|
||||||
|
del x
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def clear_all_cache() -> None:
|
def clear_all_cache() -> None:
|
||||||
@@ -212,9 +214,10 @@ class DownloadList(UserDownload):
|
|||||||
def get_one_song_file_names(song_id: str) -> list:
|
def get_one_song_file_names(song_id: str) -> list:
|
||||||
'''获取一个歌曲文件夹下的所有合法文件名,有lru缓存'''
|
'''获取一个歌曲文件夹下的所有合法文件名,有lru缓存'''
|
||||||
r = []
|
r = []
|
||||||
for i in os.listdir(os.path.join(Constant.SONG_FILE_FOLDER_PATH, song_id)):
|
for i in os.scandir(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):
|
file_name = i.name
|
||||||
r.append(i)
|
if i.is_file() and SonglistParser.is_available_file(song_id, file_name):
|
||||||
|
r.append(file_name)
|
||||||
return r
|
return r
|
||||||
|
|
||||||
def add_one_song(self, song_id: str) -> None:
|
def add_one_song(self, song_id: str) -> None:
|
||||||
@@ -265,7 +268,7 @@ class DownloadList(UserDownload):
|
|||||||
@lru_cache()
|
@lru_cache()
|
||||||
def get_all_song_ids() -> list:
|
def get_all_song_ids() -> list:
|
||||||
'''获取全歌曲文件夹列表,有lru缓存'''
|
'''获取全歌曲文件夹列表,有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:
|
def add_songs(self, song_ids: list = None) -> None:
|
||||||
'''添加一个或多个歌曲到下载列表,若`song_ids`为空,则添加所有歌曲'''
|
'''添加一个或多个歌曲到下载列表,若`song_ids`为空,则添加所有歌曲'''
|
||||||
|
|||||||
@@ -286,6 +286,8 @@ class FileChecker:
|
|||||||
self.logger.info("Start to initialize song data...")
|
self.logger.info("Start to initialize song data...")
|
||||||
try:
|
try:
|
||||||
DownloadList.initialize_cache()
|
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!')
|
self.logger.info('Complete!')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(format_exc())
|
self.logger.error(format_exc())
|
||||||
|
|||||||
@@ -13,13 +13,11 @@ def md5(code: str) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def get_file_md5(file_path: str) -> str:
|
def get_file_md5(file_path: str) -> str:
|
||||||
'''计算文件MD5'''
|
'''计算文件MD5,假设是文件'''
|
||||||
if not os.path.isfile(file_path):
|
|
||||||
return None
|
|
||||||
myhash = hashlib.md5()
|
myhash = hashlib.md5()
|
||||||
with open(file_path, 'rb') as f:
|
with open(file_path, 'rb') as f:
|
||||||
while True:
|
while True:
|
||||||
b = f.read(8096)
|
b = f.read(8192)
|
||||||
if not b:
|
if not b:
|
||||||
break
|
break
|
||||||
myhash.update(b)
|
myhash.update(b)
|
||||||
|
|||||||
Reference in New Issue
Block a user