x / Backblaze_Files_Renamer.py
saliacoel's picture
Upload Backblaze_Files_Renamer.py
7351a7c verified
Raw History Blame Contribute Delete
18.5 kB
r"""ComfyUI node: Backblaze_Files_Renamer.
INSTALL
1. Install into the Python environment used by ComfyUI:
python -m pip install "b2sdk>=2.5,<3"
Windows portable (from the ComfyUI_windows_portable directory):
.\python_embeded\python.exe -m pip install "b2sdk>=2.5,<3"
2. Put this file in ComfyUI/custom_nodes/ and restart ComfyUI.
3. Add Salia > Backblaze > Backblaze_Files_Renamer.
BEHAVIOR
BAM records are NUMBER.Name, optional, ignored, tags,
Only Name is used as the exact, case-sensitive top-level folder name.
Only <Name>/<before_filename> is renamed to <Name>/<after_filename>.
Missing folders/files and existing destinations are skipped and reported.
Every queued execution runs again, even when its inputs are unchanged.
Uses the official Backblaze SDK and server-side copies, including multipart
copies for large files; video data is never downloaded or re-encoded.
The copy is verified before hiding the old filename and deleting the exact
source version. A hide marker prevents older versions from reappearing.
Historical versions are NOT renamed or purged; they remain hidden under
their original name, subject to the bucket's existing lifecycle rules.
The report distinguishes a completed rename with a cleanup warning from
failures before completion. Existing destinations are never intentionally
replaced. B2 rename is not atomic; avoid concurrent writes to these names.
Customer-key-encrypted (SSE-C) files require a separate encryption key and
are reported as errors without changing the source. Object Lock is never
bypassed. Credentials are held in memory by the SDK for this execution.
Official references:
https://www.backblaze.com/apidocs/b2-copy-file
https://www.backblaze.com/apidocs/b2-hide-file
https://www.backblaze.com/apidocs/b2-delete-file-version
https://b2-sdk-python.readthedocs.io/en/stable/api/bucket.html
"""
import re
from collections import Counter
DEFAULT_BAM = """0001.Tira,
0002.Anita,
0003.Antonia,
0004.Applie,
0005.Scarmil,
0006.Blakey,
0007.Chandy, leg.skirtish,
0008.Chaps,
0009.Clea,
0010.Clive,
0011.Corva,
0012.Dimi,
0013.Eileen, leg.skirtish,
0014.Esmeralda, leg.skirtish,
0015.Flix,
0016.Fura,
0017.Yana,
0018.Fuma,
0019.Taya,
0020.Gemma,
0021.Tola,
0022.Gina,
0023.Yoko,
0024.Gloria,
0025.Gia, leg.skirtish,
0026.Grethe, leg.skirtish,
0027.Riff,
0028.Hellia, leg.skirtish,
0029.Hira,
0030.Hixy,
0031.Hana, leg.skirtish,
0032.Helga,
0033.Ivanka, leg.skirtish,
0034.Jasmin, leg.skirtish,
0035.Jenna, leg.skirtish,
0036.Jule,
0037.Justina,
0038.Kunade, leg.skirtish,
0039.Kaya,
0040.Katrine,
0041.Laila,
0042.Hazila, leg.skirtish,
0043.Kassia,
0044.Kasumi,
0045.Karma,
0046.Klaudia, leg.skirtish,
0047.Linsi, leg.skirtish,
0048.Kiki,
0049.Kornelia,
0050.Kumimi,
0051.Mayli,
0052.Kwanda,
0053.Leejin, leg.skirtish,
0054.Lesra, leg.skirtish,
0055.Mathilde, leg.skirtish,
0056.Lorelei, leg.skirtish,
0057.Lotta, leg.skirtish,
0058.Luca,
0059.Mathea,
0060.Macey, leg.skirtish,
0061.Marcelia,
0062.Maggie, leg.skirtish,
0063.Marise,
0064.Melissa, leg.skirtish,
0065.Mesa,
0066.Merel, leg.skirtish,
0067.Melodie, leg.skirtish,
0068.Ksandra,
0069.Mireille,
0070.Minnie, leg.skirtish,
0071.Sacha,
0072.Paola,
0073.Renata,
0074.Silvia, leg.skirtish,
0075.Serena,
0076.Tala,
0077.Valeria, leg.skirtish,
0078.Quin, leg.skirtish,
0079.Ruca,
0080.Robelle, leg.skirtish,
0081.Diane,
0082.Teresa, leg.skirtish,
0083.Sarella,
0084.Serra, leg.skirtish,
0085.Shirei, leg.skirtish,
0086.Sheena,
0087.Sario,
0088.Sidey,
0089.Sylvia, leg.skirtish,
0090.Sonya, leg.skirtish,
0091.Tali,
0092.Tenshi, leg.skirtish,
0093.Tessa, leg.skirtish,
0094.Victorya, leg.skirtish,
0095.Tara,
0096.Tarly, leg.skirtish,
0097.Vasper,
0098.Vanga,
0099.Vicaria,
0100.Ulyeta,
0101.Yanasha, leg.skirtish,
0102.Virginia, leg.skirtish,
0103.Rosabel,
0104.Defna,
0105.Zhenji, leg.skirtish,
0106.Nettie,
0107.Valina,
0108.Alena,
0109.Adali,
0110.Agnes, leg.skirtish,
0111.Arlene,
0112.Asma, leg.skirtish,
0113.Ayox,
0114.Beria, leg.skirtish,
0115.Blix,
0116.Bubble, leg.skirtish,
0117.Cecilia,
0118.Chae, leg.skirtish,
0119.Clesha,
0120.Clovie,
0121.Conli,
0122.Criza,
0123.Daizy, leg.skirtish,
0124.Darta,
0125.Dellie,
0126.Draga,
0127.Astrid,
0128.Ellen,
0129.Atium,
0130.Eshonai, leg.skirtish,
0131.Feizhi, leg.skirtish,
0132.Felya,
0133.Fiella, leg.skirtish,
0134.Fils,
0135.Filsie,
0136.Flidry,
0137.Floret,
0138.Florina, leg.skirtish,
0139.Flynia, leg.skirtish,
0140.Frile, leg.skirtish,
0141.Gabie, leg.skirtish,
0142.Ginie,
0143.Grima,
0144.Hase,
0145.Helen, leg.skirtish,
0146.Helia,
0147.Hirama, leg.skirtish,
0148.Hive,
0149.Idavie, leg.skirtish,
0150.Ilaye, leg.skirtish,
0151.Ildra,
0152.Jacria,
0153.Jeria, leg.skirtish,
0154.Jewel,
0155.Juni,
0156.Kaelie,
0157.Kali, leg.skirtish,
0158.Keris, leg.skirtish,
0159.Kesai,
0160.Kiras,
0161.Klee,
0162.Lania, leg.skirtish,
0163.Lansia,
0164.Larim,
0165.Larin,
0166.Layan, leg.skirtish,
0167.Leaf,
0168.Leejay,
0169.Lenity, leg.skirtish,
0170.Lift,
0171.Lise,
0172.Lite, leg.skirtish,
0173.Lorah,
0174.Lucky,
0175.Mabel, leg.skirtish,
0176.Walrune, leg.skirtish,
0177.Hazel,
0178.Moren, leg.skirtish,
0179.Nadja,
0180.Nalia,
0181.Navanie, leg.skirtish,
0182.Oxana,
0183.Palim, leg.skirtish,
0184.Panari, leg.skirtish,
0185.Hayata,
0186.Piena,
0187.Prava,
0188.Primm, leg.skirtish,
0189.Rangle,
0190.Rayn,
0191.Ream,
0192.Reese,
0193.Refala, leg.skirtish,
0194.Rela, leg.skirtish,
0195.Rita,
0196.Riva,
0197.Sata,
0198.Scilla, leg.skirtish,
0199.Shallan,
0200.Shavira,
0201.Shika, leg.skirtish,
0202.Eizia, leg.skirtish,
0203.Sliver,
0204.Stara,
0205.Stern, leg.skirtish,
0206.Stria,
0207.Telsin,
0208.Tetra,
0209.Thevaria,
0210.Thyme,
0211.Valenta,
0212.Vanira,
0213.Vanissa,
0214.Variena,
0215.Venli,
0216.Vivenna,
0217.Elvani, leg.skirtish,
0218.Melahan, leg.skirtish,
0219.Mina, leg.skirtish,
0220.Peidra, leg.skirtish,
0221.Amelia, leg.skirtish, crotch.pantyhose,
0222.Sarke,
0223.Nagatora, leg.skirtish,
0224.Maryn, leg.skirtish, crotch.pantyhose,
0225.Areda,
0226.Ghislaine,
0227.Piper, leg.skirtish, crotch.pantyhose,
0228.Topline, leg.skirtish,
0229.Lavinia, leg.skirtish,
0230.Pernel, leg.skirtish,
0231.Semra, leg.skirtish,
0232.Myra, leg.skirtish, crotch.pantyhose,
0233.Pencil,
9998.Mannequin,
9999.JoAhn, leg.skirtish,"""
def _validate_component(value, label):
value = str(value).strip()
if not value or value in (".", ".."):
raise ValueError("{} must not be empty, '.' or '..'.".format(label))
if "/" in value or "\\" in value or any(ord(c) < 32 or ord(c) == 127 for c in value):
raise ValueError("{} must be a single name without slashes or control characters.".format(label))
return value
def parse_bam(text):
"""Return unique names in BAM order and notes about duplicate records.
Accepts newline-separated or comma-separated NUMBER.Name records. Tokens
after the first comma, such as leg.skirtish, are ignored unless they start
a new numbered record. Conflicting IDs abort before any remote changes.
"""
pattern = r"(?:^|[,\r\n])[ \t]*(\d+)[ \t]*\.[ \t]*([^,\r\n]+)"
names, notes, by_id, seen_names = [], [], {}, set()
for match in re.finditer(pattern, str(text).lstrip("\ufeff")):
number = int(match.group(1))
name = _validate_component(match.group(2), "Character name")
if number in by_id and by_id[number] != name:
raise ValueError("BAM ID {} has conflicting names: {!r} and {!r}.".format(number, by_id[number], name))
by_id[number] = name
if name in seen_names:
notes.append("Duplicate character ignored: {}".format(name))
continue
seen_names.add(name)
names.append(name)
if not names:
raise ValueError("BAM contains no valid records. Use entries like 0109.Adali, leg.skirtish,")
return names, notes
def _current_file(bucket, name):
"""Return the latest visible exact-name version, respecting hide markers."""
for version in bucket.list_file_versions(name, fetch_count=1):
if version.file_name != name:
return None
if version.action in ("upload", "copy"):
return version
if version.action == "hide":
return None
if version.action != "start":
raise RuntimeError("Unexpected Backblaze file action: {}".format(version.action))
return None
def _folder_exists(bucket, name):
# B2 folders are prefixes; a marker or one visible child is enough.
return next(bucket.ls(name + "/", latest_only=True, recursive=True, fetch_count=1), None) is not None
def _sha1(version):
digest = str(version.content_sha1 or "").removeprefix("unverified:")
if not re.fullmatch(r"[0-9a-fA-F]{40}", digest):
digest = str((version.file_info or {}).get("large_file_sha1", ""))
return digest.lower() if re.fullmatch(r"[0-9a-fA-F]{40}", digest) else None
def _verify_copy(source, copied, destination_name):
if copied.file_name != destination_name or copied.size != source.size:
raise RuntimeError("Destination filename or byte size does not match the source.")
source_sha1, copied_sha1 = _sha1(source), _sha1(copied)
if source_sha1 and copied_sha1 and source_sha1 != copied_sha1:
raise RuntimeError("Destination SHA-1 does not match the source.")
if source.content_type != copied.content_type:
raise RuntimeError("Destination content type does not match the source.")
for key, value in (source.file_info or {}).items():
if (copied.file_info or {}).get(key) != value:
raise RuntimeError("Destination metadata does not match the source: {}".format(key))
def _rename_one(bucket, source, destination_name):
"""Return an optional cleanup warning; raise on an incomplete rename."""
encryption = getattr(source, "server_side_encryption", None)
mode = getattr(getattr(encryption, "mode", None), "value", None)
if mode == "SSE-C":
raise RuntimeError("SSE-C encryption requires a customer encryption key; source unchanged.")
options = {
# Explicit length enables the SDK's multipart path for >5 GB files.
"length": source.size if source.size else None,
"content_type": source.content_type,
"file_info": dict(source.file_info or {}),
}
if mode == "SSE-B2":
options["destination_encryption"] = encryption
retention = getattr(source, "file_retention", None)
if getattr(getattr(retention, "mode", None), "value", None) in ("governance", "compliance"):
options["file_retention"] = retention
hold = getattr(source, "legal_hold", None)
if getattr(hold, "value", None) in ("on", "off"):
options["legal_hold"] = hold
try:
copied = bucket.copy(source.id_, destination_name, **options)
except Exception as exc:
raise RuntimeError("Copy failed; source version retained. A destination copy may exist if the response was lost. {}".format(exc)) from exc
try:
confirmed = bucket.get_file_info_by_id(copied.id_)
if confirmed.id_ != copied.id_:
raise RuntimeError("Destination version ID mismatch.")
_verify_copy(source, confirmed, destination_name)
current_destination = _current_file(bucket, destination_name)
if current_destination is None or current_destination.id_ != copied.id_:
raise RuntimeError("Destination changed during copying.")
current_source = _current_file(bucket, source.file_name)
if current_source is None or current_source.id_ != source.id_:
raise RuntimeError("Source changed during copying.")
except Exception as exc:
raise RuntimeError("Copy could not be verified; source version retained. Inspect destination {} (version {}). {}".format(destination_name, copied.id_, exc)) from exc
try:
# Hide BEFORE deleting: otherwise an older source version may resurface.
bucket.hide_file(source.file_name)
if _current_file(bucket, source.file_name) is not None:
raise RuntimeError("Source filename is still visible; it may have been changed concurrently.")
except Exception as exc:
raise RuntimeError("Destination verified, but hiding the old filename could not be confirmed; source version retained. {}".format(exc)) from exc
try:
bucket.delete_file_version(source.id_, source.file_name)
except Exception as exc:
return "Old filename hidden, but deletion of source version {} was not confirmed: {}".format(source.id_, exc)
return ""
def _open_bucket(key_id, key, bucket_name):
# Lazy import keeps this node discoverable even before installing b2sdk.
try:
from b2sdk.v2 import B2Api, InMemoryAccountInfo
except ImportError as exc:
raise RuntimeError('Missing dependency. In ComfyUI\'s Python environment run: python -m pip install "b2sdk>=2.5,<3"') from exc
api = B2Api(InMemoryAccountInfo())
api.authorize_account("production", key_id, key)
return api.get_bucket_by_name(bucket_name)
def _check_interrupted():
try:
import comfy.model_management as model_management
except ImportError:
return
model_management.throw_exception_if_processing_interrupted()
def _progress_bar(total):
try:
from comfy.utils import ProgressBar
except ImportError:
return None
return ProgressBar(total)
class Backblaze_Files_Renamer:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"backblaze_master_key": ("STRING", {"default": "", "dynamicPrompts": False}),
"backblaze_master_key_ID": ("STRING", {"default": "", "dynamicPrompts": False}),
"BAM": ("STRING", {"default": DEFAULT_BAM, "multiline": True, "dynamicPrompts": False}),
"bucket_name": ("STRING", {"default": "FemuChars", "dynamicPrompts": False}),
"before_filename": ("STRING", {"default": "Front_To_Reartaunt_00000.mp4", "dynamicPrompts": False}),
"after_filename": ("STRING", {"default": "Front_To_Reartaunt.mp4", "dynamicPrompts": False}),
}}
RETURN_TYPES = ("STRING", "INT", "INT", "INT")
RETURN_NAMES = ("status_report", "renamed_count", "skipped_count", "error_count")
FUNCTION = "rename_files"
CATEGORY = "Salia/Backblaze"
OUTPUT_NODE = True
DESCRIPTION = "Rename one exact filename across character folders parsed from BAM. Missing files/folders and existing destinations are skipped."
@classmethod
def IS_CHANGED(cls, **kwargs):
# Remote state may change even when widget values are unchanged.
return float("nan")
def rename_files(self, backblaze_master_key, backblaze_master_key_ID,
BAM, bucket_name, before_filename, after_filename):
key = str(backblaze_master_key).strip()
key_id = str(backblaze_master_key_ID).strip()
def safe_error(exc):
message = str(exc)
for secret in sorted((key, key_id), key=len, reverse=True):
if secret:
message = message.replace(secret, "[REDACTED]")
return message.replace("\r", " ").replace("\n", " ")
def result(report, renamed=0, skipped=0, errors=0):
print("[Backblaze_Files_Renamer]\n" + report, flush=True)
return {"ui": {"text": [report]}, "result": (report, renamed, skipped, errors)}
try:
names, notes = parse_bam(BAM)
bucket_name = _validate_component(bucket_name, "bucket_name")
before = _validate_component(before_filename, "before_filename")
after = _validate_component(after_filename, "after_filename")
if before == after:
return result("Nothing renamed: before_filename and after_filename are identical.", skipped=len(names))
if not key or not key_id:
raise ValueError("Enter both the Backblaze master key and master key ID.")
bucket = _open_bucket(key_id, key, bucket_name)
except Exception as exc:
return result("ERROR: " + safe_error(exc), errors=1)
counts = Counter()
details = ["BAM NOTE: " + note for note in notes]
progress = _progress_bar(len(names))
for name in names:
_check_interrupted()
old_name, new_name = name + "/" + before, name + "/" + after
try:
source = _current_file(bucket, old_name)
if source is None:
if _folder_exists(bucket, name):
tag, detail = "MISSING_FILE", old_name + " (no visible source file)"
else:
tag, detail = "MISSING_FOLDER", name + "/ (no visible files or folder marker)"
counts["skipped"] += 1
elif _current_file(bucket, new_name) is not None:
tag, detail = "DESTINATION_EXISTS", new_name + " (skipped; source unchanged)"
counts["skipped"] += 1
else:
warning = _rename_one(bucket, source, new_name)
counts["renamed"] += 1
tag, detail = "RENAMED", old_name + " -> " + new_name
if warning:
counts["warnings"] += 1
tag = "RENAMED_WITH_WARNING"
detail += " | " + safe_error(warning)
except Exception as exc:
counts["errors"] += 1
tag, detail = "ERROR", old_name + " | " + safe_error(exc)
line = "[{}] {}".format(tag, detail)
details.append(line)
print("[Backblaze_Files_Renamer] " + line, flush=True)
if progress is not None:
progress.update(1)
summary = (
"Backblaze_Files_Renamer\n"
"Bucket: {}\nBefore: {}\nAfter: {}\n"
"Characters: {} | Renamed: {} | Skipped: {} | Errors: {} | Cleanup warnings: {}\n"
).format(bucket_name, before, after, len(names), counts["renamed"], counts["skipped"], counts["errors"], counts["warnings"])
report = summary + "\n" + "\n".join(details)
return result(report, counts["renamed"], counts["skipped"], counts["errors"])
NODE_CLASS_MAPPINGS = {"Backblaze_Files_Renamer": Backblaze_Files_Renamer}
NODE_DISPLAY_NAME_MAPPINGS = {"Backblaze_Files_Renamer": "Backblaze_Files_Renamer"}