]> git.plutz.net Git - serve0/blobdiff - pages/browse.sh
styling for pagination links
[serve0] / pages / browse.sh
index 95741af55f1542e952e2c9af476a27f19365fabc..faf4e67a20977521290d3cf82d96d2ff58bdcf84 100755 (executable)
 # You should have received a copy of the GNU Affero General Public License
 # along with Serve0.  If not, see <http://www.gnu.org/licenses/>. 
 
+directory="$(invalidate "${_GET[d]}" '(.*/)?\.\.(/.*)?|' /)"
+page_link="p=browse&d=$(urlsafe "$directory")&"
+
 . $_EXEC/pages/common.sh
 
-LF='
-'
-directory="$(invalidate "${_GET[d]}" '(.*/)?\.\.(/.*)?|' /)"
-[ -d "${treeroot}/${directory#/}/.index" ] && indexed=true || indexed=false
+[ -d "${treeroot%/}${directory%/}/.index" ] && indexed=true || indexed=false
 filters="${_GET[f]}"
 debug "FILTERS: $filters"
 
@@ -53,55 +53,41 @@ category_selected(){
 }
 
 _printVideo(){
-  info="$1"
-  cache="${info%/*}/.index/${info##*/}.cache"
-  meta="${info%/*}/.index/${info##*/}.meta"
-
-  if [ "$cache" -ont "$meta" ]; then
-    cat "$cache"
-  else
-    videofile="$info"
-    thumb="${info%/*}/.index/${info##*/}.jpg"
-    [ -f "$thumb" ] || genthumb "$videofile" "$thumb"
-    [ -f "$meta"  ] || genmeta "$videofile" "$meta"
-
-    { read -r length width height filename
-      read -r tags
-    } <"$meta"
-    length=$(validate "$length" '[0-9]+' 0)
-    width=$(validate "$width" '[0-9]+' 0)
-    height=$(validate "$height" '[0-9]+' 0)
-
-    video="$(urlsafe "$info")"
-    thumb="$(urlsafe "$directory/.index/${info##*/}.jpg")"
-    linkinfo="$(urlsafe "${directory%/}/${info##*/}")"
-    htmlinfo="$(htmlsafe "${info##*/}")"
-    attrinfo="$(attribsafe "$info")"
-
-    minutes="$(printf "%d:%02d" $(($length / 60)) $(($length % 60)) )"
-
-    tee "$cache" <<VIDEOend
-      <li id="$attrinfo" class="thumb">
-        <button class="watchlink" name="${linkinfo}" formaction="?action=watch&l=${linkinfo}">
-          <img src="$thumb" alt="Preview not yet available">
-        </button>
-        <h2><a href="?action=watch&l=${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>
+  name="$1"
+
+  video="${treeroot%/}${directory%/}/${name}"
+  thumb="${treeroot%/}${directory%/}/.index/${name}.jpg"
+   meta="${treeroot%/}${directory%/}/.index/${name}.meta"
+
+  [ -f "$thumb" ] || genthumb "$video" "$thumb"
+  [ -f "$meta"  ] || genmeta "$video" "$meta"
+
+  { read -r length width height filename
+    read -r tags
+  } <"$meta"
+  read length width height <<-EOF
+       $(validate "$length $width $height" '[0-9]+ [0-9]+ [0-9]+' "0 0 0")
+       EOF
+
+  location="$(attribsafe "${directory%/}/${name}")"
+  thumblocation="$(attribsafe "${directory%/}/.index/${name}.jpg")"
+  display="$(htmlsafe "$name")"
+
+  minutes="$(printf "%d:%02d" $(($length / 60)) $(($length % 60)) )"
+
+  cat <<VIDEOend
+    <li id="${location}" class="thumb">
+      <button class="watchlink" name="${location}" formaction="?action=watch&l=${location}">
+        <img src="${thumblocation}" width="212" height="162" alt="Preview not yet available">
+      </button>
+      <h2><a href="?action=watch&l=${location}">${display}</a></h2>
+      
+      <input type="checkbox" name="tagsel" value="${location}">
+      <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 '%T@ %f\n' |sort -r |cut -d\  -f2- |egrep -i "$file_pattern";;
-    Length) sed -sn 1p "${_DATA}"/meta/*.meta |sort -n |sed -r 's;^[0-9\t]+\t;;';;
-    Name)   find "${_DATA}"/videos/ -maxdepth 1 -mindepth 1 -printf     '%f\n' |sort |egrep -i "$file_pattern";;
-  esac
 }
 
 selectionlist() {
@@ -136,47 +122,84 @@ selectionlist() {
   fi
 }
 
-thumblist() {
-  selectionlist \
-  | sed -n "$page,$(($page + $pagesize - 1))p" \
-  | while read line; do
-    _printVideo "$line"
-  done
-}
+filelist(){
+  dir="${treeroot%/}${directory%/}"
 
-pagecount() {
-  printf "$(( $(selectionlist | wc -l) / $pagesize + 1 ))"
+  case "$order" in
+    Name)
+      find -L "$dir" -mindepth 1 -maxdepth 1 -type d \! -name ".index" -print0 \
+      | sort -fz \
+      | xargs -0 printf 'directory %s\n'
+
+      find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
+      | grep -ziE "$file_pattern" \
+      | sort -fz \
+      | xargs -0 printf 'video %s\n'
+
+      find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
+      | grep -zviE "$file_pattern" \
+      | sort -fz \
+      | xargs -0 printf 'plain %s\n'
+      ;;
+    Date)
+      find -L "$dir" -mindepth 1 -maxdepth 1 -type d \! -name ".index" -print0 \
+      | xargs -0 stat -c '%Z directory %n' \
+      | sort -r |cut -c12-
+
+      find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
+      | grep -ziE "$file_pattern" \
+      | xargs -0 stat -c '%Y video %n' \
+      | sort -r |cut -c12-
+
+      find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
+      | grep -zviE "$file_pattern" \
+      | xargs -0 stat -c '%Y plain %n' \
+      | sort -r |cut -c12-
+      ;;
+    Length)
+      find -L "$dir" -mindepth 1 -maxdepth 1 -type d \! -name ".index" -print0 \
+      | sort -z \
+      | xargs -0 printf 'directory %s\n'
+
+      head -qn1 "${dir}"/.index/*.meta \
+      | sort \
+      | cut -f4- \
+      | tr \\n \\0 \
+      | xargs -0 printf 'video %s\n'
+
+      find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
+      | grep -zviE "$file_pattern" \
+      | sort -z \
+      | xargs -0 printf 'plain %s\n'
+      ;;
+    esac
 }
 
-filelist(){
+thumblist(){
   debug "Filelist with dir = $directory"
 
   [ -n "${directory#/}" ] && printf %s "<a class='file directory' href='?p=browse&d=$(urlsafe "${directory%/*}")'>..</a>"
 
-  find -L "${treeroot%/}/${directory#/}" -mindepth 1 -maxdepth 1 -type d \
-  | grep -v '.index' \
-  | sort \
-  | while read dir; do
-    name="${dir##*/}"
-    printf %s "<a class='file directory' href='?p=browse&d=$(urlsafe "${directory%/}/$name")'>$(htmlsafe "$name")</a>"
-  done
-
-  find -L "${treeroot%/}/${directory#/}" -mindepth 1 -maxdepth 1 -type f \
-  | grep -E "$file_pattern" \
-  | sort \
-  | while read -r file; do
-    name="${file##*/}"
-    $indexed && _printVideo "$file" \
-    || printf '<a class="file video" href="?action=watch&l=%s">%s</a>' \
-              "$(urlsafe "${directory%/}/$name")" "$(htmlsafe "$name")"
-  done
-
-  find -L "${treeroot%/}/${directory#/}" -mindepth 1 -maxdepth 1 -type f \
-  | grep -vE "$file_pattern" \
-  | sort \
-  | while read -r file; do
-    name="${file##*/}"
-    printf '<span class="file plain">%s</span>' "$(htmlsafe "$name")"
+  filelist \
+  | sed -n "$page,$(($page + $pagesize - 1))p" \
+  | while read -r type name; do
+    case $type in
+      directory)
+        printf '<a class="file directory" href="?p=browse&d=%s">%s</a>\n' \
+               "$(urlsafe "${directory%/}/${name##*/}")" "$(htmlsafe "${name##*/}")"
+        ;;
+      video)
+        $indexed && _printVideo "${name##*/}" \
+        || printf '<a class="file video" href="?action=watch&l=%s">%s</a>\n' \
+                  "$(urlsafe "${directory%/}/${name##*/}")" "$(htmlsafe "${name##*/}")"
+        ;;
+      plain)
+        printf '<span class="file plain">%s</span>\n' "$(htmlsafe "${name##*/}")"
+        ;;
+    esac
   done
 }
 
+pagecount() {
+  printf "$(( $(filelist | wc -l) / $pagesize + 1 ))"
+}