1
0

add more sort exception handlings

This commit is contained in:
= 2024-12-30 01:19:50 -05:00
parent 1a486ba9d9
commit e78d05f8b4
3 changed files with 49 additions and 14 deletions

View File

@ -1,9 +1,9 @@
#!/bin/sh #!/bin/sh
REGISTRY1=code.balsillie.net REGISTRY1=code.balsillie.net
REGISTRY2=quay.io REGISTRY2=registry.balsillie.house
NAMESPACE1=michael/containers NAMESPACE1=michael/containers
NAMESPACE2=balsillie # NAMESPACE2=balsillie
IMAGE=$1 IMAGE=$1
TAG1=$(date +%Y-%m-%d_%H-%M-%S) TAG1=$(date +%Y-%m-%d_%H-%M-%S)
@ -12,8 +12,8 @@ TAG2=latest
docker buildx build \ docker buildx build \
--tag $REGISTRY1/$NAMESPACE1/$IMAGE:$TAG1 \ --tag $REGISTRY1/$NAMESPACE1/$IMAGE:$TAG1 \
--tag $REGISTRY1/$NAMESPACE1/$IMAGE:$TAG2 \ --tag $REGISTRY1/$NAMESPACE1/$IMAGE:$TAG2 \
--tag $REGISTRY2/$NAMESPACE2/$IMAGE:$TAG1 \ --tag $REGISTRY2/$IMAGE:$TAG1 \
--tag $REGISTRY2/$NAMESPACE2/$IMAGE:$TAG2 \ --tag $REGISTRY2/$IMAGE:$TAG2 \
--file ./$IMAGE/Dockerfile \ --file ./$IMAGE/Dockerfile \
--push \ --push \
./$IMAGE ./$IMAGE

View File

@ -1,17 +1,25 @@
FROM python:3.12-slim FROM python:3.12-slim
# Set the working directory # Create working directory
WORKDIR /app RUN mkdir -p /app
# Copy the sort.py file to the working directory # Create and set user
COPY sort.py . RUN useradd -u 1000 -d /app -UM appuser
COPY requirements.txt .
# Install ffmpeg # Install ffmpeg
RUN apt-get update \ RUN apt-get update \
&& apt-get install -y ffmpeg \ && apt-get install -y ffmpeg \
&& apt-get clean && apt-get clean
# Chown appuser home
RUN chown -R appuser:appuser /app && chmod 750 /app
# Switch to appuser
USER appuser
# Set the working directory
WORKDIR /app
# Ensure pip # Ensure pip
RUN python -m ensurepip RUN python -m ensurepip
@ -19,7 +27,11 @@ RUN python -m ensurepip
RUN pip install --upgrade pip RUN pip install --upgrade pip
# Install requirements # Install requirements
RUN pip install -r requirements.txt COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
# Copy the sort.py file to the working directory
COPY sort.py /app/sort.py
# Set the entry point to sort.py # Set the entry point to sort.py
ENTRYPOINT ["python", "/app/sort.py"] ENTRYPOINT ["python", "/app/sort.py"]

View File

