Files
dify/api/core/helper/download.py
-LAN- 4398f40450 fix(api): centralize remote file retrieval
Introduce a unified remote file fetcher that resolves first-party signed file URLs through database records and storage before falling back to the SSRF-protected HTTP client.

Route backend remote-file call sites through the new boundary, remove obsolete file signature verification helpers, and document when to use remote_fetcher versus ssrf_proxy.
2026-05-19 19:41:46 +08:00

18 lines
544 B
Python

from core.file import remote_fetcher
def download_with_size_limit(url, max_download_size: int, **kwargs):
response = remote_fetcher.get(url, follow_redirects=True, **kwargs)
if response.status_code == 404:
raise ValueError("file not found")
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