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