1
0

sort now just copies to same filesystem

adjusted to run on kubernetes
This commit is contained in:
= 2025-01-04 01:21:20 -05:00
parent 4372822080
commit d7fdf8a6dc
2 changed files with 30 additions and 34 deletions

View File

@ -1,7 +1,7 @@
FROM python:3.12-slim
# Create working directory
RUN mkdir -p /app
RUN mkdir -p /app && mkdir /gallery
# Create and set user
RUN useradd -u 1000 -d /app -UM appuser
@ -12,7 +12,10 @@ RUN apt-get update \
&& apt-get clean
# Chown appuser home
RUN chown -R appuser:appuser /app && chmod 750 /app
RUN chown -R appuser:appuser /app \
&& chown -R appuser:appuser /gallery \
&& chmod 750 /app \
&& chmod 775 /gallery
# Switch to appuser
USER appuser
@ -33,5 +36,7 @@ RUN pip install -r /app/requirements.txt
# Copy the sort.py file to the working directory
COPY sort.py /app/sort.py
VOLUME /gallery
# Set the entry point to sort.py
ENTRYPOINT ["python", "/app/sort.py"]

View File

@ -16,20 +16,8 @@ photos_directory: Path = Path(getenv('PHOTOS_DIRECTORY', '/photos')).resolve()
recordings_directory: Path = Path(getenv('RECORDINGS_DIRECTORY', '/recordings')).resolve()
photo_extensions: list[str] = ['.jpg', '.jpeg']
video_extensions: list[str] = ['.mp4', '.mov', '.avi', '.mkv']
sleep_duration: int = 30
def main():
# Check the source directories exist and create the ignore files if they don't exist
for directory in source_directories:
if not directory.exists() or not directory.is_dir():
print(f"Source directory {directory} does not exist or is not a directory. Exiting.")
sys.exit(1)
ignore_file: Path = directory.joinpath("ignored_files.json")
if not ignore_file.exists():
print(f"Ignored files list {ignore_file} does not exist. Creating.")
with open(ignore_file, 'w') as f:
json.dump([], f)
# Check the destination directories exist and are writable
for directory in [photos_directory, recordings_directory]:
@ -44,25 +32,27 @@ def main():
print(f"Destination directory {directory} is not writable")
sys.exit(1)
# Start the main loop
while True:
for directory in source_directories:
print(f"Starting sort of directory {directory}.")
start_time = time.time()
sort_count, ignore_count = sort_directory(directory = directory)
end_time = time.time()
print(f"Finished sort of directory {directory}.")
print(f"Sorted {sort_count} files.")
print(f"Added {ignore_count} files to the ignore list.")
print(f"Processing took {end_time - start_time} seconds.")
print(f"Sleeping for {sleep_duration} seconds.")
time.sleep(sleep_duration)
# Check the source directories exist and create the ignore files if they don't exist
for directory in source_directories:
if not directory.exists() or not directory.is_dir():
print(f"Source directory {directory} does not exist or is not a directory. Exiting.")
sys.exit(1)
ignore_file: Path = directory.parent.joinpath(f"{directory.name}-ignore-file.json")
if not ignore_file.exists():
print(f"Ignored files list {ignore_file} does not exist. Creating.")
with open(ignore_file, 'w') as f:
json.dump([], f)
print(f"Starting sort of directory {directory}.")
sort_count, ignore_count = sort_directory(directory = directory, ignore_file = ignore_file)
print(f"Finished sort of directory {directory}.")
print(f"Sorted {sort_count} files.")
print(f"Added {ignore_count} files to the ignore list.")
print(f"Finished sorting {len(source_directories)} directories, exiting.")
def sort_directory(directory: Path) -> tuple[int, int]:
def sort_directory(directory: Path, ignore_file: Path) -> tuple[int, int]:
sort_count: int = 0
ignore_count: int = 0
# Load the ignored files list
ignore_file = directory.joinpath("ignored_files.json")
with open(file = ignore_file) as f:
ignore_list: list[dict] = json.load(f)
# Get all files in the directory
@ -146,7 +136,7 @@ def sort_photos(*,
ignore_list.append({"name": photo.name, "reason": "No exif timestamp"})
ignore_count += 1
continue
previous_name, ignore_list, sort_count, ignore_count = rename_file(
previous_name, ignore_list, sort_count, ignore_count = copy_file(
file = photo,
timestamp = timestamp,
previous_name = previous_name,
@ -194,7 +184,7 @@ def sort_videos(*,
ignore_list.append({"name": video.name, "reason": "No creation_time metadata"})
ignore_count += 1
continue
previous_name, ignore_list, sort_count, ignore_count = rename_file(
previous_name, ignore_list, sort_count, ignore_count = copy_file(
file = video,
timestamp = timestamp,
previous_name = previous_name,
@ -206,7 +196,7 @@ def sort_videos(*,
)
return (ignore_list, sort_count, ignore_count)
def rename_file(*,
def copy_file(*,
file: Path,
timestamp: time.struct_time,
previous_name: str,
@ -229,8 +219,9 @@ def rename_file(*,
new_path.parent.mkdir(parents=True, exist_ok=True)
# Move the file
if not new_path.exists():
print(f"Moving {file.as_posix()} to {new_path.as_posix()}")
shutil.move(file, new_path)
print(f"Copying {file.as_posix()} to {new_path.as_posix()}")
shutil.copy2(src=file, dst=new_path)
ignore_list.append({"name": file.name, "reason": "Already copied"})
sort_count += 1
else:
print(f"File {new_file_name} already exists, ignoring.")