fix commited on
Commit
a4bb9d0
·
1 Parent(s): 31d7082

fix: subdirectory cache support, URL-decode, parent dirs

Browse files
Files changed (1) hide show
  1. app.py +10 -8
app.py CHANGED
@@ -54,6 +54,7 @@ class SmartCache:
54
  def put(self, key, src_path, content_type="video/mp4"):
55
  with LOCK:
56
  fpath = CACHE_DIR / key
 
57
  shutil.copy2(src_path, fpath)
58
  meta = CACHE_DIR / f"{key}.meta"
59
  with open(meta, "w") as f:
@@ -69,7 +70,7 @@ class SmartCache:
69
  files = 0
70
  size = 0
71
  with LOCK:
72
- for f in CACHE_DIR.iterdir():
73
  if f.is_file() and not f.name.endswith(".meta"):
74
  files += 1
75
  size += f.stat().st_size
@@ -77,13 +78,13 @@ class SmartCache:
77
 
78
  def cleanup(self):
79
  with LOCK:
80
- total = sum(f.stat().st_size for f in CACHE_DIR.iterdir()
81
  if f.is_file() and not f.name.endswith(".meta"))
82
  if total < MAX_CACHE_BYTES:
83
  return
84
 
85
  entries = []
86
- for f in CACHE_DIR.iterdir():
87
  if f.is_file() and not f.name.endswith(".meta"):
88
  meta = CACHE_DIR / f"{f.name}.meta"
89
  hits = 0
@@ -129,7 +130,7 @@ class Handler(http.server.BaseHTTPRequestHandler):
129
  return
130
 
131
  if parsed.path.startswith("/stream/"):
132
- key = parsed.path.split("/stream/", 1)[-1]
133
  if not key:
134
  self._json(400, {"error": "Missing file key"})
135
  return
@@ -143,9 +144,10 @@ class Handler(http.server.BaseHTTPRequestHandler):
143
 
144
  if parsed.path == "/list":
145
  files = []
146
- for f in sorted(CACHE_DIR.iterdir()):
147
  if f.is_file() and not f.name.endswith(".meta"):
148
- meta = CACHE_DIR / f"{f.name}.meta"
 
149
  expires = 0
150
  hits = 0
151
  if meta.exists():
@@ -157,7 +159,7 @@ class Handler(http.server.BaseHTTPRequestHandler):
157
  except:
158
  pass
159
  files.append({
160
- "key": f.name,
161
  "size": f.stat().st_size,
162
  "expires_at": datetime.fromtimestamp(expires).isoformat() if expires else "N/A",
163
  "hits": hits,
@@ -216,7 +218,7 @@ class Handler(http.server.BaseHTTPRequestHandler):
216
  if path == "/flush":
217
  with LOCK:
218
  count = 0
219
- for f in CACHE_DIR.iterdir():
220
  if f.is_file():
221
  f.unlink()
222
  count += 1
 
54
  def put(self, key, src_path, content_type="video/mp4"):
55
  with LOCK:
56
  fpath = CACHE_DIR / key
57
+ fpath.parent.mkdir(parents=True, exist_ok=True)
58
  shutil.copy2(src_path, fpath)
59
  meta = CACHE_DIR / f"{key}.meta"
60
  with open(meta, "w") as f:
 
70
  files = 0
71
  size = 0
72
  with LOCK:
73
+ for f in CACHE_DIR.rglob("*"):
74
  if f.is_file() and not f.name.endswith(".meta"):
75
  files += 1
76
  size += f.stat().st_size
 
78
 
79
  def cleanup(self):
80
  with LOCK:
81
+ total = sum(f.stat().st_size for f in CACHE_DIR.rglob("*")
82
  if f.is_file() and not f.name.endswith(".meta"))
83
  if total < MAX_CACHE_BYTES:
84
  return
85
 
86
  entries = []
87
+ for f in CACHE_DIR.rglob("*"):
88
  if f.is_file() and not f.name.endswith(".meta"):
89
  meta = CACHE_DIR / f"{f.name}.meta"
90
  hits = 0
 
130
  return
131
 
132
  if parsed.path.startswith("/stream/"):
133
+ key = urllib.parse.unquote(parsed.path.split("/stream/", 1)[-1])
134
  if not key:
135
  self._json(400, {"error": "Missing file key"})
136
  return
 
144
 
145
  if parsed.path == "/list":
146
  files = []
147
+ for f in sorted(CACHE_DIR.rglob("*")):
148
  if f.is_file() and not f.name.endswith(".meta"):
149
+ rel = str(f.relative_to(CACHE_DIR))
150
+ meta = f.parent / f"{f.name}.meta"
151
  expires = 0
152
  hits = 0
153
  if meta.exists():
 
159
  except:
160
  pass
161
  files.append({
162
+ "key": rel,
163
  "size": f.stat().st_size,
164
  "expires_at": datetime.fromtimestamp(expires).isoformat() if expires else "N/A",
165
  "hits": hits,
 
218
  if path == "/flush":
219
  with LOCK:
220
  count = 0
221
+ for f in CACHE_DIR.rglob("*"):
222
  if f.is_file():
223
  f.unlink()
224
  count += 1