]> git.plutz.net Git - serve0/blobdiff - pages/list.sh
styling for pagination links
[serve0] / pages / list.sh
index a86f04c0a701d2b8bbf184fd251e7ac1cd2416ce..89576f833fe2e04bdc6f9645fe71a07dfbe89511 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/zsh
 
-# Copyright 2014, 2015 Paul Hänsch
+# Copyright 2014 - 2017 Paul Hänsch
 #
 # This file is part of Serve0.
 # 
 # You should have received a copy of the GNU Affero General Public License
 # along with Serve0.  If not, see <http://www.gnu.org/licenses/>. 
 
+page_link="p=list&"
+
 . $_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
-    line="<a href="$line">$line</a>" 
-    echo "$output" |grep -qF "$line" || output="$output$line\n"
-  done 
-  echo $output
+present_filter(){
+  printf %s\\n "$*" \
+  | sed -r 's;(^|!|\^|\|)([^!\^\|:]+:);\1;g;s;\^;\n;g' \
+  | sort -r \
+  | sed -r '$q;s;$; \&amp\; ;g'
 }
 
-list_categories(){
-  echo none 
-  printf %s "$taglist" |sed -rn 's;^([^:]+):.*$;\1;p' |sort -u
-}
+if [ -n "$search" ]; then
+  TITLE="$search by $order"
+elif [ -n $filter ]; then
+  TITLE="$(present_filter "$filter") by $order"
+else
+  TITLE="List by $order"
+fi
 
 category_selected(){
   cat="${1}:"
   filter="$2"
   [ "$cat" = "none:" ] && cat=''
 
-  printf %s "$taglist" \
+  printf %s "$taglist_filter" \
   | sed -r 's;^('"$filter"')$;1 &;;t;s;^;0 ;' \
   | sed -rn 's;^(0|1) '"$cat"'([^:]+)$;\1 \2;p' \
   | 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")"
+    attrinfo="$(attribsafe "$info")"
+
+    minutes="$(printf "%d:%02d" $(($length / 60)) $(($length % 60)) )"
+
+    { tr -d '\n' |tee "$cache"; } <<-VIDEOend
+       <li id="$attrinfo" class="thumb">
+         <button class="watchlink" name="${linkinfo}" formaction="?action=watch&i=${linkinfo}">
+           <img src="$thumb" width="212" height="162" alt="Preview not yet available">
+         </button>
+         <h2><a href="?action=watch&i=${linkinfo}">${htmlinfo}</a></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)
+      find "${_DATA}"/videos/ -maxdepth 1 -mindepth 1 -printf '%i\t%p\n' \
+      | sort -n \
+      | while read i file; do printf '%s\000' "$file"; done \
+      | xargs -0 stat -c '%Y   %n' \
+      | sort -nr \
+      | cut -f2- \
+      | sed 's;^.*/;;g;' \
+      | egrep -i "$file_pattern"
+      ;;
+    Length)
+      sed -sn 1p "${_DATA}"/meta/*.meta \
+      | sort -n \
+      | sed -r 's;^[0-9\t]+\t;;' \
+      | while read file; do
+        [ -f "${_DATA}/videos/${file}" ] && printf '%s\n' "$file"
+      done
+      ;;
+    Name)
+      find "${_DATA}"/videos/ -maxdepth 1 -mindepth 1 -printf     '%f\n' \
+      | sort -f \
+      | egrep -i "$file_pattern"
+      ;;
+  esac
+}
+
+selectionlist() {
+  cachebase="?o=${order}&s=${search//\//}&f=${filter//\//}"
+  cache="${_DATA}/cache/${cachebase}"
+  if [ -s "$cache" -a "$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' \
+    | sed -r 's;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\\\&;g' \
+    | 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 \
+    | tee "$cache"
+  fi
+}
+
+thumblist() {
+  selectionlist \
+  | sed -n "$page,$(($page + $pagesize - 1))p" \
+  | while read line; do
+    _printVideo "$line"
+  done
+}
+
+pagecount() {
+  printf "$(( $(selectionlist | wc -l) / $pagesize + 1 ))"
+}