From 44583f670fe31c26ad396826a81f01419b0fae17 Mon Sep 17 00:00:00 2001 From: Jaculabilis Date: Fri, 5 Jan 2024 01:22:43 +0000 Subject: [PATCH] empyrean: cron job to move files from phone to NAS --- machine/empyrean/default.nix | 2 + machine/empyrean/sync-pipeline.nix | 74 ++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 machine/empyrean/sync-pipeline.nix diff --git a/machine/empyrean/default.nix b/machine/empyrean/default.nix index 9e97d01..ecd8cfb 100644 --- a/machine/empyrean/default.nix +++ b/machine/empyrean/default.nix @@ -11,6 +11,7 @@ #./amanuensis.nix ./catacomb.nix ./gitea.nix + ./sync-pipeline.nix ]; beatific.hostName = "empyrean"; @@ -39,6 +40,7 @@ networking.interfaces.eth0.useDHCP = true; environment.systemPackages = with pkgs; [ + exiftool tinc_pre gitea ]; diff --git a/machine/empyrean/sync-pipeline.nix b/machine/empyrean/sync-pipeline.nix new file mode 100644 index 0000000..30c4db0 --- /dev/null +++ b/machine/empyrean/sync-pipeline.nix @@ -0,0 +1,74 @@ +{ pkgs, ... }: + +# exiftool config and script based on that of @numinit + +let + # An exiftool config that adds the OriginTimestamp property containing the best guess for file creation time + exiftool-config = pkgs.writeText "exiftool.config" '' + %Image::ExifTool::UserDefined = ( + 'Image::ExifTool::Composite' => { + OriginTimestamp => { + Desire => { + 0 => 'CreateDate', + 1 => 'DateTimeOriginal', + 2 => 'FileModifyDate' + }, + ValueConv => '$val[0] || $val[1] || $val[2] || undef', + PrintConv => '$self->ConvertDateTime($val)', + PrintConvInv => '$self->InverseDateTime($val)' + } + } + ) + ''; + + # A tool to use `find` and `exiftool` to sort images into YMD folders + sibd = pkgs.writeShellScriptBin "sort-images-by-date" '' + set -euo pipefail + + if [ $# -lt 3 ]; then + echo "Usage: $0 [prefix] [src] [dest] [exiftool args...]" >&2 + exit 1 + fi + + prefix="$1" + src="$2" + dest="$3" + # Remove $1, $2, $3 from $@ so the remainder can be passed to exiftool + shift; shift; shift + + # This find expression matches .jpg/.png modified 14+ days ago. + # The -not (-name -prune) expressions exclude directories from the results by name. + # The exiftool command will move matched files to $dest/$prefix/year/month/day/filename.ext. + # -config loads the config defined above, which adds the OriginTimestamp composite property + # -filename tells exiftool to move each file to a new filename, $OriginTimestamp + # -d defines how the timestamp will be converted to a string, which ends up handling the pathing + ${pkgs.findutils}/bin/find $src \ + -not \( -name FalseKnees -prune \) \ + -not \( -name Background -prune \) \ + -type f \ + -mtime +14 \ + \( -name "*.jpg" -or -name "*.png" \) \ + -exec ${pkgs.exiftool}/bin/exiftool \ + -config ${exiftool-config} \ + -api largefilesupport=1 \ + -progress \ + -filename'