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