Files
gsfdc/demo-dify/dify/api/core/helper/download.py
T
wuzilongandCursor 6519682885 chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-18 13:59:25 +08:00

35 lines
1.4 KiB
Python

from typing import Any
def download_with_size_limit(url: str, max_download_size: int, **kwargs: Any) -> bytes:
from core.file import remote_fetcher
from core.helper.ssrf_proxy import MaxRetriesExceededError
try:
response = remote_fetcher.make_request("GET", url, follow_redirects=True, **kwargs)
except MaxRetriesExceededError as e:
if e.status_code is None:
# No response was ever received: keep the transport error as is
# instead of inventing a status code.
raise
# Retries ran out on an explicit error response. Report it through the
# same message as the non-retried path so the controller mapping shows
# the upstream status.
raise ValueError(f"Failed to download file: HTTP {e.status_code}") from e
if response.status_code == 404:
raise ValueError("file not found")
if response.status_code >= 400:
# The body of a 4xx/5xx response is an error page, not the requested
# file. Returning it would hand callers an error document as content.
raise ValueError(f"Failed to download file: HTTP {response.status_code}")
total_size = 0
chunks = []
for chunk in response.iter_bytes():
total_size += len(chunk)
if total_size > max_download_size:
raise ValueError("Max file size reached")
chunks.append(chunk)
content = b"".join(chunks)
return content