feat: add timestamp to output filenames for cache-busting

Add get_timestamp() and format_output_filename() utilities to folder_paths.py
that generate unique filenames with UTC timestamps. This eliminates the need
for client-side cache-busting query parameters.

New filename format: prefix_00001_20260131-220945-123456_.ext

Updated all save nodes to use the new format:
- nodes.py (SaveImage, SaveLatent, SaveImageWebsocket)
- comfy_api/latest/_ui.py (UILatent)
- comfy_extras/nodes_video.py (SaveWEBM, SaveAnimatedPNG, SaveAnimatedWEBP)
- comfy_extras/nodes_images.py (SaveSVG)
- comfy_extras/nodes_hunyuan3d.py (Save3D)
- comfy_extras/nodes_model_merging.py (SaveCheckpointSimple)
- comfy_extras/nodes_lora_extract.py (LoraSave)
- comfy_extras/nodes_train.py (SaveEmbedding)

Amp-Thread-ID: https://ampcode.com/threads/T-019c17e5-1c0a-736f-970d-e411aae222fc
This commit is contained in:
bymyself
2026-01-31 22:30:57 -08:00
parent 873de5f37a
commit 0f259cabdd
10 changed files with 163 additions and 20 deletions

View File

@@ -4,6 +4,7 @@ import os
import time
import mimetypes
import logging
from datetime import datetime, timezone
from typing import Literal, List
from collections.abc import Collection
@@ -11,6 +12,46 @@ from comfy.cli_args import args
supported_pt_extensions: set[str] = {'.ckpt', '.pt', '.pt2', '.bin', '.pth', '.safetensors', '.pkl', '.sft'}
def get_timestamp() -> str:
"""Generate a filesystem-safe timestamp string for output filenames.
Returns a UTC timestamp in the format YYYYMMDD-HHMMSS-ffffff (microseconds)
which is human-readable, lexicographically sortable, and Windows-safe.
"""
now = datetime.now(timezone.utc)
return now.strftime("%Y%m%d-%H%M%S-%f")
def format_output_filename(
filename: str,
counter: int,
ext: str,
*,
batch_num: str | None = None,
timestamp: str | None = None,
) -> str:
"""Format an output filename with counter and timestamp for cache-busting.
Args:
filename: The base filename prefix
counter: The numeric counter for uniqueness
ext: The file extension (with or without leading dot)
batch_num: Optional batch number to replace %batch_num% placeholder
timestamp: Optional timestamp string (defaults to current UTC time)
Returns:
Formatted filename like: filename_00001_20260131-123456-789012_.ext
"""
ext = ext.lstrip(".")
if timestamp is None:
timestamp = get_timestamp()
if batch_num is not None:
filename = filename.replace("%batch_num%", batch_num)
return f"{filename}_{counter:05}_{timestamp}_.{ext}"
folder_names_and_paths: dict[str, tuple[list[str], set[str]]] = {}
# --base-directory - Resets all default paths configured in folder_paths with a new base path