T-566 封面历史归档数据层
This commit is contained in:
+39
-1
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
import re
|
||||
|
||||
from .config import make_slug
|
||||
|
||||
@@ -22,6 +23,27 @@ def task_image_path(image_root, task, account=None, suffix="old", ext=".jpg"):
|
||||
)
|
||||
|
||||
|
||||
def list_task_cover_candidates(image_root, task, account=None):
|
||||
"""Return generated cover candidates for one task from the image directory."""
|
||||
|
||||
canonical_path = task_image_path(image_root, task, account=account, suffix="new", ext=".jpg")
|
||||
directory = os.path.dirname(canonical_path)
|
||||
prefix = os.path.splitext(os.path.basename(canonical_path))[0]
|
||||
if not os.path.isdir(directory):
|
||||
return []
|
||||
candidates = []
|
||||
for filename in os.listdir(directory):
|
||||
full_path = os.path.join(directory, filename)
|
||||
if not os.path.isfile(full_path):
|
||||
continue
|
||||
stem, ext = os.path.splitext(filename)
|
||||
if ext.lower() != ".jpg":
|
||||
continue
|
||||
if stem == prefix or stem.startswith(prefix + "_"):
|
||||
candidates.append(os.path.abspath(full_path))
|
||||
return sorted(candidates, key=lambda path: _cover_candidate_sort_key(path, prefix))
|
||||
|
||||
|
||||
def _account_slug(account, task):
|
||||
slug = _get(account, "slug")
|
||||
if slug:
|
||||
@@ -32,6 +54,22 @@ def _account_slug(account, task):
|
||||
return "unknown_account"
|
||||
|
||||
|
||||
def _cover_candidate_sort_key(path, prefix):
|
||||
filename = os.path.basename(path)
|
||||
stem, _ext = os.path.splitext(filename)
|
||||
if stem == prefix:
|
||||
return (0, 0, 0, filename)
|
||||
marker = prefix + "_"
|
||||
if stem.startswith(marker):
|
||||
suffix = stem[len(marker):]
|
||||
match = re.fullmatch(r"(\d{14})(?:_(\d+))?", suffix)
|
||||
if match:
|
||||
timestamp = int(match.group(1))
|
||||
collision = int(match.group(2) or 1)
|
||||
return (1, -timestamp, -collision, filename)
|
||||
return (2, 0, 0, filename)
|
||||
|
||||
|
||||
def _get(obj, name, default=None):
|
||||
if obj is None:
|
||||
return default
|
||||
@@ -45,4 +83,4 @@ def _safe_component(value, default):
|
||||
if not text:
|
||||
text = str(default)
|
||||
safe = "".join(ch if ch.isalnum() or ch in "_-" else "_" for ch in text).strip("_")
|
||||
return safe or str(default)
|
||||
return safe or str(default)
|
||||
|
||||
Reference in New Issue
Block a user