@ -5,6 +5,7 @@ import signal
import sys import sys
import json import json
import ffmpeg import ffmpeg
import shutil # For moving files
from os import getenv from os import getenv
from pathlib import Path from pathlib import Path
from PIL import Image, ExifTags, UnidentifiedImageError from PIL import Image, ExifTags, UnidentifiedImageError
@ -115,6 +116,8 @@ def sort_photos(*,
exif = image.getexif() exif = image.getexif()
except OSError: except OSError:
print(f"File {photo.name} could not be opened (permissions?), skipping.") print(f"File {photo.name} could not be opened (permissions?), skipping.")
ignore_list.append({"name": photo.name, "reason": "Permissions error"})
ignore_count += 1
continue continue
except UnidentifiedImageError: except UnidentifiedImageError:
print(f"File {photo.name} is not a valid image file. Ignoring.") print(f"File {photo.name} is not a valid image file. Ignoring.")
@ -123,9 +126,21 @@ def sort_photos(*,
continue continue
timestamp = None timestamp = None
if ExifTags.Base.DateTimeOriginal in exif.keys(): if ExifTags.Base.DateTimeOriginal in exif.keys():
try:
timestamp = time.strptime(exif[ExifTags.Base.DateTimeOriginal], "%Y:%m:%d %H:%M:%S") timestamp = time.strptime(exif[ExifTags.Base.DateTimeOriginal], "%Y:%m:%d %H:%M:%S")
except:
print(f"File {photo.name} has invalid timestamp format, ignoring.")
ignore_list.append({"name": photo.name, "reason": "Invalid exif timestamp"})
ignore_count += 1
continue
elif ExifTags.Base.DateTime in exif.keys(): elif ExifTags.Base.DateTime in exif.keys():
try:
timestamp = time.strptime(exif[ExifTags.Base.DateTime], "%Y:%m:%d %H:%M:%S") timestamp = time.strptime(exif[ExifTags.Base.DateTime], "%Y:%m:%d %H:%M:%S")
except:
print(f"File {photo.name} has invalid timestamp format, ignoring.")
ignore_list.append({"name": photo.name, "reason": "Invalid exif timestamp"})
ignore_count += 1
continue
if timestamp is None: if timestamp is None:
print(f"File {photo.name} does not have an exif timestamp, ignoring.") print(f"File {photo.name} does not have an exif timestamp, ignoring.")
ignore_list.append({"name": photo.name, "reason": "No exif timestamp"}) ignore_list.append({"name": photo.name, "reason": "No exif timestamp"})
@ -155,6 +170,8 @@ def sort_videos(*,
meta_dict: dict = ffmpeg.probe(video) meta_dict: dict = ffmpeg.probe(video)
except OSError: except OSError:
print(f"File {video.name} could not be opened (permissions?), skipping.") print(f"File {video.name} could not be opened (permissions?), skipping.")
ignore_list.append({"name": video.name, "reason": "Permissions error"})
ignore_count += 1
continue continue
except ffmpeg.Error: except ffmpeg.Error:
print(f"General error, file {video.name} could not be probed by ffmpeg, ignoring.") print(f"General error, file {video.name} could not be probed by ffmpeg, ignoring.")
@ -165,7 +182,13 @@ def sort_videos(*,
if "format" in meta_dict.keys(): if "format" in meta_dict.keys():
if "tags" in meta_dict["format"].keys(): if "tags" in meta_dict["format"].keys():
if "creation_time" in meta_dict["format"]["tags"].keys(): if "creation_time" in meta_dict["format"]["tags"].keys():
try:
timestamp = time.strptime(meta_dict["format"]["tags"]["creation_time"], "%Y-%m-%dT%H:%M:%S.%fZ") timestamp = time.strptime(meta_dict["format"]["tags"]["creation_time"], "%Y-%m-%dT%H:%M:%S.%fZ")
except:
print(f"{video.name} has invalid timestamp format, ignoring.")
ignore_list.append({"name": video.name, "reason": "Invalid creation_time format"})
ignore_count += 1
continue
if timestamp is None: if timestamp is None:
print(f"File {video.name} does not have creation_time metadata, ignoring.") print(f"File {video.name} does not have creation_time metadata, ignoring.")
ignore_list.append({"name": video.name, "reason": "No creation_time metadata"}) ignore_list.append({"name": video.name, "reason": "No creation_time metadata"})
@ -207,7 +230,7 @@ def rename_file(*,
# Move the file # Move the file
if not new_path.exists(): if not new_path.exists():
print(f"Moving {file.as_posix()} to {new_path.as_posix()}") print(f"Moving {file.as_posix()} to {new_path.as_posix()}")
file.rename(new_path) shutil.move(file, new_path)
sort_count += 1 sort_count += 1
else: else:
print(f"File {new_file_name} already exists, ignoring.") print(f"File {new_file_name} already exists, ignoring.")