Skip to content

Commit

Permalink
[Fix] #54
Browse files Browse the repository at this point in the history
  • Loading branch information
wxy1343 committed Oct 9, 2021
1 parent 97a0588 commit 9ec5873
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion aliyunpan/about.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.8.0'
__version__ = '2.8.1'
24 changes: 17 additions & 7 deletions aliyunpan/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ def create_file(self, file_name: str, parent_file_id: str = 'root', file_type: b
return r

def upload_file(self, parent_file_id: str = 'root', path: str = None, upload_timeout: float = 10,
retry_num: int = 3, force: bool = False, chunk_size: int = None, c: bool = False):
retry_num: int = 3, force: bool = False, chunk_size: int = None, c: bool = False,
ignore: bool = False):
"""
上传文件
:param parent_file_id: 上传目录的id
Expand All @@ -306,14 +307,21 @@ def upload_file(self, parent_file_id: str = 'root', path: str = None, upload_tim
:param force: 强制覆盖
:param chunk_size: 分块大小
:param c: 断点续传
:param ignore: 忽略上传失败的文件
:return:
"""
path = Path(path)
file_size = path.stat().st_size
file_name = path.name
self._chunk_size = chunk_size or self._chunk_size
# 获取sha1
content_hash = get_sha1(path, self._chunk_size)
try:
content_hash = get_sha1(path, self._chunk_size)
except PermissionError:
if not ignore:
self._print.upload_info(path, status=False)
self._print.print_line()
return False
while True:
# 分片列表
part_info_list = []
Expand Down Expand Up @@ -497,9 +505,10 @@ def upload_file(self, parent_file_id: str = 'root', path: str = None, upload_tim
try:
file_info = self.complete(file_id, upload_id)
except InvalidContentHash:
upload_bar.upload_info(path, status=False, refresh_line=True)
if get_real_path(log_file) != get_real_path(path):
raise
if not ignore:
upload_bar.upload_info(path, status=False, refresh_line=True)
if get_real_path(log_file) != get_real_path(path):
raise
if file_info:
upload_bar.upload_info(path, status=True, t=upload_bar.time, average_speed=upload_bar.average_speed,
refresh_line=True)
Expand All @@ -508,8 +517,9 @@ def upload_file(self, parent_file_id: str = 'root', path: str = None, upload_tim
GLOBAL_VAR.file_set.add((content_hash, str(get_real_path(path))))
return file_info
else:
upload_bar.upload_info(path, status=False, refresh_line=True)
self._print.print_line()
if not ignore:
upload_bar.upload_info(path, status=False, refresh_line=True)
self._print.print_line()
return False

def complete(self, file_id, upload_id):
Expand Down
20 changes: 12 additions & 8 deletions aliyunpan/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def mkdir(self, path, name=None, parent_file_id=None):
return file_id_list

def upload(self, path, upload_path='root', timeout=10.0, retry=3, force=False, share=False, chunk_size=None,
c=False):
c=False, ignore=False):
if isinstance(path, (str, AliyunpanPath, Path)):
path_list = (path,)
else:
Expand Down Expand Up @@ -264,7 +264,8 @@ def upload(self, path, upload_path='root', timeout=10.0, retry=3, force=False, s
try:
result = self._disk.upload_file(
parent_file_id=parent_file_id, path=str(path),
upload_timeout=timeout, retry_num=retry, force=force, chunk_size=chunk_size, c=c)
upload_timeout=timeout, retry_num=retry, force=force, chunk_size=chunk_size, c=c,
ignore=ignore)
except KeyboardInterrupt:
self.__del__()
raise
Expand All @@ -287,7 +288,8 @@ def upload(self, path, upload_path='root', timeout=10.0, retry=3, force=False, s
parent_file_id = self._path_list.get_path_fid(file[0], update=False)
result = self._disk.upload_file(
parent_file_id=parent_file_id, path=file[1],
upload_timeout=timeout, retry_num=retry, force=force, chunk_size=chunk_size, c=c)
upload_timeout=timeout, retry_num=retry, force=force, chunk_size=chunk_size, c=c,
ignore=ignore)
except KeyboardInterrupt:
self.__del__()
raise
Expand Down Expand Up @@ -549,11 +551,10 @@ def sync(self, path, upload_path, sync_time, time_out, chunk_size, retry, first=
self._print.print_info(
'Do you really want to synchronize the root? This operation may delete all your files.', error=True)
input('\nEnter to continue.')
first = False
path = AliyunpanPath(path)
relative_path = AliyunpanPath(path.name)
if str(relative_path) == '.':
return self.sync(path.absolute(), upload_path, sync_time, time_out, chunk_size, retry, first=first)
return self.sync(path.absolute(), upload_path, sync_time, time_out, chunk_size, retry, first=False)
upload_path = AliyunpanPath(upload_path)
p = upload_path / relative_path
self._path_list.update_path_list(p, is_fid=False)
Expand All @@ -567,14 +568,17 @@ def sync(self, path, upload_path, sync_time, time_out, chunk_size, retry, first=
for path_ in change_file_list:
relative_path = path.name / (path - path_)
if path_.exists():
self.upload(path_, upload_path / relative_path.parent, force=True, timeout=time_out,
chunk_size=chunk_size, retry=retry)
if not self.upload(path_, upload_path / relative_path.parent, force=True, timeout=time_out,
chunk_size=chunk_size, retry=retry, ignore=True):
if first:
self._print.upload_info(path_, status=False)
self._print.print_line()
else:
self.rm(upload_path / relative_path)
if sync_time:
self._print.wait_info('等待{time}秒后再次同步', t=sync_time, refresh_line=True)
self._print.refresh_line()
self.sync(path, upload_path, sync_time, time_out, chunk_size, retry, first=first)
self.sync(path, upload_path, sync_time, time_out, chunk_size, retry, first=False)

def share_link(self, path_list, file_id_list=None, expiration=None):
t = '' if expiration is None else time.time() + expiration
Expand Down

0 comments on commit 9ec5873

Please sign in to comment.