summaryrefslogtreecommitdiff
path: root/hel/recv-media.nix
blob: c2223dd83b4e1b2ab3d02a7616eedbeb75f33e9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{ stdenv
, coreutils
, writeScriptBin
, eject
, notifyUser ? "gkleen"
, showTitle ? true
, ffmpeg ? null
, gnused ? null
}:

assert showTitle -> ffmpeg != null && gnused != null;

writeScriptBin "recv-media" ''
  #!${stdenv.shell}

  pid=$?

  PATH=${eject}/bin:${coreutils}/bin:${if showTitle then ''${ffmpeg}/bin:${gnused}/bin:'' else ""}/var/setuid-wrappers

  exec 1> >(logger --id=$pid -t recv-media -p user.notice)
  exec 2> >(logger --id=$pid -t recv-media -p user.error)

  [[ -z "$1" || -z "$2" ]] && exit 2

  dir=/var/media
  tmpFile="''${dir}"/.tmp/$(basename "$1")
  target="''${dir}"/$(basename "$1")

  if ! cp -lnv --preserve=all "$1" "''${target}"; then
    mkdir -pv $(dirname "''${tmpFile}")

    ionice -c 3 -t cp -vn --preserve=all "$1" "''${tmpFile}" && mv -v "''${tmpFile}" "''${target}"
    touch -c -m -t "$2" "''${target}"
  fi

  (
    summary=$(ffmpeg -i "''${target}" -f ffmetadata 2>/dev/null | tee /dev/stderr | sed -r '/^title=/!d; s/^title=//')
    if [[ -z "''${summary}" ]]; then
      echo "Title is empty, replacing with filename" >&2
      summary=$(basename "$1")
    fi
    printf "%s\n%s\n" "''${summary}" "" | notify-${notifyUser} -a recv-media || true
  ) || true
''