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

@@ -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