]> git.plutz.net Git - serve0/blob - actions/multitag.sh
c1f27c76eced67935c91751b587481bb3c8ad917
[serve0] / actions / multitag.sh
1 #!/bin/zsh
2
3 filter="$_GET[\"f\"]"
4 search="$_GET[\"s\"]"
5 order=$(echo "$_GET[\"o\"]" |egrep '^(Name|Date|Length)$')
6 page=$(echo "$_GET[\"p\"]" |egrep '^[0-9]+$')
7
8 addtag(){
9   meta="$1"
10   tag="$2"
11   
12   length=0; width=0; height=0;
13   if [ -r "$meta" ]; then
14     head -n1 "$meta" |read length width height info
15     tags="$(sed -n 2p "$meta" |sed -r 's:^\|(.*)$:\1:')"
16     description="$(sed -n '3,$p' "$meta")"
17   fi
18   
19   egrep -q "(^|.*\|)$tag(\|.*|$)" <<<"$tags" || tags="$tag|$tags"
20   
21   cat <<EOF >"$meta"
22 $length $width  $height $info
23 $tags
24 $description
25 EOF
26 }
27
28 n=0
29 if [ -n "${CONTENT_LENGTH}" -a "${CONTENT_LENGTH}" -gt 0 ]; then
30 (head -c "${CONTENT_LENGTH}"; echo)|sed 's/&/\n/g' |while read line; do
31   if expr "$line" : "tagsel=" >/dev/null; then
32     n=$(($n + 1))
33     tagsel[$n]="$(echo -e "$(sed -nr 's:(^|&)tagsel=(.*)(&|$):\2:;s:&.*$::;s:\+: :g;s:%:\\x:g;p' <<<"$line" )")"
34   elif expr "$line" : "ctag=" >/dev/null; then
35     tag="$(sed -r 's:^ctag=(.*)$:\1:;s:\+:_:g;s:%:\\x:g' <<<"$line")"
36     tag="$(echo -e "${tag}" |sed -r 's:\t| :_:g;s:^!|\r|\$|\^::g;s:\\:\\\\:g;s/</\&lt\;/g;s/>/\&gt\;/g')"
37     for i in {1..$n}; do
38       addtag "${_DATA}/meta/$tagsel[$i].meta" "$tag"
39       rm "${_DATA}/cache/$tagsel[$i].cache" 2>/dev/null
40     done
41   elif expr "$line" : 'tags=.\+' >/dev/null; then
42     tags="$(sed -r 's:^tags=(.*)$:\1:;s:\+:_:g;s:%:\\x:g' <<<"$line")"
43     tags="$(echo -e "${tags}" |sed -r 's:\t| :_:g;s:^!|\r|\$|\^::g;s:\\:\\\\:g;s/</\&lt\;/g;s/>/\&gt\;/g')"
44     for i in {1..$n}; do echo "$tags" |while read tag; do
45       addtag "${_DATA}/meta/$tagsel[$i].meta" "$tag"
46       rm "${_DATA}/cache/$tagsel[$i].cache" 2>/dev/null
47     done; done
48   fi
49 done
50 fi
51
52 rm "${_DATA}"/cache/\?* 2>/dev/null
53
54 echo -n "Location: ?o=$order&s=$search&f=$filter&p=$page#$info\n\n"