Skip to content

Commit

Permalink
fixed txt download error
Browse files Browse the repository at this point in the history
  • Loading branch information
zaxtyson committed Mar 3, 2021
1 parent 947b561 commit 3000e36
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@

# 更新日志

## `v2.6.1`

- 修复下载某些 txt 文件失败的问题[#53](https://github.com/zaxtyson/LanZouCloud-API/issues/53)

## `v2.6.0`

- 修复无法上传文件的问题 [#52](https://github.com/zaxtyson/LanZouCloud-API/pull/52)
Expand Down
2 changes: 1 addition & 1 deletion lanzou/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from lanzou.api.core import LanZouCloud

version = '2.6.0'
version = '2.6.1'

__all__ = ['utils', 'types', 'models', 'LanZouCloud', 'version']
20 changes: 19 additions & 1 deletion lanzou/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,8 +947,26 @@ def down_file_by_url(self, share_url, pwd='', save_path='./Download', *, callbac
resp = self._get(info.durl, stream=True)
if not resp:
return LanZouCloud.FAILED
total_size = int(resp.headers['Content-Length'])

content_length = resp.headers.get('Content-Length', None)
# 如果无法获取 Content-Length, 先读取一点数据, 再尝试获取一次
# 通常只需读取 1 字节数据
data_iter = resp.iter_content(chunk_size=1)
while not content_length:
logger.warning("Not found Content-Length in response headers")
logger.debug("Read 1 byte from stream...")
try:
next(data_iter)
except StopIteration:
logger.debug("Please wait for a moment before downloading")
return LanZouCloud.FAILED
resp_ = self._get(info.durl, stream=True)
if not resp_:
return LanZouCloud.FAILED
content_length = resp_.headers.get('Content-Length', None)
logger.debug(f"Content-Length: {content_length}")

total_size = int(content_length)
file_path = save_path + os.sep + info.name
if os.path.exists(file_path):
if overwrite:
Expand Down

0 comments on commit 3000e36

Please sign in to comment.