]> git.plutz.net Git - serve0/blob - list.sh
device width header field for stupid browsers
[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
11   if [ -d "$_DATA/$ITEM/$1" ]; then
12     printf '[a .list .dir href="%s?%s" %s]' \
13       "$path" "$(HTML "$QUERY_STRING")" "$name"
14   else
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   fi
29 }
30
31 list_directories(){
32   (cd "$_DATA/$ITEM";
33     find ./ -type d \! -name .index -mindepth 1 -maxdepth 1 \
34   ) | cut -d/ -f2- | sort
35 }
36
37 [ "$FILTER" ] && list_fex="$(
38   fex='p'
39   STRING "$FILTER^" \
40   | sed -r 's;\^;\n;g; s;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\&;g' \
41   | while read -r f; do
42     [ ! "${f#~}" ] && continue
43     [ "${f#~}" = "$f" ] \
44     && fex="/(\ttags=([^\t]*,)?)(${f})((,[^\t]*)?\t)/{${fex}}" \
45     || fex="/(\ttags=([^\t]*,)?)(${f#~})((,[^\t]*)?\t)/d; ${fex}"
46     printf '%s\n' "${fex}"
47   done \
48   | tail -n1
49 )"
50
51 list_filemeta(){
52   local meta base f fn file
53   base="$1"
54   meta="$1/.index/meta"
55   meta_dir "$_DATA/$ITEM/$base"
56
57   if [ "$FILTER" ]; then
58     sed -nr "$list_fex" "$_DATA/$ITEM/$meta"
59   elif [ "${SEARCH#!}" != "${SEARCH}" ]; then
60     grep -viE "$(STRING "${SEARCH#!}")" "$_DATA/$ITEM/$meta"
61   else
62     grep -iE "$(STRING "${SEARCH}")" "$_DATA/$ITEM/$meta"
63   fi |cut -f1,6- \
64   | while f="$(line)"; do
65     fn="$(UNSTRING "${f#*       }")"; fn="${fn%${CR}}";
66     file="$(printf '%s\n' "$_DATA/$ITEM/$base/${fn}".*)"
67     file="${file##*/}"
68     [ -e "$_DATA/$ITEM/$base/${file}" ] \
69     && printf '%s       %s\n' "${f%%    *}" "${base}/${file}"
70   done
71 }
72
73 list_index(){
74   local meta
75   (cd "$_DATA/$ITEM";
76     find ./ -path '*/.index/meta'
77   ) | while meta="$(line)"; do
78     list_filemeta "${meta%/.index/meta}"
79   done
80 }
81
82 list_items() {
83   local mode
84   mode="$(COOKIE mode |grep -m1 -xE 'index|browse' || printf browse )"
85
86   [ "$mode" = browse -a "$ITEM" ] && printf '..\n'
87
88   if   [ "$mode" = browse -a "$ORDER" = Date ]; then
89     list_directories
90     list_filemeta . |cut -f2- \
91     | xargs -rd'\n' stat -c '%Y %n' \
92     | sort -rn |cut -d/ -f2-
93   elif [ "$mode" = browse -a "$ORDER" = Name ]; then
94     list_directories
95     list_filemeta . \
96     | sort -k 2 |cut -d/ -f2-
97   elif [ "$mode" = index  -a "$ORDER" = Date ]; then
98     list_index |cut -f2- \
99     | xargs -rd'\n' stat -c '%Y %n' \
100     | sort -rn | cut -d/ -f2-
101   elif [ "$mode" = index  -a "$ORDER" = Name ]; then
102     list_index | sort -k 2 | cut -d/ -f2-
103   elif [ "$mode" = browse -a "$ORDER" = Length ]; then
104     list_directories
105     list_filemeta . \
106     | sort -n |cut -d/ -f2-
107   elif [ "$mode" = index  -a "$ORDER" = Length ]; then
108     list_index \
109     | sort -n |cut -d/ -f2-
110   fi
111 }
112
113 list_paginate() {
114   local page i c n
115   page="$(GET p |grep -xE '[0-9]+' || printf 1)"
116
117   printf '[div .itemlist '
118   while i="$(line)"; do
119     c=$((${c-0} + 1))
120     if [ $c -lt $page ]; then
121       true
122     elif [ $c -lt $((LISTSIZE + page)) ]; then
123       list_item "$i"
124     fi
125   done
126   printf ']'
127
128   printf '[div .pagination'
129   for n in $(seq 1 $((c / LISTSIZE + 1)) ); do
130     printf '[a .page href="%s" %s]' \
131       "?p=$(( (n - 1) * LISTSIZE + 1))" "$n"
132   done
133   printf ']'
134 }
135
136 printf 'Content-Type: text/html;charset=utf-8\r\n\r\n'
137
138 "$_EXEC/cgilite/html-sh.sed" <<-EOF
139 [!DOCTYPE HTML]
140 [html [head [title Listing]
141   [meta name="viewport" content="width=device-width"]
142   [link rel=stylesheet href="/style.css" ]
143 ] [body
144   [div #navigation
145     [a #t_bookmarks href="#bookmarks" &#x2605;]
146     $(w_search)
147     [a #t_avsearch href="#advsearch" Advanced]
148     [a #t_prefs href="#prefs" &#x2699;]
149   ]
150   $(w_prefs)
151   $(w_advsearch)
152
153   [form method=POST action="?a=multitag"
154     $(list_items \
155       | list_paginate
156     )
157     [div #editing
158       [a href="#multitag" Add Tags] $(w_tagging)
159       $(w_index)
160     ]
161   ]
162 ] ]
163 EOF
164