q6 commited on
Commit
055e7c6
·
1 Parent(s): 025a41b

Clean legacy cache files on startup

Browse files
Files changed (2) hide show
  1. backend/app.py +10 -1
  2. frontend/app.js +3 -2
backend/app.py CHANGED
@@ -81,6 +81,7 @@ RECLASS_LOG_KEY = "TF#g9FA@UgKdN'usVtm3dTz*yoqbria`"
81
  IMAGE_CACHE_DIR = Path("/data")
82
  IMAGE_CACHE_PASSWORD = '68c6I5B!W[6/"0n>W%G/lUOEY`K|zvQ4'
83
  IMAGE_CACHE_KEY = hashlib.sha256(IMAGE_CACHE_PASSWORD.encode()).digest()
 
84
  THUMB_MAX_AGE = 1800
85
  THUMB_CLEAN_INTERVAL = 300
86
  THUMB_DIR = Path(tempfile.gettempdir()) / "pixif2-thumbs"
@@ -2002,6 +2003,13 @@ def image_cache_path(post_id, page):
2002
  return IMAGE_CACHE_DIR / str(post_id) / f"{page}.epng"
2003
 
2004
 
 
 
 
 
 
 
 
2005
  def write_image_cache(path, data):
2006
  path.parent.mkdir(parents=True, exist_ok=True)
2007
  fd, tmp = tempfile.mkstemp(dir=path.parent, suffix=".tmp")
@@ -2065,7 +2073,7 @@ async def create_webp(post_id, image_url, page=0, kind="t"):
2065
 
2066
  def image_links(post_id, url):
2067
  page = page_num_from_url(url)
2068
- suffix = f"?p={page}"
2069
  pid = quote(str(post_id), safe="")
2070
  thumb_url = f"/api/i/{pid}/t{suffix}"
2071
  preview_url = f"/api/i/{pid}/v{suffix}"
@@ -2327,6 +2335,7 @@ class ReclassRequest(BaseModel):
2327
 
2328
  @app.on_event("startup")
2329
  async def startup():
 
2330
  if not TURSO_DB_URL:
2331
  print("WARN: TURSO_DB_URL not set, skipping DB init")
2332
  return
 
81
  IMAGE_CACHE_DIR = Path("/data")
82
  IMAGE_CACHE_PASSWORD = '68c6I5B!W[6/"0n>W%G/lUOEY`K|zvQ4'
83
  IMAGE_CACHE_KEY = hashlib.sha256(IMAGE_CACHE_PASSWORD.encode()).digest()
84
+ IMAGE_URL_VERSION = 2
85
  THUMB_MAX_AGE = 1800
86
  THUMB_CLEAN_INTERVAL = 300
87
  THUMB_DIR = Path(tempfile.gettempdir()) / "pixif2-thumbs"
 
2003
  return IMAGE_CACHE_DIR / str(post_id) / f"{page}.epng"
2004
 
2005
 
2006
+ def cleanup_image_cache_root():
2007
+ IMAGE_CACHE_DIR.mkdir(parents=True, exist_ok=True)
2008
+ for path in IMAGE_CACHE_DIR.iterdir():
2009
+ if path.is_file():
2010
+ path.unlink()
2011
+
2012
+
2013
  def write_image_cache(path, data):
2014
  path.parent.mkdir(parents=True, exist_ok=True)
2015
  fd, tmp = tempfile.mkstemp(dir=path.parent, suffix=".tmp")
 
2073
 
2074
  def image_links(post_id, url):
2075
  page = page_num_from_url(url)
2076
+ suffix = f"?p={page}&v={IMAGE_URL_VERSION}"
2077
  pid = quote(str(post_id), safe="")
2078
  thumb_url = f"/api/i/{pid}/t{suffix}"
2079
  preview_url = f"/api/i/{pid}/v{suffix}"
 
2335
 
2336
  @app.on_event("startup")
2337
  async def startup():
2338
+ await asyncio.to_thread(cleanup_image_cache_root)
2339
  if not TURSO_DB_URL:
2340
  print("WARN: TURSO_DB_URL not set, skipping DB init")
2341
  return
frontend/app.js CHANGED
@@ -9,6 +9,7 @@ const LONG_DIGITS_RE = /\d{6,}/g
9
  const POST_ID_RE = /\d+/g
10
  const PAGE_SIZE = 60
11
  const SEARCH_PAGE_SIZE = 10
 
12
  let viewerScale = 1
13
  let viewerDrag = null
14
  let viewerPostId = ""
@@ -658,8 +659,8 @@ function openViewer(item) {
658
  function showViewerPage() {
659
  const page = viewerPages[viewerPageIndex]
660
  const pid = encodeURIComponent(viewerPostId)
661
- $("#viewer-img").src = `/api/i/${pid}/v?p=${page}`
662
- $("#viewer-download").href = `/api/i/${pid}/o?p=${page}`
663
  $("#viewer-prev").disabled = viewerPageIndex <= 0
664
  $("#viewer-next").disabled = viewerPageIndex >= viewerPages.length - 1
665
  $("#viewer-prev").classList.toggle("hidden", viewerPages.length <= 1)
 
9
  const POST_ID_RE = /\d+/g
10
  const PAGE_SIZE = 60
11
  const SEARCH_PAGE_SIZE = 10
12
+ const IMAGE_URL_VERSION = 2
13
  let viewerScale = 1
14
  let viewerDrag = null
15
  let viewerPostId = ""
 
659
  function showViewerPage() {
660
  const page = viewerPages[viewerPageIndex]
661
  const pid = encodeURIComponent(viewerPostId)
662
+ $("#viewer-img").src = `/api/i/${pid}/v?p=${page}&v=${IMAGE_URL_VERSION}`
663
+ $("#viewer-download").href = `/api/i/${pid}/o?p=${page}&v=${IMAGE_URL_VERSION}`
664
  $("#viewer-prev").disabled = viewerPageIndex <= 0
665
  $("#viewer-next").disabled = viewerPageIndex >= viewerPages.length - 1
666
  $("#viewer-prev").classList.toggle("hidden", viewerPages.length <= 1)