diff --git a/app/api/session.py b/app/api/session.py
index a5f765d..1e6d796 100644
--- a/app/api/session.py
+++ b/app/api/session.py
@@ -24,6 +24,10 @@ class SelectedFilesReorderRequest(BaseModel):
to_index: int = Field(ge=0)
+class SessionSettingsRequest(BaseModel):
+ set_file_date_to_first_aired_date: bool
+
+
def _normalize_session_id(session_id: str) -> str:
normalized = session_id.strip()
if not normalized:
@@ -139,6 +143,22 @@ def reorder_selected_files(
return {"session_id": normalized_session_id, "items": items}
+@router.get("/settings")
+def get_session_settings():
+ service = SessionService()
+ return {"settings": service.get_settings()}
+
+
+@router.put("/settings")
+def put_session_settings(payload: SessionSettingsRequest):
+ service = SessionService()
+ try:
+ settings = service.update_settings(payload.model_dump())
+ except ValueError as exc:
+ raise HTTPException(status_code=400, detail=str(exc))
+ return {"settings": settings}
+
+
@router.get("/mapping-preview")
def get_mapping_preview(session_id: str = Query("default", min_length=1)):
service = SessionService()
diff --git a/app/services/session_service.py b/app/services/session_service.py
index 6fe3416..5975cb3 100644
--- a/app/services/session_service.py
+++ b/app/services/session_service.py
@@ -10,6 +10,8 @@ from app.config import APP_DATA_DIR
class SessionService:
+ FILE_DATE_SETTING_KEY = "set_file_date_to_first_aired_date"
+
def __init__(self) -> None:
self._db_path = Path(APP_DATA_DIR) / "session_state.sqlite3"
self._db_path.parent.mkdir(parents=True, exist_ok=True)
@@ -110,6 +112,53 @@ class SessionService:
ON rename_run_items(run_id, item_index)
"""
)
+ conn.execute(
+ """
+ CREATE TABLE IF NOT EXISTS app_settings (
+ key TEXT PRIMARY KEY,
+ value TEXT NOT NULL,
+ updated_at TEXT NOT NULL
+ )
+ """
+ )
+
+ def get_settings(self) -> dict:
+ with self._connect() as conn:
+ rows = conn.execute(
+ """
+ SELECT key, value
+ FROM app_settings
+ """
+ ).fetchall()
+
+ values = {str(row["key"]): str(row["value"]) for row in rows}
+ return {
+ self.FILE_DATE_SETTING_KEY: values.get(self.FILE_DATE_SETTING_KEY, "0") == "1",
+ }
+
+ def update_settings(self, settings: dict) -> dict:
+ if self.FILE_DATE_SETTING_KEY not in settings:
+ raise ValueError(f"missing required setting: {self.FILE_DATE_SETTING_KEY}")
+ setting_value = settings[self.FILE_DATE_SETTING_KEY]
+ if not isinstance(setting_value, bool):
+ raise ValueError(f"{self.FILE_DATE_SETTING_KEY} must be boolean")
+
+ stored_value = "1" if setting_value else "0"
+ updated_at = datetime.now(timezone.utc).isoformat()
+
+ with self._connect() as conn:
+ conn.execute(
+ """
+ INSERT INTO app_settings (key, value, updated_at)
+ VALUES (?, ?, ?)
+ ON CONFLICT(key) DO UPDATE SET
+ value = excluded.value,
+ updated_at = excluded.updated_at
+ """,
+ (self.FILE_DATE_SETTING_KEY, stored_value, updated_at),
+ )
+
+ return self.get_settings()
def list_selected_episodes(self, session_id: str) -> list[dict]:
with self._connect() as conn:
@@ -467,9 +516,15 @@ class SessionService:
started_at = time.perf_counter()
preview = self.build_filename_preview(session_id)
+ settings = self.get_settings()
+ set_file_date_to_first_aired = bool(settings.get(self.FILE_DATE_SETTING_KEY, False))
allowed_roots = self._allowed_media_roots()
preflight_items = []
preflight_errors = 0
+ aired_by_index = {}
+
+ for preview_item in preview["items"]:
+ aired_by_index[int(preview_item["index"])] = preview_item["episode"].get("aired")
for item in preview["items"]:
source_path_str = str(item["file"].get("path") or "").strip()
@@ -499,6 +554,8 @@ class SessionService:
"proposed_filename": proposed_filename,
"status": status,
"errors": errors,
+ "file_date_status": "file_date_skipped",
+ "file_date_detail": "rename not executed due to preflight failure",
}
)
@@ -523,11 +580,19 @@ class SessionService:
source_path = Path(item["source_path"])
destination_path = Path(item["destination_path"])
os.replace(str(source_path), str(destination_path))
+
+ file_date_status, file_date_detail = self._apply_file_date_after_rename(
+ enabled=set_file_date_to_first_aired,
+ aired_value=aired_by_index.get(int(item["index"])),
+ destination_path=destination_path,
+ )
results.append(
{
**item,
"status": "renamed",
"errors": [],
+ "file_date_status": file_date_status,
+ "file_date_detail": file_date_detail,
}
)
@@ -546,6 +611,49 @@ class SessionService:
)
return result
+ def _apply_file_date_after_rename(
+ self,
+ enabled: bool,
+ aired_value,
+ destination_path: Path,
+ ) -> tuple[str, str]:
+ if not enabled:
+ return ("file_date_skipped", "setting disabled")
+
+ ts = self._aired_to_local_noon_timestamp(aired_value)
+ if ts is None:
+ return ("file_date_skipped", "aired date missing or invalid")
+
+ try:
+ os.utime(destination_path, (ts, ts))
+ return ("file_date_updated", "mtime+atime set to aired date at 12:00 local time")
+ except Exception as exc:
+ return ("file_date_error", str(exc))
+
+ def _aired_to_local_noon_timestamp(self, aired_value) -> float | None:
+ if aired_value is None:
+ return None
+
+ text = str(aired_value).strip()
+ if not text:
+ return None
+ date_text = text[:10]
+
+ try:
+ date_part = datetime.strptime(date_text, "%Y-%m-%d")
+ except ValueError:
+ return None
+
+ local_noon = datetime(
+ date_part.year,
+ date_part.month,
+ date_part.day,
+ 12,
+ 0,
+ 0,
+ )
+ return local_noon.timestamp()
+
def _log_rename_run(self, session_id: str, result: dict, duration_ms: int) -> None:
created_at = datetime.now(timezone.utc).isoformat()
counts = result.get("counts", {})
diff --git a/app/static/app.js b/app/static/app.js
index 909a240..933eac1 100644
--- a/app/static/app.js
+++ b/app/static/app.js
@@ -14,6 +14,9 @@
modalFiles: [],
modalSelectedFilePaths: new Set(),
syncScrolling: false,
+ settings: {
+ set_file_date_to_first_aired_date: false,
+ },
};
const el = {
@@ -30,6 +33,11 @@
seriesStatus: document.getElementById("seriesStatus"),
seriesOverview: document.getElementById("seriesOverview"),
seriesTvdbLink: document.getElementById("seriesTvdbLink"),
+ settingsBtn: document.getElementById("settingsBtn"),
+ settingsModal: document.getElementById("settingsModal"),
+ closeSettingsModalBtn: document.getElementById("closeSettingsModalBtn"),
+ saveSettingsBtn: document.getElementById("saveSettingsBtn"),
+ setFileDateToFirstAiredDateInput: document.getElementById("setFileDateToFirstAiredDateInput"),
refreshEpisodesBtn: document.getElementById("refreshEpisodesBtn"),
episodesList: document.getElementById("episodesList"),
episodeMeta: document.getElementById("episodeMeta"),
@@ -199,6 +207,42 @@
el.fileMeta.innerHTML = `Rows: ${fileCount} ${mismatchText}`;
}
+ function applySettingsToForm() {
+ el.setFileDateToFirstAiredDateInput.checked = !!state.settings.set_file_date_to_first_aired_date;
+ }
+
+ function openSettingsModal() {
+ applySettingsToForm();
+ el.settingsModal.classList.remove("hidden");
+ el.settingsModal.setAttribute("aria-hidden", "false");
+ }
+
+ function closeSettingsModal() {
+ el.settingsModal.classList.add("hidden");
+ el.settingsModal.setAttribute("aria-hidden", "true");
+ }
+
+ async function loadSettings() {
+ const data = await api("/api/session/settings");
+ state.settings = data.settings || { set_file_date_to_first_aired_date: false };
+ applySettingsToForm();
+ }
+
+ async function saveSettings() {
+ const payload = {
+ set_file_date_to_first_aired_date: !!el.setFileDateToFirstAiredDateInput.checked,
+ };
+ const data = await api("/api/session/settings", {
+ method: "PUT",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify(payload),
+ });
+ state.settings = data.settings || payload;
+ applySettingsToForm();
+ closeSettingsModal();
+ out("Settings saved", data);
+ }
+
function selectPair(index) {
state.selectedPairIndex = index;
renderSelectedEpisodes();
@@ -557,6 +601,12 @@
function bindEvents() {
el.searchBtn.addEventListener("click", () => withHandler(doSearch, el.searchBtn));
+ el.settingsBtn.addEventListener("click", openSettingsModal);
+ el.closeSettingsModalBtn.addEventListener("click", closeSettingsModal);
+ el.saveSettingsBtn.addEventListener("click", () => withHandler(saveSettings, el.saveSettingsBtn));
+ el.settingsModal.addEventListener("click", (e) => {
+ if (e.target === el.settingsModal) closeSettingsModal();
+ });
el.refreshEpisodesBtn.addEventListener("click", () => withHandler(loadEpisodes, el.refreshEpisodesBtn));
el.refreshSelectedEpisodesBtn.addEventListener("click", () => withHandler(loadSelectedEpisodes, el.refreshSelectedEpisodesBtn));
@@ -611,6 +661,7 @@
el.modalRecursiveInput.checked = true;
renderSelectedSeriesDetails();
bindEvents();
+ await loadSettings();
await loadSelectedEpisodes();
await loadSelectedFiles();
await loadRoots();
diff --git a/app/static/styles.css b/app/static/styles.css
index da3ece0..849ba63 100644
--- a/app/static/styles.css
+++ b/app/static/styles.css
@@ -63,6 +63,11 @@ body {
overflow: hidden;
}
+#panelSearch .panel-body {
+ flex: 1;
+ min-height: 0;
+}
+
.panel h2 {
margin: 0 0 10px;
font-size: 16px;
@@ -223,6 +228,10 @@ button.secondary {
min-height: 40px;
}
+.panel-search-footer {
+ justify-content: flex-end;
+}
+
.mismatch {
color: #b91c1c;
font-weight: 700;
@@ -265,6 +274,29 @@ button.secondary {
max-height: 280px;
}
+.settings-card {
+ width: min(520px, 94vw);
+}
+
+.settings-section h4 {
+ margin: 0 0 8px;
+ font-size: 14px;
+ color: #1e293b;
+}
+
+.settings-check {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ font-size: 13px;
+ color: #334155;
+}
+
+.settings-actions {
+ justify-content: flex-end;
+ margin-top: 12px;
+}
+
#panelSelectedEpisodes .panel-footer button:first-child,
#panelSelectedEpisodes .panel-footer button:last-child,
#panelSelectedFiles .panel-footer button:first-child,
diff --git a/app/templates/index.html b/app/templates/index.html
index fd99bbf..86d5788 100644
--- a/app/templates/index.html
+++ b/app/templates/index.html
@@ -15,23 +15,28 @@
1. TV Shows
-
+
+
+
+
Settings
+
+
+
+
+
+
+
+
+