diff --git a/cache_manager.py b/cache_manager.py index d67de47..5b82c0a 100644 --- a/cache_manager.py +++ b/cache_manager.py @@ -496,13 +496,27 @@ class L1CacheManager: if legacy_path.exists() and legacy_path.is_file(): return legacy_path - # Try to recover from IPFS if we have a CID - ipfs_cid = self._get_ipfs_cid_from_index(cid) - if ipfs_cid: - logger.info(f"Recovering from IPFS: {cid[:16]}... ({ipfs_cid})") + # Fetch from IPFS - this is the source of truth for all content + if self._is_ipfs_cid(cid): + logger.info(f"get_by_cid: Fetching from IPFS: {cid[:16]}...") recovery_path = self.legacy_dir / cid - if ipfs_client.get_file(ipfs_cid, recovery_path): - logger.info(f"Recovered from IPFS: {recovery_path}") + recovery_path.parent.mkdir(parents=True, exist_ok=True) + if ipfs_client.get_file(cid, str(recovery_path)): + logger.info(f"get_by_cid: Fetched from IPFS: {recovery_path}") + self._set_content_index(cid, cid) + return recovery_path + else: + logger.warning(f"get_by_cid: IPFS fetch failed for {cid[:16]}...") + + # Also try with a mapped IPFS CID if different from cid + ipfs_cid = self._get_ipfs_cid_from_index(cid) + if ipfs_cid and ipfs_cid != cid: + logger.info(f"get_by_cid: Fetching from IPFS via mapping: {ipfs_cid[:16]}...") + recovery_path = self.legacy_dir / cid + recovery_path.parent.mkdir(parents=True, exist_ok=True) + if ipfs_client.get_file(ipfs_cid, str(recovery_path)): + logger.info(f"get_by_cid: Fetched from IPFS: {recovery_path}") + return recovery_path return recovery_path return None