Skip to content

Commit 3000e36

Browse files
committed
fixed txt download error
1 parent 947b561 commit 3000e36

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434

3535
# 更新日志
3636

37+
## `v2.6.1`
38+
39+
- 修复下载某些 txt 文件失败的问题[#53](https://github.com/zaxtyson/LanZouCloud-API/issues/53)
40+
3741
## `v2.6.0`
3842

3943
- 修复无法上传文件的问题 [#52](https://github.com/zaxtyson/LanZouCloud-API/pull/52)

lanzou/api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from lanzou.api.core import LanZouCloud
22

3-
version = '2.6.0'
3+
version = '2.6.1'
44

55
__all__ = ['utils', 'types', 'models', 'LanZouCloud', 'version']

lanzou/api/core.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,8 +947,26 @@ def down_file_by_url(self, share_url, pwd='', save_path='./Download', *, callbac
947947
resp = self._get(info.durl, stream=True)
948948
if not resp:
949949
return LanZouCloud.FAILED
950-
total_size = int(resp.headers['Content-Length'])
951950

951+
content_length = resp.headers.get('Content-Length', None)
952+
# 如果无法获取 Content-Length, 先读取一点数据, 再尝试获取一次
953+
# 通常只需读取 1 字节数据
954+
data_iter = resp.iter_content(chunk_size=1)
955+
while not content_length:
956+
logger.warning("Not found Content-Length in response headers")
957+
logger.debug("Read 1 byte from stream...")
958+
try:
959+
next(data_iter)
960+
except StopIteration:
961+
logger.debug("Please wait for a moment before downloading")
962+
return LanZouCloud.FAILED
963+
resp_ = self._get(info.durl, stream=True)
964+
if not resp_:
965+
return LanZouCloud.FAILED
966+
content_length = resp_.headers.get('Content-Length', None)
967+
logger.debug(f"Content-Length: {content_length}")
968+
969+
total_size = int(content_length)
952970
file_path = save_path + os.sep + info.name
953971
if os.path.exists(file_path):
954972
if overwrite:

0 commit comments

Comments
 (0)