nix/host/x86_64-linux/home/Photoprocess.nix

88 lines
2 KiB
Nix
Raw Normal View History

# Takes pictures from first photo dir, processes and stores to the second one.
{
2024-11-04 04:37:29 +03:00
lib,
pkgs,
secret,
util,
...
} @args: let
imgInput = "/storage/hot/container/cloud/data/data/cakee/files/photo";
imgOutput = "/storage/cold_1/backup/tmp/photo";
in {
systemd.services.photosprocess = let
script = pkgs.writeText "PhotoprocessScript" (util.trimTabs ''
function notify_silent() {
curl -X POST -H 'Content-Type: Application/json' -d "${secret.tg.dt "true"}" ${secret.tg.bt} &> /dev/null
}
2024-11-04 04:37:29 +03:00
cd ${imgInput}
2024-11-04 04:37:29 +03:00
# Convert png to jpg.
for img in $(ls *.png) $(ls *.PNG); do
filename=''${img%.*}
convert "$filename.png" "$filename.jpg"
done
rm *.png *.PNG
2024-11-04 04:37:29 +03:00
# Rename bad extensions.
for img in $(ls *.jpeg) $(ls *.JPG) $(ls *.JPEG); do
filename=''${img%.*}
mv "$img" "$filename.jpg"
done
2024-11-04 04:37:29 +03:00
# Compress jpg.
mogrify -resize 2073600@ *.jpg
jpegoptim --size=1000k *.jpg
2024-11-04 04:37:29 +03:00
# Rename to hash.
for file in *; do
if [[ -f "$file" ]]; then
extension="''${file##*.}"
if [ -f "$file" ] && [ "$extension" != "$file" ]; then
new_name="''${file%$extension}"
else
new_name="$file"
extension=""
fi
new_name=$(sha1sum "$file" | cut -d\ -f1)
if [[ "$extension" != "" ]]; then
new_name="''${new_name,,}.$extension"
else
new_name="''${new_name,,}"
fi
mv "$file" "$new_name"
fi
done
2024-11-04 04:37:29 +03:00
# Move to images.
total_photos=$(ls | wc -l)
mv *.jpg ${imgOutput}
notify_silent "Photos processed: $total_photos"
'');
in util.mkStaticSystemdService {
enable = true;
description = "Process uploaded photos";
serviceConfig = {
ExecStart = "-${lib.getExe pkgs.bashInteractive} ${script}";
Restart = "on-failure";
Type = "oneshot";
};
path = with pkgs; [
curl
imagemagick
jpegoptim
];
};
2024-11-04 04:37:29 +03:00
systemd.timers.photosprocess = {
timerConfig = {
OnCalendar = "daily";
Persistent = true;
Unit = "photosprocess.service";
};
wantedBy = [
"timers.target"
];
};
}