Return ``(bytes, fmt)`` for a cached image, or ``None`` on a miss / integrity failure (corrupt or tampered cache file).
(self, url: str)
| 119 | self._valid = True |
| 120 | |
| 121 | def get(self, url: str) -> tuple[bytes, str] | None: |
| 122 | """Return ``(bytes, fmt)`` for a cached image, or ``None`` on a miss / |
| 123 | integrity failure (corrupt or tampered cache file).""" |
| 124 | if not self._valid: |
| 125 | return None |
| 126 | entry = self._index.get(url) |
| 127 | if not entry: |
| 128 | return None |
| 129 | file_name = str(entry.get("file") or "") |
| 130 | if not file_name: |
| 131 | return None |
| 132 | path = self._raw_dir / file_name |
| 133 | if not path.is_file(): |
| 134 | return None |
| 135 | try: |
| 136 | data = path.read_bytes() |
| 137 | except OSError: |
| 138 | return None |
| 139 | if "sha256:" + hashlib.sha256(data).hexdigest() != entry.get("sha256"): |
| 140 | logger.warning("[native_md_cache] cached file integrity mismatch: %s", url) |
| 141 | return None |
| 142 | fmt = str(entry.get("fmt") or "") |
| 143 | self._entries[url] = entry |
| 144 | return data, fmt |
| 145 | |
| 146 | def put(self, url: str, data: bytes, fmt: str) -> None: |
| 147 | """Store freshly-downloaded image bytes and record them for the manifest.""" |
no outgoing calls