File size: 2,068 Bytes
1c8d1ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from __future__ import annotations

import re
import shutil
import zipfile
from pathlib import Path

from src.config import FAVORITES_DIR


def ensure_dir(path: Path) -> None:
    Path(path).mkdir(parents=True, exist_ok=True)


def safe_filename(name: str) -> str:
    cleaned = re.sub(r"[^A-Za-z0-9._-]+", "_", name.strip())
    cleaned = cleaned.strip("._")
    return cleaned or "file"


def _unique_destination(directory: Path, filename: str) -> Path:
    stem = Path(filename).stem
    suffix = Path(filename).suffix
    candidate = directory / filename
    index = 2
    while candidate.exists():
        candidate = directory / f"{stem}_{index}{suffix}"
        index += 1
    return candidate


def copy_to_favorites(image_path: str | Path) -> Path:
    source = Path(image_path)
    if not source.exists():
        raise FileNotFoundError(f"Image file does not exist: {source}")
    ensure_dir(FAVORITES_DIR)
    filename = safe_filename(source.name)
    destination = _unique_destination(FAVORITES_DIR, filename)
    shutil.copy2(source, destination)
    return destination


def export_favorites_to_zip(favorite_paths: list[str | Path], zip_path: Path) -> Path:
    zip_path = Path(zip_path)
    ensure_dir(zip_path.parent)

    valid_paths = [
        Path(path)
        for path in favorite_paths
        if path and Path(path).exists() and Path(path).resolve() != zip_path.resolve()
    ]
    if not valid_paths:
        raise ValueError("No favorite image files are available to export.")

    used_names: dict[str, int] = {}
    with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as archive:
        for path in valid_paths:
            archive_name = safe_filename(path.name)
            count = used_names.get(archive_name, 0)
            used_names[archive_name] = count + 1
            if count:
                stem = Path(archive_name).stem
                suffix = Path(archive_name).suffix
                archive_name = f"{stem}_{count + 1}{suffix}"
            archive.write(path, arcname=archive_name)
    return zip_path