1
0
mirror of https://github.com/hanxi/xiaomusic.git synced 2026-05-09 00:34:25 +08:00

feat: 下载完成之后修改文件权限为755 close #316

This commit is contained in:
涵曦
2024-12-16 20:20:42 +08:00
parent 8e0fc0bf03
commit d30cb67bc1
3 changed files with 46 additions and 9 deletions

View File

@@ -35,6 +35,7 @@ from starlette.responses import FileResponse, Response
from xiaomusic import __version__
from xiaomusic.utils import (
chmoddir,
convert_file_to_mp3,
deepcopy_data_no_sensitive_info,
download_one_music,
@@ -489,6 +490,7 @@ async def downloadplaylist(data: DownloadPlayList, Verifcation=Depends(verificat
log.debug(f"Download dir_path: {dir_path}")
# 可能只是部分失败,都需要整理下载目录
remove_common_prefix(dir_path)
chmoddir(dir_path)
asyncio.create_task(check_download_proc())
return {"ret": "OK"}
@@ -507,7 +509,15 @@ class DownloadOneMusic(BaseModel):
@app.post("/downloadonemusic")
async def downloadonemusic(data: DownloadOneMusic, Verifcation=Depends(verification)):
try:
await download_one_music(config, data.url, data.name)
download_proc = await download_one_music(config, data.url, data.name)
async def check_download_proc():
# 等待子进程完成
exit_code = await download_proc.wait()
log.info( f"Download completed with exit code {exit_code}")
chmoddir(config.download_path)
asyncio.create_task(check_download_proc())
return {"ret": "OK"}
except Exception as e:
log.exception(f"Execption {e}")

View File

@@ -1016,11 +1016,7 @@ def is_docker():
return os.path.exists("/app/.dockerenv")
def restart_xiaomusic():
if not is_docker():
ret = "xiaomusic 重启只能在 docker 中进行"
log.info(ret)
return False, ret
def _restart_xiaomusic():
try:
# 重启 xiaomusic 程序
subprocess.run(["supervisorctl", "restart", "xiaomusic"], check=True)
@@ -1032,6 +1028,33 @@ def restart_xiaomusic():
def update_version(version):
ok, ret = restart_xiaomusic()
if not is_docker():
ret = "xiaomusic 更新只能在 docker 中进行"
log.info(ret)
return ret
# https://github.com/hanxi/xiaomusic/releases/download/main/app-amd64-lite.tar.gz
ok, ret = _restart_xiaomusic()
if not ok:
return ret
def chmodfile(file_path: str):
try:
os.chmod(file_path, 0o775)
except Exception as e:
log.info(f"chmodfile failed: {e}")
def chmoddir(dir_path: str):
# 获取指定目录下的所有文件和子目录
for item in os.listdir(dir_path):
item_path = os.path.join(dir_path, item)
# 确保是文件,且不是目录
if os.path.isfile(item_path):
try:
os.chmod(item_path, 0o775)
log.info(f"Changed permissions of file: {item_path}")
except Exception as e:
log.info(f"chmoddir failed: {e}")

View File

@@ -41,6 +41,7 @@ from xiaomusic.plugin import PluginManager
from xiaomusic.utils import (
Metadata,
chinese_to_number,
chmodfile,
custom_sort_key,
deepcopy_data_no_sensitive_info,
extract_audio_metadata,
@@ -1433,8 +1434,6 @@ class XiaoMusicDevice:
await self.do_tts(f"本地不存在歌曲{name}")
return
await self.download(search_key, name)
self.log.info(f"正在下载中 {search_key} {name}")
await self._download_proc.wait()
# 把文件插入到播放列表里
await self.add_download_music(name)
await self._playmusic(name)
@@ -1661,6 +1660,11 @@ class XiaoMusicDevice:
self.log.info(f"download cmd: {cmd}")
self._download_proc = await asyncio.create_subprocess_exec(*sbp_args)
await self.do_tts(f"正在下载歌曲{search_key}")
self.log.info(f"正在下载中 {search_key} {name}")
await self._download_proc.wait()
# 下载完成后,修改文件权限
file_path = os.path.join(self.download_path, f"{name}.mp3")
chmodfile(file_path)
# 继续播放被打断的歌曲
async def check_replay(self):