]> git.plutz.net Git - serve0/blob - list.sh
speed gain by omitting record checks (heuristic pagination)
[serve0] / list.sh
1 #!/bin/sh
2
3 . "$_EXEC/indexmeta.sh"
4 . "$_EXEC/widgets.sh"
5
6 list_item() {
7   local name path length width height tags comment n
8   name="$(HTML "$1")"
9   path="$(URL "$ITEM/$1")"
10   qry=$(HTML "$QUERY_STRING")
11
12   if [ -d "$_DATA/$ITEM/$1" ]; then
13     printf '[a .list .dir href="%s" %s]' "${path}?${qry}" "$name"
14   elif [ -f "$_DATA/$ITEM/$1" ]; then
15     read -r length width height tags comment n <<-EOF
16         $(meta_info "$_DATA/$ITEM/$1")
17         EOF
18     printf '[div .list .file
19               [a href="%s" [img src="%s?a=thumbnail"][label %s]]
20               [span .time %i:%imin] [span .dim %ix%i] %s
21               [checkbox "select" "%s" id="select_%s" form="multitag"][label for="select_%s" +]
22             ]' \
23       "$path" "$path" "$name" \
24       "$((length / 60))" "$((length % 60))" \
25       "$width" "$height" \
26       "$(UNSTRING "${tags#tags=}" |tr , '\0' |xargs -r0 printf ' [span .tag %s]')" \
27       "$path" "$path" "$path"
28   else
29     printf 'Canning record for nonexist file: %s\n' "$1" >&2
30     meta_purge "$_DATA/$ITEM/$1"
31   fi
32 }
33
34 list_directories(){
35   (cd "$_DATA/$ITEM";
36     find ./ -type d \! -name .index -mindepth 1 -maxdepth 1 \
37   ) | cut -d/ -f2- | sort
38 }
39
40 [ "$FILTER" ] && list_fex="$(
41   fex='p'
42   STRING "$FILTER^" \
43   | sed -r 's;\^;\n;g; s;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\&;g' \
44   | while read -r f; do
45     [ ! "${f#~}" ] && continue
46     [ "${f#~}" = "$f" ] \
47     && fex="/(\ttags=([^\t]*,)?)(${f})((,[^\t]*)?\t)/{${fex}}" \
48     || fex="/(\ttags=([^\t]*,)?)(${f#~})((,[^\t]*)?\t)/d; ${fex}"
49     printf '%s\n' "${fex}"
50   done \
51   | tail -n1
52 )"
53
54 list_fullname(){
55   sn="$1"
56   [ ! "${sn%%*/*}" ] && base="${sn%/*}" || base=.
57   file="$(printf '%s' "$_DATA/$ITEM/$sn".*)"
58   file="${file##*/}"
59   [ -e "$_DATA/$ITEM/$base/${file}" ] \
60   && printf '%s\n' "${base}/${file}"
61 }
62
63 list_filemeta(){
64   local meta base f fn file
65   base="$1"
66   meta="$_DATA/$ITEM/$base/.index/meta"
67   meta_dir "$_DATA/$ITEM/$base"
68
69   if [ $ORDER = Name ]; then
70     sort -k6 "$meta"
71   elif [ $ORDER = Length ]; then
72     sort -n "$meta"
73   else
74     cat "$meta"
75   fi \
76   | if [ "$FILTER" ]; then
77     sed -nr "$list_fex"
78   elif [ "${SEARCH#!}" != "${SEARCH}" ]; then
79     grep -viE "$(STRING "${SEARCH#!}")"
80   else
81     grep -iE "$(STRING "${SEARCH}")"
82   fi |cut -f6 | UNSTRING \
83   | while read -r fn; do
84     printf '%s\n' "${base}/${fn%${CR}}"
85   done
86 }
87
88 list_index(){
89   local meta
90   (cd "$_DATA/$ITEM";
91     find ./ -path '*/.index/meta'
92   ) | while meta="$(line)"; do
93     list_filemeta "${meta%/.index/meta}"
94   done
95 }
96
97 list_dateorder(){
98   while read -r sn; do 
99     list_fullname "$sn"
100   done \
101   | xargs -rd'\n' stat -c '%Y   %n' \
102   | sort -rn |cut -d/ -f2- \
103   | sed -r 's;\.[^\.]*$;;;'
104 }
105
106 list_items() {
107   local mode
108   mode="$(COOKIE mode |grep -m1 -xE 'index|browse' || printf browse )"
109
110   [ "$mode" = browse -a "$ITEM" ] && printf '..\n'
111
112   if   [ "$mode" = browse -a "$ORDER" = Date ]; then
113     list_directories
114     list_filemeta . |list_dateorder
115   elif [ "$mode" = index  -a "$ORDER" = Date ]; then
116     list_index |list_dateorder
117   elif [ "$mode" = browse ]; then
118     list_directories
119     list_filemeta .
120   elif [ "$mode" = index ]; then
121     list_index
122   fi
123 }
124
125 list_paginate() {
126   local page i c n end
127   page="$(GET p |grep -xE '[0-9]+' || printf 1)"; c=1
128   end=$((page + LISTSIZE))
129
130   printf '[div .itemlist '
131   while read -r i; do
132     [ $c -ge $page -a $c -lt $end  ] \
133     && list_item "$(list_fullname "$i")"
134     c=$((c + 1))
135   done
136   printf ']'
137
138   printf '[div .pagination'
139   for n in $( seq 1 $((c / LISTSIZE + 1)) ); do
140     printf '[a .page href="%s" %s]' \
141       "?p=$(( (n - 1) * LISTSIZE + 1))&$qry" "$n"
142   done
143   printf ']'
144 }
145
146 printf 'Content-Type: text/html;charset=utf-8\r\n\r\n'
147
148 { printf '
149 [!DOCTYPE HTML]
150 [html [head [title Listing]
151   [meta name="viewport" content="width=device-width"]
152   [link rel=stylesheet href="/style.css" ]
153 ] [body
154   [div #navigation
155     [a #t_bookmarks href="#bookmarks" &#x2605;]'
156     w_search
157     printf '
158     [a #t_avsearch href="#advsearch" Advanced]
159     [a #t_prefs href="#prefs" &#x2699;]
160   ]'
161   w_prefs
162   w_advsearch
163   printf '
164   [form method=POST action="?a=multitag"'
165     list_items \
166     | list_paginate
167     printf '
168     [div #editing
169       [a href="#multitag" Add Tags]'
170       w_tagging
171       w_index
172       printf '
173     ]
174   ]
175 ] ]
176 '; } | "$_EXEC/cgilite/html-sh.sed"