Add IPFS CID support for asset lookup

- Upload endpoint returns both CID and content_hash
- Cache manager handles both SHA3-256 hashes and IPFS CIDs
- get_by_cid() fetches from IPFS if not cached locally
- Execute tasks support :cid in addition to :hash

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
gilesb
2026-01-12 07:36:18 +00:00
parent c7c7c90909
commit 494a2a8650
4 changed files with 51 additions and 14 deletions

View File

@@ -209,9 +209,9 @@ async def upload_content(
ctx: UserContext = Depends(require_auth),
cache_service: CacheService = Depends(get_cache_service),
):
"""Upload content to cache."""
"""Upload content to cache and IPFS."""
content = await file.read()
content_hash, error = await cache_service.upload_content(
content_hash, ipfs_cid, error = await cache_service.upload_content(
content=content,
filename=file.filename,
actor_id=ctx.actor_id,
@@ -221,7 +221,8 @@ async def upload_content(
raise HTTPException(400, error)
return {
"content_hash": content_hash,
"cid": ipfs_cid,
"content_hash": content_hash, # Legacy, for backwards compatibility
"filename": file.filename,
"size": len(content),
"uploaded": True,

View File

@@ -453,8 +453,8 @@ class CacheService:
content: bytes,
filename: str,
actor_id: str,
) -> Tuple[Optional[str], Optional[str]]:
"""Upload content to cache. Returns (content_hash, error)."""
) -> Tuple[Optional[str], Optional[str], Optional[str]]:
"""Upload content to cache. Returns (content_hash, ipfs_cid, error)."""
import tempfile
try:
@@ -466,7 +466,7 @@ class CacheService:
# Detect MIME type before moving file
mime_type = get_mime_type(tmp_path)
# Store in cache
# Store in cache (also stores in IPFS)
cached, ipfs_cid = self.cache.put(tmp_path, node_type="upload", move=True)
content_hash = cached.content_hash
@@ -479,9 +479,9 @@ class CacheService:
filename=filename
)
return content_hash, None
return content_hash, ipfs_cid, None
except Exception as e:
return None, f"Upload failed: {e}"
return None, None, f"Upload failed: {e}"
async def list_media(
self,