Skip to content

Commit

Permalink
normalize filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
tymmej committed Aug 23, 2023
1 parent 7a79a0c commit 2887630
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/sync_drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import re
import time
import unicodedata
from pathlib import Path
from shutil import copyfileobj, rmtree, unpack_archive

Expand Down Expand Up @@ -78,12 +79,13 @@ def process_folder(item, destination_path, filters, ignore, root):
if not (item and destination_path and root):
return None
new_directory = os.path.join(destination_path, item.name)
new_directory_norm = unicodedata.normalize('NFC', new_directory)
if not wanted_folder(
filters=filters, ignore=ignore, folder_path=new_directory, root=root
filters=filters, ignore=ignore, folder_path=new_directory_norm, root=root
):
LOGGER.debug(f"Skipping the unwanted folder {new_directory} ...")
return None
os.makedirs(new_directory, exist_ok=True)
os.makedirs(new_directory_norm, exist_ok=True)
return new_directory


Expand Down Expand Up @@ -152,6 +154,10 @@ def process_package(local_file):
os.rename(local_file, archive_file)
LOGGER.info(f"Unpacking {archive_file} to {os.path.dirname(archive_file)}")
unpack_archive(filename=archive_file, extract_dir=os.path.dirname(archive_file))
try:
os.rename(unicodedata.normalize('NFD', local_file), local_file)
except:
LOGGER.warning("Normalizing failed")
os.remove(archive_file)
elif "application/gzip" == magic_object.from_file(filename=local_file):
archive_file += ".gz"
Expand Down Expand Up @@ -203,6 +209,7 @@ def process_file(item, destination_path, filters, ignore, files):
if not (item and destination_path and files is not None):
return False
local_file = os.path.join(destination_path, item.name)
local_file = unicodedata.normalize('NFC', local_file)
if not wanted_file(filters=filters, ignore=ignore, file_path=local_file):
return False
files.add(local_file)
Expand Down Expand Up @@ -267,7 +274,7 @@ def sync_directory(
if not new_folder:
continue
try:
files.add(new_folder)
files.add(unicodedata.normalize('NFC', new_folder))
files.update(
sync_directory(
drive=item,
Expand Down
8 changes: 7 additions & 1 deletion src/sync_photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import shutil
import time
import unicodedata
from pathlib import Path

from icloudpy import exceptions
Expand Down Expand Up @@ -33,11 +34,16 @@ def generate_file_name(photo, file_size, destination_path):
destination_path,
f'{"__".join([name, file_size, base64.urlsafe_b64encode(photo.id.encode()).decode()])}.{extension}',
)

file_size_id_path_norm = unicodedata.normalize('NFC', file_size_id_path)

if os.path.isfile(file_path):
os.rename(file_path, file_size_id_path)
if os.path.isfile(file_size_path):
os.rename(file_size_path, file_size_id_path)
return file_size_id_path
if os.path.isfile(file_size_id_path):
os.rename(file_size_id_path, file_size_id_path_norm)
return file_size_id_path_norm


def photo_exists(photo, file_size, local_path):
Expand Down

0 comments on commit 2887630

Please sign in to comment.