mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-04-30 05:31:40 +08:00
fix storage api
This commit is contained in:
@@ -185,15 +185,15 @@ class FileManagerModule(_ModuleBase):
|
||||
return False
|
||||
return storage_oper.rename(fileitem, name)
|
||||
|
||||
def download_file(self, fileitem: FileItem, path: Path) -> bool:
|
||||
def download_file(self, fileitem: FileItem) -> Optional[Path]:
|
||||
"""
|
||||
下载文件
|
||||
"""
|
||||
storage_oper = self.__get_storage_oper(fileitem.storage)
|
||||
if not storage_oper:
|
||||
logger.error(f"不支持 {fileitem.storage} 的下载处理")
|
||||
return False
|
||||
return storage_oper.download(fileitem, path)
|
||||
return None
|
||||
return storage_oper.download(fileitem)
|
||||
|
||||
def upload_file(self, fileitem: FileItem, path: Path) -> Optional[FileItem]:
|
||||
"""
|
||||
@@ -414,13 +414,19 @@ class FileManagerModule(_ModuleBase):
|
||||
# 网盘到本地
|
||||
if transfer_type == "copy":
|
||||
# 下载
|
||||
if source_oper.download(fileitem, target_file):
|
||||
tmp_file = source_oper.download(fileitem)
|
||||
if tmp_file:
|
||||
# 将tmp_file移动后target_file
|
||||
tmp_file.rename(target_file)
|
||||
return __get_targetitem(target_file), ""
|
||||
else:
|
||||
return None, f"{fileitem.path} {fileitem.storage} 下载失败"
|
||||
elif transfer_type == "move":
|
||||
# 下载
|
||||
if source_oper.download(fileitem, target_file):
|
||||
tmp_file = source_oper.download(fileitem)
|
||||
if tmp_file:
|
||||
# 将tmp_file移动后target_file
|
||||
tmp_file.rename(target_file)
|
||||
# 删除源文件
|
||||
source_oper.delete(fileitem)
|
||||
return __get_targetitem(target_file), ""
|
||||
|
||||
@@ -96,9 +96,9 @@ class StorageBase(metaclass=ABCMeta):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def download(self, fileitm: schemas.FileItem, path: Path) -> bool:
|
||||
def download(self, fileitm: schemas.FileItem) -> Path:
|
||||
"""
|
||||
下载文件,保存到本地
|
||||
下载文件,保存到本地,返回本地临时文件地址
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
@@ -587,13 +587,13 @@ class AliPan(StorageBase):
|
||||
self.__handle_error(res, "重命名文件")
|
||||
return False
|
||||
|
||||
def download(self, fileitem: schemas.FileItem, path: Path) -> bool:
|
||||
def download(self, fileitem: schemas.FileItem) -> Optional[Path]:
|
||||
"""
|
||||
下载文件,保存到本地
|
||||
"""
|
||||
params = self.__access_params
|
||||
if not params:
|
||||
return False
|
||||
return None
|
||||
headers = self.__get_headers(params)
|
||||
res = RequestUtils(headers=headers, timeout=10).post_res(self.download_url, json={
|
||||
"drive_id": fileitem.drive_id,
|
||||
@@ -602,15 +602,16 @@ class AliPan(StorageBase):
|
||||
if res:
|
||||
download_url = res.json().get("url")
|
||||
if not download_url:
|
||||
return False
|
||||
return None
|
||||
res = RequestUtils().get_res(download_url)
|
||||
if res:
|
||||
path = settings.TEMP_PATH / fileitem.name
|
||||
with path.open("wb") as f:
|
||||
f.write(res.content)
|
||||
return True
|
||||
return path
|
||||
else:
|
||||
self.__handle_error(res, "获取下载链接")
|
||||
return False
|
||||
return None
|
||||
|
||||
def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]:
|
||||
"""
|
||||
|
||||
@@ -177,11 +177,11 @@ class LocalStorage(StorageBase):
|
||||
return False
|
||||
return True
|
||||
|
||||
def download(self, fileitem: schemas.FileItem, path: Path) -> bool:
|
||||
def download(self, fileitem: schemas.FileItem) -> Optional[Path]:
|
||||
"""
|
||||
下载文件
|
||||
"""
|
||||
pass
|
||||
return Path(fileitem.path)
|
||||
|
||||
def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]:
|
||||
"""
|
||||
|
||||
@@ -5,6 +5,7 @@ from pathlib import Path
|
||||
from typing import Optional, List
|
||||
|
||||
from app import schemas
|
||||
from app.core.config import settings
|
||||
from app.log import logger
|
||||
from app.modules.filemanager.storages import StorageBase
|
||||
from app.schemas.types import StorageSchema
|
||||
@@ -225,24 +226,25 @@ class Rclone(StorageBase):
|
||||
logger.error(f"rclone重命名文件失败:{err}")
|
||||
return False
|
||||
|
||||
def download(self, fileitm: schemas.FileItem, path: Path) -> bool:
|
||||
def download(self, fileitem: schemas.FileItem) -> Optional[Path]:
|
||||
"""
|
||||
下载文件
|
||||
"""
|
||||
path = settings.TEMP_PATH / fileitem.name
|
||||
try:
|
||||
retcode = subprocess.run(
|
||||
[
|
||||
'rclone', 'copyto',
|
||||
f'MP:{fileitm.path}',
|
||||
f'MP:{fileitem.path}',
|
||||
f'{path}'
|
||||
],
|
||||
startupinfo=self.__get_hidden_shell()
|
||||
).returncode
|
||||
if retcode == 0:
|
||||
return True
|
||||
return path
|
||||
except Exception as err:
|
||||
logger.error(f"rclone复制文件失败:{err}")
|
||||
return False
|
||||
return None
|
||||
|
||||
def upload(self, fileitm: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]:
|
||||
"""
|
||||
|
||||
@@ -8,6 +8,7 @@ from py115 import Cloud
|
||||
from py115.types import LoginTarget, QrcodeSession, QrcodeStatus, Credential
|
||||
|
||||
from app import schemas
|
||||
from app.core.config import settings
|
||||
from app.log import logger
|
||||
from app.modules.filemanager.storages import StorageBase
|
||||
from app.schemas.types import StorageSchema
|
||||
@@ -289,23 +290,24 @@ class U115Pan(StorageBase, metaclass=Singleton):
|
||||
logger.error(f"115重命名文件失败:{str(e)}")
|
||||
return False
|
||||
|
||||
def download(self, fileitem: schemas.FileItem, path: Path) -> bool:
|
||||
def download(self, fileitem: schemas.FileItem) -> Optional[Path]:
|
||||
"""
|
||||
获取下载链接
|
||||
"""
|
||||
if not self.__init_cloud():
|
||||
return False
|
||||
return None
|
||||
try:
|
||||
ticket = self.cloud.storage().request_download(fileitem.pickcode)
|
||||
if ticket:
|
||||
path = settings.TEMP_PATH / fileitem.name
|
||||
res = RequestUtils(headers=ticket.headers).get_res(ticket.url)
|
||||
if res:
|
||||
with open(path, "wb") as f:
|
||||
f.write(res.content)
|
||||
return True
|
||||
return path
|
||||
except Exception as e:
|
||||
logger.error(f"115下载失败:{str(e)}")
|
||||
return False
|
||||
return None
|
||||
|
||||
def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user