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