This commit is contained in:
2026-01-14 18:15:00 -08:00
parent d474916874
commit f40d0b61a2
4 changed files with 122 additions and 59 deletions

View File

@@ -277,15 +277,55 @@ class ZeroTier(Store):
debug(f"Hydrus get_file failed: {exc}")
return None
# remote storage: try metadata endpoint
res = self._request_remote("GET", f"/files/{file_hash}")
if isinstance(res, dict):
# remote server returns a 'path' to the file (server-local path)
p = res.get("path") or res.get("file") or None
if isinstance(p, str) and p.startswith("http"):
return p
return p
return None
# remote storage: return download URL
base = self._ensure_client()
if not base or not isinstance(base, str):
return None
url = f"{base.rstrip('/')}/files/raw/{file_hash}"
if self._api_key:
sep = "&" if "?" in url else "?"
url += f"{sep}api_key={self._api_key}"
return url
def download_to_temp(self, file_hash: str, temp_root: Optional[Path] = None) -> Optional[Path]:
"""Download a file from the remote peer to a local temporary file."""
import os
import httpx
import tempfile
if self._service == "hydrus":
return None
url = self.get_file(file_hash)
if not url or not isinstance(url, str) or not url.startswith("http"):
return None
try:
# Use provided temp_root or system temp
if temp_root:
temp_root.mkdir(parents=True, exist_ok=True)
fd, tmp_path = tempfile.mkstemp(dir=str(temp_root), suffix=".tmp")
else:
fd, tmp_path = tempfile.mkstemp(suffix=".tmp")
os_fd = os.fdopen(fd, 'wb')
headers = {}
if self._api_key:
headers["X-API-Key"] = self._api_key
with httpx.stream("GET", url, headers=headers, timeout=self._timeout) as r:
r.raise_for_status()
for chunk in r.iter_bytes():
os_fd.write(chunk)
os_fd.close()
return Path(tmp_path)
except Exception as exc:
debug(f"ZeroTier download_to_temp failed for {file_hash}: {exc}")
return None
def add_file(self, file_path: Path, **kwargs: Any) -> Optional[str]:
"""Upload a local file to the remote ZeroTier peer (supports 'remote' and 'hydrus' services).