]> git.plutz.net Git - serve0/blobdiff - pages/list.sh
added licence headers, coding style improvements
[serve0] / pages / list.sh
index a86f04c0a701d2b8bbf184fd251e7ac1cd2416ce..0870961fa124b7b498214dd66d2cef358a441406 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/zsh
 
-# Copyright 2014, 2015 Paul Hänsch
+# Copyright 2014 - 2016 Paul Hänsch
 #
 # This file is part of Serve0.
 # 
 
 . $_EXEC/pages/common.sh
 
-filters="$(printf %s "${_GET[f]}" |sed -r 's;^!*\^*;;; s;\^*!*$;;; s;!+;!;g; s;\^+;^;g')"
+LF='
+'
+filters="${_GET[f]}"
 debug "FILTERS: $filters"
 
 quicklinks(){
   output=''
-  tac ${_DATA}/meta/recent |while [ "$(echo "$output" |wc -l)" -lt 10 ] && read line; do
+  tac ${_DATA}/meta/recent \
+  | while [ "$(printf %s "$output" |wc -l)" -lt 10 ] && read line; do
     line="<a href="$line">$line</a>" 
-    echo "$output" |grep -qF "$line" || output="$output$line\n"
+    printf %s "$output" |grep -qF "$line" || output="$output$line$LF"
   done 
-  echo $output
-}
-
-list_categories(){
-  echo none 
-  printf %s "$taglist" |sed -rn 's;^([^:]+):.*$;\1;p' |sort -u
+  printf %s\\n "$output"
 }
 
 category_selected(){
@@ -47,14 +45,87 @@ category_selected(){
   | sed 's;^0 ;<option>;;s;^1 ;<option selected>;;s;$;</option>;'
 }
 
-case "$1" in
-  title)
-    echo "Videos"
-  ;;
-  css)
-    . ${_EXEC}/templates/common.css.sh
-  ;;
-  body)
-    . ${_EXEC}/templates/list.html.sh
-  ;;
-esac
+_printVideo(){
+  info="$1"
+  cache="${_DATA}/cache/$info.cache"
+  meta="${_DATA}/meta/$info.meta"
+
+  if [ "$cache" -nt "$meta" ]; then
+    cat "$cache"
+  else
+    videofile="${_DATA}/videos/$info"
+    thumb="${_DATA}/thumbs/${info}.jpg"
+    [ -r "$thumb" ] || genthumb "$videofile" "$thumb"
+    [ -r "$meta"  ] || genmeta "$videofile" "$meta"
+
+    head -n1 "$meta" |read length width height filename
+    length=$(validate "$length" '[0-9]+' 0)
+    width=$(validate "$width" '[0-9]+' 0)
+    height=$(validate "$height" '[0-9]+' 0)
+
+    tags="$(sed -n 2p "$meta")"
+    video="$(urlsafe "videos/$info")"
+    thumb="$(urlsafe "thumbs/$info.jpg")"
+    linkinfo="$(urlsafe "$info")"
+    htmlinfo="$(htmlsafe "$info")"
+
+    [ "$(($length % 60))" -lt 10 ] && minutes="$(($length / 60)):0$(($length % 60))" \
+                                   || minutes="$(($length / 60)):$(($length % 60))"
+
+    tee "$cache" <<VIDEOend
+      <li class="thumb">
+        <a class="watchlink" name="${linkinfo}" href="?action=watch&i=${linkinfo}">
+          <img src="$thumb" alt="Preview not yet available">
+        </a>
+        <h2>${htmlinfo}</h2>
+        
+        <input type="checkbox" name="tagsel" value="${htmlinfo}">
+        <span class="info property">${minutes}min</span>
+        <span class="info property">${width}x${height}</span>
+       $(printf %s "$tags" |sed -r 's:\|*([^|]+)\|*: <span class="info tag">\1</span>:g')
+      </li>
+VIDEOend
+  fi
+}
+
+genlist(){
+  case "$order" in
+    Date)   ls -c "${_DATA}"/videos/ |egrep -i "$file_pattern";;
+    Length) sed -sn 1p "${_DATA}"/meta/*.meta |sort -n |sed -r 's;^[0-9\t]+\t;;';;
+    Name)   printf '%s\n' "${_DATA}"/videos/* |sed -r 's;^.*/;;;';;
+  esac
+}
+
+thumblist() {
+  cache="${_DATA}/cache/${pagesize}?o=${order}&s=${search}&f=${filter}&pn=${page}"
+  if [ "$cache" -nt ${_DATA}/videos -a "$cache" -nt ${_DATA}/meta ]; then
+    cat "$cache"
+  else
+    filterex="s;^([0-9]+\t){3}(.+)\n.*$;\2;p"
+    printf '%s\n' "$filter" |tr '^' '\n' \
+    | while read each; do
+      [ "${each:0:1}" = '!' ] && filterex="/^[^\n]+\n(.*\|)?(${each#?})(\|.*)?$/d;{${filterex}}" \
+                              || filterex="/^[^\n]+\n(.*\|)?(${each})(\|.*)?$/{${filterex}}"
+    done
+      
+    genlist \
+    | if [ -n "$search" ] ; then
+      debug "Applying search: ${search}"
+      xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
+      | xargs -d '\n' egrep -sil "$search" \
+      | sed -r 's;^.*/;;;s;\.meta$;;'
+    elif [ -n "$filter" ]; then
+      debug "Applying filters: ${filter} ++ ${filterex}"
+      xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
+      | xargs -d '\n' sed -srn ":a;N;2!ba;{${filterex}}"
+    else
+      debug "No search or filtering"
+      cat
+    fi \
+    | sed -n "$page,$(($page + $pagesize - 1))p" \
+    | tee "$cache"
+  fi \
+  | while read line; do
+    _printVideo "$line"
+  done
+}