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