Jul 14 - Using CDN - Phase 2b
This commit is contained in:
@@ -98,6 +98,10 @@ class LocalBackend:
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def materialize_to_dir(self, keys):
|
||||
# Files already live under the static folder — no copy needed.
|
||||
return self._static_folder(), (lambda: None)
|
||||
|
||||
|
||||
# ── Cloudflare R2 / S3-compatible backend ─────────────────────────────────────
|
||||
|
||||
@@ -201,6 +205,32 @@ class S3Backend:
|
||||
except Exception as e:
|
||||
logger.warning('S3 delete failed for %s: %s', key, e)
|
||||
|
||||
def materialize_to_dir(self, keys):
|
||||
"""Download the given keys to a temp dir mirroring the key layout.
|
||||
|
||||
Returns (base_dir, cleanup); each key resolves at base_dir/key so tools
|
||||
that need real file paths (ReportLab/PIL) work unchanged. Missing or
|
||||
unreadable keys are skipped — callers already guard with os.path.exists.
|
||||
"""
|
||||
import tempfile
|
||||
import shutil
|
||||
|
||||
base = tempfile.mkdtemp(prefix='jqc_media_')
|
||||
for key in {k for k in keys if k}:
|
||||
try:
|
||||
data = self.read(key)
|
||||
except Exception:
|
||||
continue # skip missing; caller guards existence
|
||||
dest = os.path.join(base, key)
|
||||
os.makedirs(os.path.dirname(dest), exist_ok=True)
|
||||
with open(dest, 'wb') as fh:
|
||||
fh.write(data)
|
||||
|
||||
def _cleanup():
|
||||
shutil.rmtree(base, ignore_errors=True)
|
||||
|
||||
return base, _cleanup
|
||||
|
||||
|
||||
_BACKENDS = {
|
||||
'local': LocalBackend,
|
||||
@@ -247,3 +277,7 @@ def delete(key):
|
||||
|
||||
def abs_local_path(key):
|
||||
return get_backend().abs_local_path(key)
|
||||
|
||||
|
||||
def materialize_to_dir(keys):
|
||||
return get_backend().materialize_to_dir(keys)
|
||||
|
||||
Reference in New Issue
Block a user