]> git.plutz.net Git - serve0/blob - list.sh
Merge commit 'efaa2a8db2857d130e598104aea0fdbab398f714'
[serve0] / list.sh
1 #!/bin/sh
2
3 . "$_EXEC/indexmeta.sh"
4 . "$_EXEC/widgets.sh"
5
6 list_item() {
7   local meta type length width height tags comment name display link
8   meta="${1}"; type="${meta%%   *}"; meta="${meta#*     }"
9
10   if [ "$type" = dir ]; then
11     name="${meta%%      *}";
12     display="$(HTML "$name")"; link="$(URL "${PATH_INFO%/}/$name")"
13     printf '[a .list .dir href="%s" %s]' "${link}?${w_refuri#*\?}" "$name"
14     return 0
15   fi
16
17   length="${meta%%      *}"; meta="${meta#*     }"
18   width="${meta%%       *}"; meta="${meta#*     }"
19   height="${meta%%      *}"; meta="${meta#*     }"
20   tags="${meta%%        *}"; meta="${meta#*     }"
21   comment="${meta%%     *}"; meta="${meta#*     }"
22   name="${meta%%        *}"; meta="${meta#*     }"
23
24   if [ "$type" = metashort ]; then
25     name="$(list_fullname "$(UNSTRING "${name%${CR}}")")"
26   fi
27   if [ -f "$_DATA/${PATH_INFO%/}/$name" ]; then
28     link="$(URL "${PATH_INFO%/}/${name#./}")"
29     name="$(HTML "${PATH_INFO%/}/${name#./}")"
30     printf '[div .list .file
31               [a href="%s" [img src="%s?a=thumbnail"]][label . %s]
32               [span .time %i:%02imin] [span .dim %ix%i] %s
33               [checkbox "select" "%s" id="select_%s"][label for="select_%s" +]
34             ]' \
35       "$link" "$link" "${name##/}" \
36       "$((length / 60))" "$((length % 60))" \
37       "$width" "$height" \
38       "$(UNSTRING "${tags#tags=}" \
39          | sed -r 's;^;,;; s;,+;,;g; s;,$;;;
40                    :X s;,-?([^,]+)(,|$); [span .tag\n \1]\2;; tX;'
41       )" "$name" "$link" "$link"
42   else
43     debug "Canning record for nonexist file: $name"
44     meta_purge "$_DATA/$ITEM/$name"
45   fi
46 }
47
48
49 [ "$FILTER" ] && list_fex="$(
50   fex='p'
51   STRING "$FILTER^" \
52   | sed -E 's;\^;\n;g; s;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\&;g' \
53   | while read -r f; do
54     [ "${f##*[A-Z]*}" ] && tl="y;ABCDEFGHIJKLMNOPQRSTUVWXYZ;abcdefghijklmnopqrstuvwxyz;;"
55     case $f in
56       ''|~) continue;;
57       ~\\\$:*) fex="h; ${tl} /${f#~\\\$:}/d; g;${fex}";;
58       \\\$:*) fex="h; ${tl} /${f#\\\$:}/{g;${fex}}";;
59       ~*) fex="/(\ttags=([^\t]*,)?)(${f#\~})((,[^\t]*)?\t)/d; ${fex}";;
60        *) fex="/(\ttags=([^\t]*,)?)(${f})((,[^\t]*)?\t)/{${fex}}";;
61     esac
62     printf '%s\n' "${fex}"
63   done \
64   | tail -n1
65 )"
66
67 list_fullname(){
68   sn="$1"
69   [ ! "${sn%%*/*}" ] && base="${sn%/*}" || base=.
70   file="$(printf '%s' "$_DATA/$ITEM/$sn".*)"
71   file="${file##*/}"
72   [ -e "$_DATA/$ITEM/$base/${file}" ] \
73   && printf '%s\n' "${base}/${file}"
74 }
75
76 list_filter(){
77   if [ "$FILTER" ]; then
78     debug "FEX:" "$list_fex"
79     sed -nE "$list_fex"
80   elif [ "${SEARCH#!}" != "${SEARCH}" ]; then
81     grep -aviEe "$(STRING "${SEARCH}" \
82                  | sed -E ':x s;((^|[^\\])(\\\\)*)\+;\1 ;g; tx;
83                             s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
84                             s; ;\\+;g;')"
85   elif [ "${SEARCH}" ]; then
86     grep -aiEe "$(STRING "${SEARCH}" \
87                  | sed -E ':x s;((^|[^\\])(\\\\)*)\+;\1 ;g; tx;
88                             s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
89                             s; ;\\+;g;')"
90   else
91     cat
92   fi
93 }
94
95 list_order(){
96   local fm fn fn al length ln h w t c name buffer l
97
98   if [ $ORDER = Name ]; then
99     sort -k6 |sed 's;^;metashort\t;;'
100   elif [ $ORDER = Group ]; then
101     sed -E '
102       :X
103       s;^([^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^ 0-9]*)-([0-9a-zA-Z_-]{11}\r|ph[0-9a-f]{13}\r|[0-9]{8}\r)(.*)$;\1-\r\3 \2\n;;
104       t;
105       s;^([^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^ 0-9]*)([0-9]+)(.*)$;\1\r\3 \2;;
106       tX;' \
107     | { sort -n -k7 -k8 -k9 -k10 -k11 |sort -s -k6,6 ; echo '0 0 0 tags= comment= _'; } \
108     | while read -r length h w t c name; do
109       if [ "${ln%% *}" = "${name%% *}" ]; then
110         al=$((al + length))
111         buffer="${buffer}${BR}$length   $h      $w      $t      $c      $name"
112       else
113         printf '%s\n' "$buffer" |while read -r l; do
114           [ "$l" ] && printf '%s        %s\n' "$al" "$l"
115         done
116         al="$length"
117         buffer="$length $h      $w      $t      $c      $name"
118       fi
119       ln="$name"
120     done \
121     | sort -s -n -k1,1 |sed -E 's;^[0-9]+\t;metashort\t;;' \
122     | sed -E ':X s;^([^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\r]*)\r([^ ]*) ([^ ]+)( .*)?$;\1\3\2\4;; tX'
123   elif [ $ORDER = Length ]; then
124     sort -sn -k1 |sed 's;^;metashort\t;;'
125   elif [ $ORDER = Date ]; then
126     while read -r fm; do
127       sn="${fm##*       }"
128       fn="$(list_fullname "$(UNSTRING "${sn%${CR}}")")"
129       printf '%i        %s      %s\n' \
130              "$(stat -c %Y "$fn")" "${fm%       *}" "$fn"
131     done \
132     | sort -srn -k1 |sed -E 's;^[0-9]+\t;metalong\t;;'
133   fi
134 }
135
136 list_filemeta(){
137   local meta base cbase fm cachename
138   base="$1"
139   meta="$_DATA/$ITEM/$base/.index/meta"
140   meta_dir "$_DATA/$ITEM/$base"
141
142   cachename="$(printf '%s\n' "$mode" "$FILTER" "$SEARCH" "$ORDER" |sha1sum)"
143   cachename="$_DATA/$ITEM/.index/${cachename%  -}.cache"
144
145   if [ "$cachename" -nt "$meta" ] 2>&-; then
146     cat "$cachename"
147   else
148     cbase="$(STRING "$base")"
149     grep -axE '[0-9]+   [0-9]+  [0-9]+  tags=[^ ]*      comment=[^      ]*      .+' "$meta" \
150     | while read -r fm; do
151       printf '%s        %s/%s\n' "${fm% *}" "$cbase" "${fm##*   }"
152     done \
153     | list_filter \
154     | list_order \
155     | { [ -d "${meta%meta}" ] && tee "$cachename" || cat; }
156   fi
157 }
158
159 list_items() {
160   local mode meta
161   mode="$(COOKIE mode |grep -m1 -axE 'index|browse' || printf index )"
162   
163   if [ "$mode" = browse ]; then
164     [ "$ITEM" ] && printf 'dir\t..\n'
165     (cd "$_DATA/$ITEM";
166       find ./ -type d \! -name .index -mindepth 1 -maxdepth 1 \
167     ) | cut -d/ -f2- | sort |sed 's;^;dir\t;;'
168     list_filemeta .
169   elif [ "$mode" = index ]; then
170     (cd "$_DATA/$ITEM";
171       find ./ -path '*/.index/meta'
172     ) | while read -r meta; do
173       list_filemeta "${meta%/.index/meta}"
174     done
175   fi
176 }
177
178 list_paginate() {
179   local page i c n end qry
180   page="$(GET p |grep -axE '[0-9]+' || printf 1)"; c=1
181   end=$((page + LISTSIZE))
182   qry="${w_refuri#*\?}"; qry="${qry#p=*&}"
183
184   printf '[div .itemlist '
185   while read -r i; do
186     c=$((c + 1))
187     [ $c -gt $page -a $c -le $end  ] && list_item "$i"
188   done
189   printf ']'
190
191   [ $(( c % LISTSIZE )) -gt 0 ] \
192   && end=$((c / LISTSIZE + 1)) \
193   || end=$((c / LISTSIZE))
194
195   printf '[div .pagination'
196   for n in $( seq 1 $end ); do
197     c=$(( (n - 1) * LISTSIZE + 1 ))
198     [ $c = $page ] \
199     && printf '[a .page .current href="%s" %s]' "?p=${c}&${qry}" "$n" \
200     || printf '[a .page href="%s" %s]' "?p=${c}&${qry}" "$n"
201   done
202   printf ']'
203 }
204
205 printf 'Content-Type: text/html;charset=utf-8\r\n\r\n'
206
207 { printf '
208 [!DOCTYPE HTML]
209 [html [head [title '
210   w_bmname
211   printf ' by %s]' "$ORDER"
212   printf '
213   [meta name="viewport" content="width=device-width"]
214   [link rel=stylesheet href="/cgilite/common.css" ]
215   [link rel=stylesheet href="/style.css" ]
216 ] [body
217   [div #navigation
218     [a #t_bookmarks href="#bookmarks" ★]'
219     w_search
220     printf '
221     [a #t_prefs href="#prefs" ⚙]
222   ]'
223   w_bookmarks
224   w_advsearch
225   w_prefs
226   printf '
227   [form method=POST action="?a=multitag"'
228     list_items \
229     | list_paginate
230     [ -d "$_DATA/$ITEM/.index" ] && { printf '
231     [div #editing'
232       w_tagging
233     printf '
234     ]'; }
235   printf '
236   ]'
237   [ ! -d "$_DATA/$ITEM/.index" ] && { printf '
238   [div #editing'
239     w_index
240   printf '
241   ]'; }
242   printf '
243 ] ]
244 '; } | "$_EXEC/cgilite/html-sh.sed"