feat(wallpaper_changer):add image hash, use conf_dir, scan dir parameter, move to src

add FileInfo class to store hash
use conf_dir instead of ~/Wallpaper
scan: opt scan dir as parameter
encode directly in confdir/New
skip adding images with same hash
add images with same name, change number only
add decode_image function to chack hash after decoding
This commit is contained in:
Lucas Vivo committed 2026-11-13 10:57:39 +01:00
1 parent 8aa679b405
commit 7f729330c7
1 file changed
+105 -73
@@ -1,3 +1,4 @@
import hashlib
import logging
import re
import shutil
@@ -6,11 +7,12 @@ from pathlib import Path
from PIL import Image, ImageOps
from wallpaper_changer.encryption_manager import EncryptionManager
from wallpaper_changer.encryption_manager import EncryptionManager, FileInfo
from wallpaper_changer.logging_utils import get_logger
IMAGE_EXTENSIONS = frozenset(Image.registered_extensions())
logger = get_logger(__name__)
@dataclass
class WallpaperImage:
@@ -19,25 +21,26 @@ class WallpaperImage:
height: int
aspect: float
class ImageRepository:
def __init__(self, root: str | Path):
self.img_folder_path = Path(root)
self.logger = get_logger(__name__)
logger.info(f"Set {self.img_folder_path} as root dir")
self.image_list = []
def scan(self, private_key: str | Path, disable_encryption: bool = False):
def scan(self, private_key: str | Path, img_dir: Path | None = None,
disable_encryption: bool = False):
out = []
result: dict[str, str] = {}
result: dict[str,dict[str, str]] = {}
img_scan_path = self.img_folder_path if img_dir is None else img_dir
if not disable_encryption:
encryption_manager = EncryptionManager()
result = {
encrypted_name: original_name
for original_name, encrypted_name in encryption_manager.load_naming_table(
self.img_folder_path / "naming_table.enc", private_key
item.encrypted_file: {"name": item.image_name, "hash": item.image_hash}
for item in encryption_manager.load_naming_table(
img_scan_path / "naming_table.enc", private_key
)
}
for file in self.img_folder_path.rglob("*"):
for file in img_scan_path.rglob("*"):
if not file.is_file():
continue
if file.suffix.lower() not in IMAGE_EXTENSIONS | {".jpg_enc"}:
@@ -49,14 +52,16 @@ class ImageRepository:
):
continue
self.logger.trace(f"Found image {file}")
logger.trace(f"Found image {file}")
out.append(
{
"file": file,
"name": result.get(file.name, file.name),
"name": result[file.name].get("name") if result.get(file.name) else file.name,
"hash": result[file.name].get("hash") if result.get(file.name) else None,
}
)
self.image_list = out
logger.info(f"Scanned {len(out)} images in {img_scan_path}")
return out
def convert_to_jpg(self, image_folder: str | Path | None = None) -> list[Path]:
@@ -68,28 +73,22 @@ class ImageRepository:
converted_path = self._convert_to_jpg(image_path)
if converted_path is not None:
converted_paths.append(converted_path)
if len(converted_paths)>0:
logger.info(f"Converted {len(converted_paths)} images to JPG")
return converted_paths
def check_encryption(
self, new_images_folder: Path, private_key: str | Path, replace: bool = False
) -> list[Path]:
if not new_images_folder.is_dir():
self.logger.warning(f"New folder does not exist: {new_images_folder}")
logger.warning(f"New folder does not exist: {new_images_folder}")
return []
new_images = list(new_images_folder.glob("*"))
new_images = list(sorted(new_images_folder.glob("*")))
if not new_images:
self.logger.trace(f"No new images found in {new_images_folder}")
logger.trace(f"No new images found in {new_images_folder}")
return []
self.convert_to_jpg(new_images_folder)
for image_path in sorted(new_images_folder.rglob("*.jpg")):
if not image_path.is_file():
continue
if image_path.parent == self.img_folder_path:
continue
destination = self._available_path(self.img_folder_path / image_path.name)
shutil.move(str(image_path), destination)
encryption_manager = EncryptionManager()
naming_table_path = self.img_folder_path / "naming_table.enc"
naming_table = encryption_manager.load_naming_table(
@@ -104,46 +103,59 @@ class ImageRepository:
encrypted_images = []
plaintext_images = []
for image_path in sorted(self.img_folder_path.rglob("*")):
for image_path in new_images:
if (
not image_path.is_file()
or image_path.suffix.lower() not in IMAGE_EXTENSIONS
):
continue
existing_entry = next(
(entry for entry in naming_table if entry[0] == image_path.name),
None,
)
file_hash = hashlib.sha256(image_path.read_bytes()).hexdigest()
skip = False
existing_entry = None
for entry in naming_table:
if entry.image_hash == file_hash and not replace:
logger.warning(f"Skipping already encrypted image: {image_path.name}")
logger.warning(f"Entry: {entry.image_name}:{entry.encrypted_file}")
skip = True
break
elif entry.image_name == image_path.name or entry.image_hash == file_hash:
existing_entry = entry
break
if skip:
continue
if existing_entry and replace:
image_name = existing_entry[1]
encrypted_file_name = existing_entry.encrypted_file
identifier = image_path.name
else:
identifier = image_path.name
if existing_entry:
identifier = self._unique_identifier(identifier, naming_table)
image_name = f"{next_number:05}.jpg_enc"
encrypted_file_name = f"{next_number:05}.jpg_enc"
next_number += 1
image_name = self._available_path(
self.img_folder_path / image_name
).name
encrypted_file_name = self._available_path(
self.img_folder_path / encrypted_file_name
).name
encrypted_path = self.img_folder_path / encrypted_file_name
encrypted_path = self.img_folder_path / image_name
self.logger.trace(f"Encrypting {image_path} to {encrypted_path}")
message = "Encrypting new Image " if not existing_entry else "Replacing existing Image "
logger.info(f"{message} {image_path} to {encrypted_path}:{identifier}")
encryption_manager.encrypt(image_path, encrypted_path, private_key)
encrypted_images.append(encrypted_path)
plaintext_images.append(image_path)
if not existing_entry or not replace:
naming_table.append((identifier, image_name))
naming_table.append(FileInfo(identifier, encrypted_file_name, file_hash))
elif replace:
existing_entry.image_hash = file_hash
existing_entry.image_name = identifier
encryption_manager.store_naming_table(
naming_table, naming_table_path, private_key
)
for image_path in plaintext_images:
image_path.unlink()
self.logger.info("Check encryption done")
logger.info("Check encryption done")
return encrypted_images
def check_consecutive_filenames(
@@ -175,7 +187,7 @@ class ImageRepository:
continue
is_consecutive = False
self.logger.debug(
logger.debug(
f"Non-consecutive JPG filenames in {folder} for prefix "
f"{prefix!r}: found {numbers}"
)
@@ -189,14 +201,14 @@ class ImageRepository:
renamed_files.append((path, path.with_name(new_name)))
if not correct or is_consecutive:
self.logger.debug(
logger.debug(
f"Numbered JPG images are consecutive: {is_consecutive}"
)
return is_consecutive
staged_files = []
for old_path, new_path in renamed_files:
self.logger.debug(f"Renaming {old_path} to {new_path}")
logger.debug(f"Renaming {old_path} to {new_path}")
temporary_path = self._available_path(
old_path.with_name(f".{old_path.name}.renumbering.jpg")
)
@@ -206,7 +218,7 @@ class ImageRepository:
for temporary_path, new_path in staged_files:
temporary_path.rename(new_path)
self.logger.info("Corrected gaps in JPG filename sequences")
logger.info("Corrected gaps in JPG filename sequences")
return True
def decode_all(self, private_key: str | Path) -> list[Path]:
@@ -223,30 +235,29 @@ class ImageRepository:
if self._is_jpg(encrypted_path):
continue
image_name = next(
image_name,image_hash = next(
(
original_name
for original_name, encrypted_name in naming_table
if encrypted_name == encrypted_path.name
),
None,
(item.image_name,item.image_hash)
for item in naming_table
if item.encrypted_file == encrypted_path.name
)
)
if image_name is None:
image_name = self._find_name_in_legacy_tables(encrypted_path.name)
self.logger.warning(
logger.warning(
f"Could not find original name for {encrypted_path.name} in naming table, checking legacy tables: {image_name}"
)
if image_name is None:
image_name = f"Sexy_MISSING_{len(decoded_paths) + 1}.jpg"
self.logger.warning(
logger.warning(
f"Could not find original name for {encrypted_path.name}, using {image_name}"
)
decoded_path = self._available_path(
self.img_folder_path / Path(image_name).name
)
self.logger.trace(f"Decoding {encrypted_path} to {decoded_path}")
encryption_manager.decrypt(encrypted_path, decoded_path, private_key)
logger.info(f"Decoding {encrypted_path} to {decoded_path}")
self.decode_image(encrypted_path, decoded_path, private_key, image_hash)
decoded_paths.append(decoded_path)
sources_to_remove.append(encrypted_path)
@@ -255,11 +266,11 @@ class ImageRepository:
encrypted_table = self.img_folder_path / "naming_table.enc"
encrypted_table.unlink(missing_ok=True)
self.logger.info("Decoded all encrypted images")
logger.info("Decoded all encrypted images")
return decoded_paths
def decode_images(
self, pattern: str, private_key: str | Path, remove: bool = False
self, pattern: str, private_key: str | Path, conf_dir : Path, remove: bool = False
) -> list[Path]:
matcher = re.compile(pattern)
naming_table_path = self.img_folder_path / "naming_table.enc"
@@ -268,38 +279,39 @@ class ImageRepository:
naming_table_path, private_key
)
selected_entries = [
entry for entry in naming_table if matcher.search(f"{entry[0]};{entry[1]}")
entry for entry in naming_table if matcher.search(f"{entry.image_name};{entry.encrypted_file};{entry.image_hash}")
]
decoded_paths = []
sources_to_remove = []
decoded_entries = []
new_folder = Path("~").expanduser() / "Wallpaper" / "New"
new_folder = conf_dir / "New"
new_folder.mkdir(parents=True, exist_ok=True)
for original_name, encrypted_name in selected_entries:
encrypted_path = self.img_folder_path / Path(encrypted_name).name
for item in selected_entries:
encrypted_path = self.img_folder_path / Path(item.encrypted_file).name
if not encrypted_path.exists():
matches = list(self.img_folder_path.rglob(Path(encrypted_name).name))
matches = list(self.img_folder_path.rglob(Path(item.encrypted_file).name))
if len(matches) != 1:
raise FileNotFoundError(
f"Could not uniquely locate encrypted image: {encrypted_name}"
f"Could not uniquely locate encrypted image: {item.encrypted_file}"
)
encrypted_path = matches[0]
if self._is_jpg(encrypted_path):
continue
decoded_path = self._available_path(new_folder / Path(original_name).name)
self.logger.trace(f"Decoding {encrypted_path} to {decoded_path}")
encryption_manager.decrypt(encrypted_path, decoded_path, private_key)
decoded_path = self._available_path(new_folder / Path(item.image_name).name)
logger.info(f"Decoding {encrypted_path} to {decoded_path}")
self.decode_image(encrypted_path, decoded_path, private_key, item.image_hash)
decoded_paths.append(decoded_path)
sources_to_remove.append(encrypted_path)
decoded_entries.append((original_name, encrypted_name))
decoded_entries.append(item)
if remove:
entries_to_keep = list(naming_table)
for entry in decoded_entries:
entries_to_keep.remove(entry)
logger.info(f"Remove entry {entry.image_name}")
encryption_manager.store_naming_table(
entries_to_keep, naming_table_path, private_key
)
@@ -312,6 +324,17 @@ class ImageRepository:
return decoded_paths
def decode_image(
self, encrypted_path: Path, decoded_path: Path, private_key: str | Path, hash_str: str
) -> None:
encryption_manager = EncryptionManager()
decrypted, hash = encryption_manager.decrypt(encrypted_path, decoded_path, private_key)
if hash != hash_str:
raise ValueError(
f"Hash mismatch for {decoded_path}: expected {hash_str}, got {hash}"
)
return decrypted
def _find_name_in_legacy_tables(self, encrypted_name: str) -> str | None:
legacy_folder = self.img_folder_path / "NT"
if not legacy_folder.is_dir():
@@ -345,15 +368,24 @@ class ImageRepository:
counter += 1
@staticmethod
def _unique_identifier(identifier: str, naming_table: list[tuple[str, str]]) -> str:
existing_names = {original_name for original_name, _ in naming_table}
def _unique_identifier(identifier: str, naming_table: list[FileInfo]) -> str:
path = Path(identifier)
counter = 1
while True:
candidate = f"{path.stem}_enc_DOUBLE_{counter}{path.suffix}"
if candidate not in existing_names:
return candidate
counter += 1
match = re.fullmatch(r"(.*?)(\d+)", path.stem)
prefix = match.group(1) if match else path.stem
width = len(match.group(2)) if match else 2
existing_numbers = []
for item in naming_table:
existing_path = Path(item.image_name)
if existing_path.suffix.casefold() != path.suffix.casefold():
continue
numbered_name = re.fullmatch(
rf"{re.escape(prefix)}(\d+)", existing_path.stem, re.IGNORECASE
)
if numbered_name:
existing_numbers.append(int(numbered_name.group(1)))
next_number = max(existing_numbers, default=0) + 1
return f"{prefix}{next_number:0{width}d}{path.suffix}"
def _convert_to_jpg(self, image_path: Path) -> Path | None:
if (
@@ -382,7 +414,7 @@ class ImageRepository:
converted.paste(rgba, mask=rgba.getchannel("A"))
else:
converted = image.convert("RGB")
self.logger.trace(f"Converting {image_path} to {jpg_path}")
logger.trace(f"Converting {image_path} to {jpg_path}")
converted.save(jpg_path, format="JPEG", quality=95)
finally:
pil_logger.setLevel(previous_log_level)