From 3f9026072887cc5723948ec151dd108cf874225d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Sun, 15 Jul 2018 17:36:31 +0200 Subject: [PATCH] improved meta generation code --- cgilite | 2 +- indexmeta.sh | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ list.sh | 22 +++++++++---------- multitag.sh | 42 ++++++++++++++++------------------- update_meta.sh | 39 --------------------------------- 5 files changed, 90 insertions(+), 74 deletions(-) create mode 100644 indexmeta.sh delete mode 100644 update_meta.sh diff --git a/cgilite b/cgilite index d480b7c..813580b 160000 --- a/cgilite +++ b/cgilite @@ -1 +1 @@ -Subproject commit d480b7cb55bbd085b361398bdc4903817de1709a +Subproject commit 813580b4cdebfe5161ef01bcbd126405c3162d7a diff --git a/indexmeta.sh b/indexmeta.sh new file mode 100644 index 0000000..84dd26a --- /dev/null +++ b/indexmeta.sh @@ -0,0 +1,59 @@ +#!/bin/sh + +[ -n "$include_indexmeta" ] && return 0 +include_indexmeta="$0" + +. "$_EXEC/cgilite/storage.sh" +file_pattern='^.*\.(mov|ts|mpg|mpeg|mp4|m4v|avi|mkv|flv|sfv|wmv|ogm|ogv|webm|iso|rmvb)$' + +meta_name() { + fn="$1" + fn="${fn##*/}" + fn="${fn%.*}" + STRING "$fn" +} + +meta_line() { + video="$1" + + read l h w <<__EOF + $(printf '' \ + | mplayer -input nodefault-bindings -nosound -vo null -identify -frames 0 "$video" 2>&- \ + | sort | sed -rn ' + s:ID_LENGTH=(.*)(\..*)$:\1:p; + s:ID_VIDEO_HEIGHT=(.*):\1:p; + s:ID_VIDEO_WIDTH=(.*):\1:p;' \ + | tr '\n' ' ' + ) +__EOF + printf '%i\t%i\t%i\ttags=\tcomment=\t%s\n' \ + "${l-0}" "${w-0}" "${h-0}" "$(meta_name "$video")" +} + +meta_dir(){ + dir="${1}" + meta="${dir}/.index/meta" + + if [ -d "$dir/.index" -a \! -f "$meta" ] && LOCK "$meta"; then + touch "$meta" # preliminary touch to prevent concurrent generators + find "$dir" -type f -mindepth 1 -maxdepth 1 \ + | grep -E "$file_pattern" \ + | while v="$(line)"; do + meta_line "$v" + done >"$meta" + + RELEASE "$meta" + elif [ -d "$dir/.index" -a "$dir" -nt "$meta" ] && LOCK "$meta"; then + touch "$meta" + find "$dir" -type f -mindepth 1 -maxdepth 1 \ + | grep -E "$file_pattern" \ + | while v="$(line)"; do + if [ "$v" -nt "$meta" ]; then + grep -qF " $(meta_name "$v")" "$meta" \ + || meta_line "$v" + fi + done >>"$meta" + + RELEASE "$meta" + fi +} diff --git a/list.sh b/list.sh index d0868ba..4596dfc 100644 --- a/list.sh +++ b/list.sh @@ -1,5 +1,7 @@ #!/bin/sh +. "$_EXEC/indexmeta.sh" + list_item() { name="$(HTML "$1")" path="$(HTML "$ITEM/$1")" @@ -10,7 +12,7 @@ list_item() { "$path" "$(HTML "$QUERY_STRING")" "$name" elif [ -f "$meta" ]; then read -r length width height tags comment n <<-EOF - $(grep -m1 -F " ${1##*/}" "$meta") + $(grep -m1 -F " $(meta_name "$1")" "$meta") EOF printf '[div .list .file [a href="%s" [img src="%s?a=thumbnail"][label %s]] @@ -29,8 +31,7 @@ list_item() { } list_fs_browse(){ - [ -d "$_DATA/$ITEM/.index" -a \! "$_DATA/$ITEM" -ot "$_DATA/$ITEM/.index/meta" ] \ - && dir="$_DATA/$ITEM" . "$_EXEC/update_meta.sh" + meta_dir "$_DATA/$ITEM" (cd "$_DATA/$ITEM"; find ./ -type d \! -name .index -mindepth 1 -maxdepth 1 \ -exec stat -c '%Y %n' '{}' + @@ -41,8 +42,7 @@ list_fs_browse(){ list_fs_index(){ find "$_DATA/$ITEM" -type d -name .index \ | while d="$(line)"; do - [ ! "${d%/.index}" -ot "$d/meta" ] \ - && dir="${d%/.index}" . "$_EXEC/update_meta.sh" + meta_dir "${d%/.index}" done (cd "$_DATA/$ITEM"; find ./ \! -path '*/.index/*' -type f \ @@ -52,8 +52,7 @@ list_fs_index(){ list_browse(){ meta="$_DATA/$ITEM/.index/meta" - [ -d "${meta%/meta}" -a \! "$_DATA/$ITEM" -ot "${meta}" ] \ - && dir="$_DATA/$ITEM" . "$_EXEC/update_meta.sh" + meta_dir "$_DATA/$ITEM" (cd "$_DATA/$ITEM"; find ./ -type d \! -name .index -mindepth 1 -maxdepth 1 \ ) | cut -d/ -f2- | sort @@ -67,12 +66,13 @@ list_index(){ find ./ -path '*/.index/meta' ) | while meta="$(line)"; do base="${meta%/.index/meta}" - [ \! "$_DATA/$ITEM/$base" -ot "$_DATA/$ITEM/$meta" ] \ - && dir="$_DATA/$ITEM/$base" . "$_EXEC/update_meta.sh" + meta_dir "$_DATA/$ITEM/$base" cut -f1,6- <"$_DATA/$ITEM/$meta" \ | while f="$(line)"; do - [ -e "$_DATA/$ITEM/$base/${f#* }" ] \ - && printf '%s %s\n' "${f%% *}" "${base}/${f#* }" + fn="$(printf '%s\n' "$_DATA/$ITEM/$base/${f#* }".*)" + fn="${fn%%${BR}*}"; fn="${fn##*/}" + [ -e "$_DATA/$ITEM/$base/${fn}" ] \ + && printf '%s %s\n' "${f%% *}" "${base}/${fn}" done done \ | sort -n \ diff --git a/multitag.sh b/multitag.sh index 43484e6..0a8e9d4 100644 --- a/multitag.sh +++ b/multitag.sh @@ -1,35 +1,31 @@ #!/bin/sh +. "$_EXEC/cgilite/storage.sh" + for select in $(seq 1 $(POST_COUNT select)); do file="$_DATA/$ITEM/$(POST select $select)" meta="${file%/*}/.index/meta" fn="${file##*/}" - while ! mkdir "${meta}.lock"; do - sleep 1 - ps -o pid \ - | grep -q "$(cat "${meta}.lock/pid")" \ - || break - done - printf '%i\n' >"${meta}.lock/pid" - - grep -vF " $fn" "$meta" >"${meta}.lock/mtmp" - read length width height tags comment x <<-EOF - $(grep -F " $fn" "$meta") + if LOCK "$meta"; then + grep -vF " $fn" "$meta" >"${meta}.tmp" + read length width height tags comment x <<-EOF + $(grep -F " $fn" "$meta") EOF - tags="$(printf '%s' "${tags#tags=}" | tr , '\n')" + tags="$(printf '%s' "${tags#tags=}" | tr , '\n')" - for tn in $(seq 1 $(POST_COUNT tag)); do - tags="$tags${BR}$(POST tag $tn |sed -r 's;\t;\\t;g; s;\r;;g;')" - done - tags="$tags${BR}$(POST newtag |sed -r 's;\t;\\t;g;')" + for tn in $(seq 1 $(POST_COUNT tag)); do + tags="$tags${BR}$(POST tag $tn |sed -r 's;\t;\\t;g; s;\r;;g;')" + done + tags="$tags${BR}$(POST newtag |sed -r 's;\t;\\t;g; s;\r;;g;')" - printf '%s\t%s\t%s\ttags=%s\t%s\t%s\n' \ - "$length" "$width" "$height" \ - "$(printf %s "$tags" |sort -u |tr '\n' ,)" \ - "$comment" "$fn" \ - >>"${meta}.lock/mtmp" - mv "${meta}.lock/mtmp" "$meta" + printf '%i\t%i\t%i\ttags=%s\t%s\t%s\n' \ + "$length" "$width" "$height" \ + "$(printf %s "$tags" |sort -u |tr '\n' ,)" \ + "$comment" "$fn" \ + >>"${meta}.tmp" + mv "${meta}.tmp" "$meta" - rm "${meta}.lock/pid"; rmdir "${meta}.lock" + RELEASE "$meta" + fi done diff --git a/update_meta.sh b/update_meta.sh deleted file mode 100644 index 04fc77e..0000000 --- a/update_meta.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/sh - -file_pattern='^.*\.(mov|ts|mpg|mpeg|mp4|m4v|avi|mkv|flv|sfv|wmv|ogm|ogv|webm|iso|rmvb)$' -meta="${dir}/.index/meta" - -meta_line() { - video="$1" - - read l h w <<__EOF - $(printf '' \ - | mplayer -input nodefault-bindings -nosound -vo null -identify -frames 0 "$video" 2>&- \ - | sort | sed -rn ' - s:ID_LENGTH=(.*)(\..*)$:\1:p; - s:ID_VIDEO_HEIGHT=(.*):\1:p; - s:ID_VIDEO_WIDTH=(.*):\1:p;' \ - | tr '\n' ' ' - ) -__EOF - printf '%i\t%i\t%i\ttags=\tcomment=\t%s\n' \ - "$l" "$w" "$h" "${video##*/}" -} - - -if [ \! -f "$meta" ]; then - find "$dir" -type f -mindepth 1 -maxdepth 1 \ - | grep -E "$file_pattern" \ - | while v="$(line)"; do - meta_line "$v" - done >"$meta" -else - find "$dir" -type f -mindepth 1 -maxdepth 1 \ - | grep -E "$file_pattern" \ - | while v="$(line)"; do - if [ "$v" -nt "$meta" ]; then - grep -qF " ${v}" "$meta" \ - || meta_line "$v" - fi - done >>"$meta" -fi -- 2.39.2