]> git.plutz.net Git - serve0/blob - list.sh
page caching
[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 "$ITEM/$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/$ITEM/$name" ]; then
28     display="$(HTML "$name")"
29     link="$(URL "$ITEM/$name")"
30     printf '[div .list .file
31               [a href="%s" [img src="%s?a=thumbnail"]][label %s]
32               [span .time %i:%imin] [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=}" |tr , '\0' |xargs -r0 printf ' [span .tag %s]')" \
39       "$link" "$link" "$link"
40   else
41     printf 'Canning record for nonexist file: %s\n' "$name" >&2
42     # meta_purge "$_DATA/$ITEM/$name"
43   fi
44 }
45
46
47 [ "$FILTER" ] && list_fex="$(
48   fex='p'
49   STRING "$FILTER^" \
50   | sed -r 's;\^;\n;g; s;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\&;g' \
51   | while read -r f; do
52     [ ! "${f#~}" ] && continue
53     [ "${f#~}" = "$f" ] \
54     && fex="/(\ttags=([^\t]*,)?)(${f})((,[^\t]*)?\t)/{${fex}}" \
55     || fex="/(\ttags=([^\t]*,)?)(${f#~})((,[^\t]*)?\t)/d; ${fex}"
56     printf '%s\n' "${fex}"
57   done \
58   | tail -n1
59 )"
60
61 list_fullname(){
62   sn="$1"
63   [ ! "${sn%%*/*}" ] && base="${sn%/*}" || base=.
64   file="$(printf '%s' "$_DATA/$ITEM/$sn".*)"
65   file="${file##*/}"
66   [ -e "$_DATA/$ITEM/$base/${file}" ] \
67   && printf '%s\n' "${base}/${file}"
68 }
69
70 list_filter(){
71   if [ "$FILTER" ]; then
72     sed -nr "$list_fex"
73   elif [ "${SEARCH#!}" != "${SEARCH}" ]; then
74     grep -aviEe "$(STRING "${SEARCH}" \
75                  | sed -r ':x s;((^|[^\\])(\\\\)*)\+;\1 ;g; tx;
76                             s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
77                             s; ;\\+;g;')"
78   elif [ "${SEARCH}" ]; then
79     grep -aiEe "$(STRING "${SEARCH}" \
80                  | sed -r ':x s;((^|[^\\])(\\\\)*)\+;\1 ;g; tx;
81                             s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
82                             s; ;\\+;g;')"
83   else
84     cat
85   fi
86 }
87
88 list_order(){
89   local fm fn fn
90
91   if [ $ORDER = Name ]; then
92     sort -k6 |sed 's;^;metashort\t;;'
93   elif [ $ORDER = Length ]; then
94     sort -n -k1 |sed 's;^;metashort\t;;'
95   elif [ $ORDER = Date ]; then
96     while read -r fm; do
97       sn="${fm##*       }"
98       fn="$(list_fullname "$(UNSTRING "${sn%${CR}}")")"
99       printf '%i        %s      %s\n' \
100              "$(stat -c %Y "$fn")" "${fm%       *}" "$fn"
101     done \
102     | sort -n -k1 |cut -f2- |sed 's;^;metalong\t;;'
103   fi
104 }
105
106 list_filemeta(){
107   local meta base cbase fm cachename
108   base="$1"
109   meta="$_DATA/$ITEM/$base/.index/meta"
110   meta_dir "$_DATA/$ITEM/$base"
111
112   cachename="$(printf '%s\n' "$mode" "$FILTER" "$SEARCH" "$ORDER" |sha1sum)"
113   cachename="$_DATA/$ITEM/.index/${cachename%  -}.cache"
114
115   if [ "$cachename" -nt "$meta" ] 2>&-; then
116     cat "$cachename"
117   else
118     cbase="$(STRING "$base")"
119     grep -axE '[0-9]+   [0-9]+  [0-9]+  tags=[^ ]*      comment=[^      ]*      .+' "$meta" \
120     | while read -r fm; do
121       printf '%s        %s/%s\n' "${fm% *}" "$cbase" "${fm##*   }"
122     done \
123     | list_filter \
124     | list_order \
125     | { [ -d "${meta%meta}" ] && tee "$cachename" || cat; }
126   fi
127 }
128
129 list_items() {
130   local mode meta
131   mode="$(COOKIE mode |grep -m1 -axE 'index|browse' || printf browse )"
132   
133   if [ "$mode" = browse ]; then
134     [ "$ITEM" ] && printf 'dir\t..\n'
135     (cd "$_DATA/$ITEM";
136       find ./ -type d \! -name .index -mindepth 1 -maxdepth 1 \
137     ) | cut -d/ -f2- | sort |sed 's;^;dir\t;;'
138     list_filemeta .
139   elif [ "$mode" = index ]; then
140     (cd "$_DATA/$ITEM";
141       find ./ -path '*/.index/meta'
142     ) | while read -r meta; do
143       list_filemeta "${meta%/.index/meta}"
144     done
145   fi
146 }
147
148 list_paginate() {
149   local page i c n end qry
150   page="$(GET p |grep -axE '[0-9]+' || printf 1)"; c=1
151   end=$((page + LISTSIZE))
152   qry="${w_refuri#*\?}"; qry="${qry#p=*&}"
153
154   printf '[div .itemlist '
155   while read -r i; do
156     c=$((c + 1))
157     [ $c -gt $page -a $c -le $end  ] && list_item "$i"
158   done
159   printf ']'
160
161   printf '[div .pagination'
162   for n in $( seq 1 $((c / LISTSIZE + 1)) ); do
163     printf '[a .page href="%s" %s]' \
164       "?p=$(( (n - 1) * LISTSIZE + 1))&${qry}" "$n"
165   done
166   printf ']'
167 }
168
169 printf 'Content-Type: text/html;charset=utf-8\r\n\r\n'
170
171 { printf '
172 [!DOCTYPE HTML]
173 [html [head [title Listing]
174   [meta name="viewport" content="width=device-width"]
175   [link rel=stylesheet href="/style.css" ]
176 ] [body
177   [div #navigation
178     [a #t_bookmarks href="#bookmarks" ★]'
179     w_search
180     printf '
181     [a #t_avsearch href="#advsearch" Advanced]
182     [a #t_prefs href="#prefs" ⚙]
183   ]'
184   w_bookmarks
185   w_advsearch
186   w_prefs
187   printf '
188   [form method=POST action="?a=multitag"'
189     list_items \
190     | list_paginate
191     printf '
192     [div #editing
193       [a href="#multitag" Add Tags]'
194       w_tagging
195       w_index
196       printf '
197     ]
198   ]
199 ] ]
200 '; } | "$_EXEC/cgilite/html-sh.sed"