This commit is contained in:
jxxghp
2024-11-16 09:25:46 +08:00
parent af3a50f7ea
commit f6a2efb256
8 changed files with 31 additions and 27 deletions

View File

@@ -57,13 +57,14 @@ class StorageChain(ChainBase):
"""
return self.run_module("download_file", fileitem=fileitem, path=path)
def upload_file(self, fileitem: schemas.FileItem, path: Path) -> Optional[bool]:
def upload_file(self, fileitem: schemas.FileItem, path: Path, new_name: str = None) -> Optional[bool]:
"""
上传文件
:param fileitem: 保存目录项
:param path: 本地文件路径
:param new_name: 新文件名
"""
return self.run_module("upload_file", fileitem=fileitem, path=path)
return self.run_module("upload_file", fileitem=fileitem, path=path, new_name=new_name)
def delete_file(self, fileitem: schemas.FileItem) -> Optional[bool]:
"""

View File

@@ -268,7 +268,7 @@ class FileManagerModule(_ModuleBase):
return None
return storage_oper.download(fileitem, path=path)
def upload_file(self, fileitem: FileItem, path: Path) -> Optional[FileItem]:
def upload_file(self, fileitem: FileItem, path: Path, new_name: str = None) -> Optional[FileItem]:
"""
上传文件
"""
@@ -276,7 +276,7 @@ class FileManagerModule(_ModuleBase):
if not storage_oper:
logger.error(f"不支持 {fileitem.storage} 的上传处理")
return None
return storage_oper.upload(fileitem, path)
return storage_oper.upload(fileitem, path, new_name)
def get_file_item(self, storage: str, path: Path) -> Optional[FileItem]:
"""
@@ -487,13 +487,8 @@ class FileManagerModule(_ModuleBase):
target_fileitem = target_oper.get_folder(target_file.parent)
if target_fileitem:
# 上传文件
new_item = target_oper.upload(target_fileitem, filepath)
new_item = target_oper.upload(target_fileitem, filepath, target_file.name)
if new_item:
# 重命名为目标文件名
if new_item.name != target_file.name:
if target_oper.rename(new_item, target_file.name):
new_item.name = target_file.name
new_item.path = str(Path(new_item.path).parent / target_file.name)
return new_item, ""
else:
return None, f"{fileitem.path} 上传 {target_storage} 失败"
@@ -505,13 +500,8 @@ class FileManagerModule(_ModuleBase):
target_fileitem = target_oper.get_folder(target_file.parent)
if target_fileitem:
# 上传文件
new_item = target_oper.upload(target_fileitem, filepath)
new_item = target_oper.upload(target_fileitem, filepath, target_file.name)
if new_item:
# 重命名为目标文件名
if new_item.name != target_file.name:
if target_oper.rename(new_item, target_file.name):
new_item.name = target_file.name
new_item.path = str(Path(new_item.path).parent / target_file.name)
# 删除源文件
source_oper.delete(fileitem)
return new_item, ""

View File

@@ -119,11 +119,12 @@ class StorageBase(metaclass=ABCMeta):
pass
@abstractmethod
def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]:
def upload(self, fileitem: schemas.FileItem, path: Path, new_name: str = None) -> Optional[schemas.FileItem]:
"""
上传文件
:param fileitem: 上传目录项
:param path: 本地文件路径
:param new_name: 上传后文件名
"""
pass

View File

@@ -353,7 +353,7 @@ class AliPan(StorageBase):
return Path(local_path)
return None
def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]:
def upload(self, fileitem: schemas.FileItem, path: Path, new_name: str = None) -> Optional[schemas.FileItem]:
"""
上传文件,并标记完成
"""
@@ -361,7 +361,7 @@ class AliPan(StorageBase):
return None
# 上传文件
result = self.aligo.upload_file(file_path=str(path), parent_file_id=fileitem.fileid,
drive_id=fileitem.drive_id, name=path.name,
drive_id=fileitem.drive_id, name=new_name or path.name,
check_name_mode="refuse")
if result:
item = self.aligo.get_file(file_id=result.file_id, drive_id=result.drive_id)

View File

@@ -534,12 +534,13 @@ class Alist(StorageBase):
return None
def upload(
self, fileitem: schemas.FileItem, path: Path, task: bool = False
self, fileitem: schemas.FileItem, path: Path, new_name: str = None, task: bool = False
) -> Optional[schemas.FileItem]:
"""
上传文件
:param fileitem: 上传目录项
:param path: 本地文件路径
:param new_name: 上传后文件名
:param task: 是否为任务默认为False避免未完成上传时对文件进行操作
"""
encoded_path = UrlUtils.quote(fileitem.path)
@@ -557,7 +558,11 @@ class Alist(StorageBase):
logging.warning(f"请求上传文件 {path} 失败,状态码:{resp.status_code}")
return
return fileitem
if new_name and new_name != path.name:
if self.rename(fileitem, new_name):
return self.get_item(Path(fileitem.path).parent / new_name)
return self.get_item(Path(fileitem.path) / path.name)
def detail(self, fileitem: schemas.FileItem) -> Optional[schemas.FileItem]:
"""

View File

@@ -183,12 +183,12 @@ class LocalStorage(StorageBase):
"""
return Path(fileitem.path)
def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]:
def upload(self, fileitem: schemas.FileItem, path: Path, new_name: str = None) -> Optional[schemas.FileItem]:
"""
上传文件
"""
dir_path = Path(fileitem.path)
target_path = dir_path / path.name
target_path = dir_path / (new_name or path.name)
code, message = SystemUtils.move(path, target_path)
if code != 0:
logger.error(f"移动文件失败:{message}")

View File

@@ -260,7 +260,7 @@ class Rclone(StorageBase):
logger.error(f"rclone复制文件失败{err}")
return None
def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]:
def upload(self, fileitem: schemas.FileItem, path: Path, new_name: str = None) -> Optional[schemas.FileItem]:
"""
上传文件
"""
@@ -269,7 +269,7 @@ class Rclone(StorageBase):
[
'rclone', 'copyto',
str(path),
f'MP:{Path(fileitem.path) / path.name}'
f'MP:{Path(fileitem.path) / (new_name or path.name)}'
],
startupinfo=self.__get_hidden_shell()
).returncode

View File

@@ -328,7 +328,7 @@ class U115Pan(StorageBase, metaclass=Singleton):
logger.error(f"115下载失败{str(e)}")
return None
def upload(self, fileitem: schemas.FileItem, path: Path) -> Optional[schemas.FileItem]:
def upload(self, fileitem: schemas.FileItem, path: Path, new_name: str = None) -> Optional[schemas.FileItem]:
"""
上传文件
"""
@@ -359,7 +359,7 @@ class U115Pan(StorageBase, metaclass=Singleton):
if result:
result_data = result.get('data')
logger.info(f"115上传文件成功{result_data.get('file_name')}")
return schemas.FileItem(
item = schemas.FileItem(
storage=self.schema.value,
fileid=result_data.get('file_id'),
parent_fileid=fileitem.fileid,
@@ -371,6 +371,13 @@ class U115Pan(StorageBase, metaclass=Singleton):
extension=Path(result_data.get('file_name')).suffix[1:],
pickcode=result_data.get('pickcode')
)
if new_name and new_name != item.name:
if self.rename(item, new_name):
item.name = new_name
item.basename = Path(new_name).stem
item.path = f"{fileitem.path}{new_name}"
item.extension = Path(new_name).suffix[1:]
return item
else:
logger.warn(f"115上传文件失败{por.resp.response.text}")
return None