MCPcopy Create free account
hub / github.com/TheSmallHanCat/flow2api / download_and_cache

Method download_and_cache

src/services/file_cache.py:279–443  ·  view source on GitHub ↗

Download file from URL and cache it locally Args: url: File URL to download media_type: 'image' or 'video' Returns: Local cache filename

(self, url: str, media_type: str)

Source from the content-addressed store, hash-verified

277 return message
278
279 async def download_and_cache(self, url: str, media_type: str) -> str:
280 """
281 Download file from URL and cache it locally
282
283 Args:
284 url: File URL to download
285 media_type: 'image' or 'video'
286
287 Returns:
288 Local cache filename
289 """
290 filename = self._generate_cache_filename(url, media_type)
291 file_path = self.cache_dir / filename
292 download_lock = self._download_locks.setdefault(filename, asyncio.Lock())
293
294 async with download_lock:
295 # Check if already cached and not expired
296 if file_path.exists():
297 if self._is_cleanup_disabled():
298 return filename
299 file_age = time.time() - file_path.stat().st_mtime
300 if file_age < self.default_timeout:
301 debug_logger.log_info(f"Cache hit: {filename}")
302 return filename
303 try:
304 file_path.unlink()
305 except Exception:
306 pass
307
308 # Download file
309 debug_logger.log_info(f"Downloading file from: {url}")
310
311 fingerprint = self._get_request_fingerprint()
312 proxy_url = await self._resolve_download_proxy(media_type, fingerprint=fingerprint)
313 headers = self._build_download_headers(media_type, fingerprint=fingerprint)
314
315 # Try method 1: curl_cffi with browser impersonation
316 try:
317 async with AsyncSession() as session:
318 response = await session.get(
319 url,
320 timeout=60,
321 proxy=proxy_url,
322 headers=headers,
323 impersonate="chrome120",
324 verify=False
325 )
326
327 if response.status_code == 200 and response.content:
328 self._write_cached_content(file_path, response.content)
329 debug_logger.log_info(
330 f"File cached (curl_cffi): {filename} ({len(response.content)} bytes)"
331 )
332 return filename
333 debug_logger.log_warning(
334 f"curl_cffi failed with HTTP {response.status_code}, trying wget..."
335 )
336

Callers 2

_poll_video_resultMethod · 0.80

Calls 10

_is_cleanup_disabledMethod · 0.95
_write_cached_contentMethod · 0.95
log_infoMethod · 0.80
log_warningMethod · 0.80
log_errorMethod · 0.80

Tested by

no test coverage detected