From b8c27de2a15f0ac29ac7619ca83433fdc097934a Mon Sep 17 00:00:00 2001 From: Dmitry Voronin Date: Sun, 23 Jun 2024 16:40:50 +0300 Subject: [PATCH] Photoprocess : Convert from container to systemd service. --- container/Photoprocess.nix | 100 ------------------------------------- container/default.nix | 5 -- host/home/Container.nix | 1 - host/home/Photoprocess.nix | 79 +++++++++++++++++++++++++++++ host/home/default.nix | 1 + 5 files changed, 80 insertions(+), 106 deletions(-) delete mode 100644 container/Photoprocess.nix create mode 100644 host/home/Photoprocess.nix diff --git a/container/Photoprocess.nix b/container/Photoprocess.nix deleted file mode 100644 index 0a32e3ce..00000000 --- a/container/Photoprocess.nix +++ /dev/null @@ -1,100 +0,0 @@ -# Takes pictures from first photo dir, processes and stores to the second one. -{ container, util, pkgs, ... } @args: let - cfg = container.config.photoprocess; -in { - systemd.tmpfiles.rules = container.mkContainerDir cfg [ - "data" - ]; - - containers.photoprocess = container.mkContainer cfg { - bindMounts = { - "/data" = { - hostPath = "${cfg.storage}/data"; - isReadOnly = false; - }; - "/in" = { - # hostPath = "${container.config.cloud.storage}/data/data/" - hostPath = builtins.elemAt cfg.photo 0; - isReadOnly = false; - }; - "/out" = { - hostPath = builtins.elemAt cfg.photo 1; - isReadOnly = false; - }; - }; - - config = { lib, ... }: container.mkContainerConfig cfg { - systemd.services.photosprocess = let - script = pkgs.writeText "PhotoprocessScript" (util.trimTabs '' - source /data/Notify.sh - - cd /in - - # Convert png to jpg. - for img in $(ls *.png) $(ls *.PNG); do - filename=''${img%.*} - convert "$filename.png" "$filename.jpg" - done - rm *.png *.PNG - - # Rename bad extensions. - for img in $(ls *.jpeg) $(ls *.JPG) $(ls *.JPEG); do - filename=''${img%.*} - mv "$img" "$filename.jpg" - done - - # Compress jpg. - mogrify -resize 2073600@ *.jpg - jpegoptim --size=1000k *.jpg - - # 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 - - # Move to images. - total_photos=$(ls | wc -l) - mv *.jpg /out - notify_silent "Photos processed: $total_photos" - ''); - in util.mkStaticSystemdService { - enable = true; - description = "Process uploaded photos."; - serviceConfig = { - Type = "oneshot"; - ExecStart = "-${lib.getExe pkgs.bashInteractive} ${script}"; - Restart = "on-failure"; - }; - path = with pkgs; [ - curl - imagemagick - jpegoptim - ]; - }; - - systemd.timers.photosprocess = { - timerConfig = { - OnCalendar = "daily"; - Persistent = true; - Unit = "photosprocess.service"; - }; - wantedBy = [ "timers.target" ]; - }; - }; - }; -} diff --git a/container/default.nix b/container/default.nix index f4efc09a..dd898374 100644 --- a/container/default.nix +++ b/container/default.nix @@ -141,11 +141,6 @@ port = 80; storage = "${storage}/paste"; }; - photoprocess = { - inherit (media) photo; - address = "10.1.0.4"; - storage = "${storage}/photoprocess"; - }; print = { domain = "print.${domain}"; address = "10.1.0.46"; diff --git a/host/home/Container.nix b/host/home/Container.nix index 6da46dfb..531bb139 100644 --- a/host/home/Container.nix +++ b/host/home/Container.nix @@ -52,7 +52,6 @@ in { (import ../../container/Paper.nix args) (import ../../container/Pass.nix args) (import ../../container/Paste.nix args) - (import ../../container/Photoprocess.nix args) (import ../../container/Postgres.nix args) (import ../../container/Print.nix args) (import ../../container/Proxy.nix args) diff --git a/host/home/Photoprocess.nix b/host/home/Photoprocess.nix new file mode 100644 index 00000000..273be8d6 --- /dev/null +++ b/host/home/Photoprocess.nix @@ -0,0 +1,79 @@ +# Takes pictures from first photo dir, processes and stores to the second one. +{ util, pkgs, lib, ... } @args: let + imgInput = "/storage/hot/container/cloud/data/data/cakee/files/media/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 "{\"chat_id\":\"155897358\",\"text\":\"$1\",\"disable_notification\":\"true\"}" https://api.telegram.org/bot2046849441:AAHQpjRK4xpL8tEUyN4JTSDUUze4J0wSIy4/sendMessage &> /dev/null + } + + cd ${imgInput} + + # Convert png to jpg. + for img in $(ls *.png) $(ls *.PNG); do + filename=''${img%.*} + convert "$filename.png" "$filename.jpg" + done + rm *.png *.PNG + + # Rename bad extensions. + for img in $(ls *.jpeg) $(ls *.JPG) $(ls *.JPEG); do + filename=''${img%.*} + mv "$img" "$filename.jpg" + done + + # Compress jpg. + mogrify -resize 2073600@ *.jpg + jpegoptim --size=1000k *.jpg + + # 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 + + # 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 = { + Type = "oneshot"; + ExecStart = "-${lib.getExe pkgs.bashInteractive} ${script}"; + Restart = "on-failure"; + }; + path = with pkgs; [ + curl + imagemagick + jpegoptim + ]; + }; + + systemd.timers.photosprocess = { + timerConfig = { + OnCalendar = "daily"; + Persistent = true; + Unit = "photosprocess.service"; + }; + wantedBy = [ "timers.target" ]; + }; +} diff --git a/host/home/default.nix b/host/home/default.nix index be751078..de4e1b0a 100644 --- a/host/home/default.nix +++ b/host/home/default.nix @@ -4,6 +4,7 @@ ./Container.nix ./Filesystem.nix ./Network.nix + ./Photoprocess.nix ]; # Disable auto-switch.