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