comparison s4-funcs.sh @ 326:d19e85a72a29

Allow spaces and special characters in filenames(tentative).
author HIROSE Yuuji <yuuji@gentei.org>
date Fri, 21 Oct 2016 16:22:12 +0859
parents 18be0c210dc8
children 5e56160ad1f5
comparison
equal deleted inserted replaced
325:b5185c66aacc 326:d19e85a72a29
640 val=`query "select val from $cond and type like 'file:%' order by rowid;"` 640 val=`query "select val from $cond and type like 'file:%' order by rowid;"`
641 err gvb1-sql: "select count(val) from $cond;" 641 err gvb1-sql: "select count(val) from $cond;"
642 vcount=`query "select count(val) from $cond;"` 642 vcount=`query "select count(val) from $cond;"`
643 echo $vcount > $td/$c.count 643 echo $vcount > $td/$c.count
644 i=0 644 i=0
645 err gvbid: i=$i vcount=$vcount 645 ## err gvbid: i=$i vcount=$vcount
646 while [ $i -lt $vcount ]; do 646 while [ $i -lt $vcount ]; do
647 slice="order by rowid limit 1 offset $i" 647 slice="order by rowid limit 1 offset $i"
648 i=$((i+1)) 648 i=$((i+1))
649 fn=$c.$i 649 fn=$c.$i
650 err td=$td, fn=$fn, type=$type, val="[$val]" 650 err td=$td, fn=$fn, type=$type, val="[$val]"
651 case $type in 651 case $type in
652 file:*) 652 file:*)
653 #file=$td/$val 653 #file=$td/$val
654 file=$td/`query "select val from $cond $slice;"` 654 r_f=`query "select rowid||'//'||val from $cond $slice;"`
655 f_rid=${r_f%%//*}
656 file=$td/${r_f##*//}
655 # FOR SPEED: Skip file generation if imgcache exists 657 # FOR SPEED: Skip file generation if imgcache exists
656 [ -s $file -a -s $td/$fn.rowid -a -s $file.rowid ] && continue 658 [ -s "$file" -a -s "$td/$fn.rowid" -a -s "$file.rowid" ] \
659 && [ x"$f_rid" = x"`cat $td/$fn.rowid`" ] \
660 && continue
657 # err gvbid-get="select quote(bin) from $cond $slice;" 661 # err gvbid-get="select quote(bin) from $cond $slice;"
658 sq $db<<EOF | unhexize > $file 662 ## err output: "fn=[$fn] file=[$file]"
659 .output $td/$fn.rowid 663 sq $db<<EOF | unhexize > "$file"
664 .output '$td/$fn.rowid'
660 select rowid from $cond $slice; 665 select rowid from $cond $slice;
661 .output $td/$fn 666 .output '$td/$fn'
662 select val from $cond $slice; 667 select val from $cond $slice;
663 .output $file.content-type 668 .output '$td/${fn}.content-type'
664 select substr(type, 6) from $cond $slice; 669 select substr(type, 6) from $cond $slice;
665 .output stdout 670 .output stdout
666 select quote(bin) from $cond $slice; 671 select quote(bin) from $cond $slice;
667 EOF 672 EOF
668 ## err gvbid-get2: "`ls -lF $file`" 673 ## err gvbid-get2: "`ls -lF $file`"
669 ## err i=$i - file=$file rowid=`cat $td/$fn.rowid` 674 ## err i=$i - file=$file rowid=`cat $td/$fn.rowid`
670 cp $td/$fn.rowid $file.rowid 2>&3 # for convenience 675 cp "$td/$fn.rowid" "$file.rowid" 2>&3 # for convenience
671 cp $file $file.orig 2>&3 676 cp "$file" "$file.orig" 2>&3
672 ls -lh $file | awk '{print $5"B"}'|sed 's/BB/B/' > $file.size 677 ls -lh "$file" |
673 case $type in 678 awk '{print $5"B"}'|sed 's/BB/B/' > "$file.size"
674 *:[Ii]mage*) mogrify -geometry $thumbxy $file ;; 679 case "$type" in
680 *:[Ii]mage*) mogrify -geometry $thumbxy "$file" ;;
675 ### ここのアイコンを増やしたい 681 ### ここのアイコンを増やしたい
676 *|*:[Aa]pplication*) 682 *|*:[Aa]pplication*)
677 convert -geometry $thumbxy $imgdir/file-icon.png \ 683 convert -geometry $thumbxy $imgdir/file-icon.png \
678 png:- > $file 684 png:- > "$file"
679 ;; 685 ;;
680 esac 686 esac
681 ;; 687 ;;
682 *) 688 *)
683 sq $db<<EOF 689 sq $db<<EOF
695 case $type in 701 case $type in
696 file:*) 702 file:*)
697 echo "$val" \ 703 echo "$val" \
698 | while read fn; do 704 | while read fn; do
699 file=$td/$fn 705 file=$td/$fn
700 if [ ! -s $file ]; then 706 if [ ! -s "$file" ]; then
701 ## sq $db "select quote(bin) from $cond and val=\"$fn\"" \ 707 ## sq $db "select quote(bin) from $cond and val=\"$fn\"" \
702 query "select quote(bin) from $cond and val=\"$fn\";" \ 708 query "select quote(bin) from $cond and val=\"$fn\";" \
703 | unhexize > $file 709 | unhexize > "$file"
704 echo ${type#file:} > $file.content-type 710 ##@@## -- echo ${type#file:} > "$file.content-type"
705 # err TTTTTTTTTTTTTTTT: $type
706 case $type in 711 case $type in
707 *:[Ii]mage*) mogrify -geometry $thumbxy $file ;; 712 *:[Ii]mage*) mogrify -geometry $thumbxy "$file" ;;
708 *:[Aa]pplication*) 713 *:[Aa]pplication*)
709 convert -geometry $thumbxy $imgdir/file-icon.png \ 714 convert -geometry $thumbxy $imgdir/file-icon.png \
710 png:- > $file ;; 715 png:- > $file ;;
711 esac 716 esac
712 fi 717 fi
786 hexdump -ve '1/1 "%.2x"' 791 hexdump -ve '1/1 "%.2x"'
787 } 792 }
788 hexize="hexize_hd" 793 hexize="hexize_hd"
789 fi 794 fi
790 fi 795 fi
791 cat $1 | $hexize | tr -d '\n' 796 cat "$@" | $hexize | tr -d '\n'
792 } 797 }
793 unhexize() { 798 unhexize() {
794 if [ -z "$unhex" ]; then 799 if [ -z "$unhex" ]; then
795 if type xxd >/dev/null 2>&1; then 800 if type xxd >/dev/null 2>&1; then
796 unhex="xxd -p -r" 801 unhex="xxd -p -r"
802 # avoided by creating scripts in a file where its parent directory is 807 # avoided by creating scripts in a file where its parent directory is
803 # world writable...:) 808 # world writable...:)
804 unhex="perl -n $tmpd/unhex.pl" 809 unhex="perl -n $tmpd/unhex.pl"
805 fi 810 fi
806 fi 811 fi
807 cat $1 | $unhex 812 cat "$@" | $unhex
808 # cat $1 | tee /tmp/uh.in| $unhex | tee /tmp/uh.out 813 # cat $1 | tee /tmp/uh.in| $unhex | tee /tmp/uh.out
809 } 814 }
810 percenthex() { 815 percenthex() {
811 hexize $1 | sed 's/\(..\)/%\1/g' 816 hexize "$@" | sed 's/\(..\)/%\1/g'
812 } 817 }
813 htmlescape() { 818 htmlescape() {
814 sed -e 's/\&/\&amp;/g' -e 's/"/\&quot;/g' -e "s/'/\&apos;/g" \ 819 sed -e 's/\&/\&amp;/g' -e 's/"/\&quot;/g' -e "s/'/\&apos;/g" \
815 -e "s/</\&lt;/g; s/>/\&gt;/g" 820 -e "s/</\&lt;/g; s/>/\&gt;/g"
816 } 821 }
937 case $conf in 942 case $conf in
938 */password) 943 */password)
939 type=encoded ### val=`echo $val|encode` 944 type=encoded ### val=`echo $val|encode`
940 ;; 945 ;;
941 */image*|*/document*) 946 */image*|*/document*)
942 type=`file --mime-type $val` 947 type=`file --mime-type - < "$val" | cut -d' ' -f2`
943 bin="X'`hexize $val`'" 948 bin="X'`hexize "$val"`'"
944 ;; 949 ;;
945 esac 950 esac
946 pkey=`echo "$tconfs"|grep "${t0}/.*=p"|sed 1q` 951 pkey=`echo "$tconfs"|grep "${t0}/.*=p"|sed 1q`
947 pkey=${pkey#/*/} # cut $tbl/ 952 pkey=${pkey#/*/} # cut $tbl/
948 pkey=${pkey%=p/*} # cut =p/... -> primary key 953 pkey=${pkey%=p/*} # cut =p/... -> primary key
1149 v="`echo ${us#*=}|unhexize|sed -e 's/\"/\"\"/g'`" 1154 v="`echo ${us#*=}|unhexize|sed -e 's/\"/\"\"/g'`"
1150 # err k=$k v=$v 1155 # err k=$k v=$v
1151 case "$k" in 1156 case "$k" in
1152 *:filename) 1157 *:filename)
1153 type='file'; k=${k%:filename} 1158 type='file'; k=${k%:filename}
1154 (echo k=$k; ls -lF $tmpd/$v; file --mime-type $tmpd/$v) 1>&3 1159 # DO NOT ALLOW Space and '|' in file names
1155 case `file --mime-type $tmpd/$v|cut -d' ' -f2` in 1160 newv=`echo "$v"|sed 's/[ \|]/X/g'`
1161 if [ x"$v" != x"$newv" ]; then
1162
1163 fi
1164 # (echo k=$k v="[$v]"; ls -lF "$tmpd/$v"; file --mime-type "$tmpd/$v") 1>&3
1165 case `file --mime-type - < "$tmpd/$v"|cut -d' ' -f2` in
1156 [Ii]mage/x-xcf) 1166 [Ii]mage/x-xcf)
1157 bzip2 $tmpd/$v 1167 bzip2 "$tmpd/$v"
1158 v=${v}.bz2 1168 v=${v}.bz2
1159 ;; 1169 ;;
1160 [Ii]mage/x-*|*/vnd.*) ;; 1170 [Ii]mage/x-*|*/vnd.*) ;;
1161 [Ii]mage/*) 1171 [Ii]mage/*)
1162 mogrify -resize $maximagexy'>' $tmpd/$v 1172 mogrify -resize $maximagexy'>' "$tmpd/$v"
1163 ;; 1173 ;;
1164 esac 1174 esac
1165 ;; 1175 ;;
1166 *) 1176 *)
1167 type='string' 1177 type='string'
1532 ## b is much smaller than a 1542 ## b is much smaller than a
1533 cat $tmpf | unhexize \ 1543 cat $tmpf | unhexize \
1534 | tee $fifo \ 1544 | tee $fifo \
1535 | convert -define ${fmt}:size=${iconxy_M}x${iconxy_M} \ 1545 | convert -define ${fmt}:size=${iconxy_M}x${iconxy_M} \
1536 -resize ${iconxy_M}x${iconxy_M}'>' - pnm:- \ 1546 -resize ${iconxy_M}x${iconxy_M}'>' - pnm:- \
1537 | convert - $cacheimg_M & 1547 | convert - "$cacheimg_M" &
1538 cat $fifo | convert -define ${fmt}:size=${iconxy_S}x${iconxy_S} \ 1548 cat $fifo | convert -define ${fmt}:size=${iconxy_S}x${iconxy_S} \
1539 -resize ${iconxy_S}x${iconxy_S}'>' - pnm:- \ 1549 -resize ${iconxy_S}x${iconxy_S}'>' - pnm:- \
1540 | convert - $cacheimg_S & 1550 | convert - "$cacheimg_S" &
1541 printf '%s' "<img src=\"data:${filetype}," 1551 printf '%s' "<img src=\"data:${filetype},"
1542 cat $tmpf | sed 's/\(..\)/%\1/g' 1552 cat $tmpf | sed 's/\(..\)/%\1/g'
1543 echo '">' 1553 echo '">'
1544 echo "$sum" > $sumfile 1554 echo "$sum" > $sumfile
1545 fi 1555 fi
1763 done 1773 done
1764 echo '</div>' 1774 echo '</div>'
1765 } 1775 }
1766 iconhref() ( 1776 iconhref() (
1767 # $1=icon-file, $2=Href $3=title $4...=anchor 1777 # $1=icon-file, $2=Href $3=title $4...=anchor
1768 data=`percenthex $1` 1778 data=`percenthex "$1"`
1769 ct=`file --mime-type $1|cut -d' ' -f2` 1779 ct=`file --mime-type - < "$1"|cut -d' ' -f2`
1770 err iconhref: \$1=$1 \$2=$2 \$3="$@" 1780 err iconhref: \$1=$1 \$2=$2 \$3="$@"
1771 href=$2; title=$3; shift 3 1781 href=$2; title=$3; shift 3
1772 echo "<a href=\"$href\"><img title=\"$title\" src=\"data:$ct,$data\">$@</a>" 1782 echo "<a href=\"$href\"><img title=\"$title\" src=\"data:$ct,$data\">$@</a>"
1773 ) 1783 )
1774 iconhref2() ( 1784 iconhref2() (
2965 image|document|binary) 2975 image|document|binary)
2966 file=$tmpd/`getparfilename $v` 2976 file=$tmpd/`getparfilename $v`
2967 err type=file=$file 2977 err type=file=$file
2968 [ -z "$file" ] && continue 2978 [ -z "$file" ] && continue
2969 bn=`sqlquotestr "${file##*/}"` 2979 bn=`sqlquotestr "${file##*/}"`
2970 bin="X'"$(hexize $file)"'" 2980 bin="X'"$(hexize "$file")"'"
2971 ct=`file --mime-type $file|cut -d' ' -f2` 2981 ct=`file --mime-type - < "$file" |cut -d' ' -f2`
2972 type=\"file:$ct\" 2982 type=\"file:$ct\"
2973 newsql="update $tb2 set val=$bn, type=$type, bin=$bin" 2983 newsql="update $tb2 set val=$bn, type=$type, bin=$bin"
2974 cachedir=`getcachedir "$tbl/$rowid"` 2984 cachedir=`getcachedir "$tbl/$rowid"`
2975 err getcache tbl/rowid=$tbl/$rowid, rm -r $cachedir 2985 err getcache tbl/rowid=$tbl/$rowid, rm -r $cachedir
2976 rm -r $cachedir 2986 rm -rf $cachedir
2977 ;; 2987 ;;
2978 *) 2988 *)
2979 newsql="update $tb2 set val=(select val from par where var \ 2989 newsql="update $tb2 set val=(select val from par where var \
2980 like '$col.%.$st_rowid')" 2990 like '$col.%.$st_rowid')"
2981 ;; 2991 ;;
3018 err partype$col=`getpartype $col "$limit"` 3028 err partype$col=`getpartype $col "$limit"`
3019 case $ptype in 3029 case $ptype in
3020 file) file=$tmpd/`getparfilename $col "$limit"` 3030 file) file=$tmpd/`getparfilename $col "$limit"`
3021 err parfile-$col=$file 3031 err parfile-$col=$file
3022 [ -z "$file" ] && continue 3032 [ -z "$file" ] && continue
3023 bin="X'"$(hexize $file)"'" 3033 bin="X'"$(hexize "$file")"'"
3024 ct=`file --mime-type $file|cut -d' ' -f2` 3034 ct=`file --mime-type - < "$file"|cut -d' ' -f2`
3025 type=\"file:$ct\" ;; 3035 type=\"file:$ct\" ;;
3026 "*"*) continue ;; # foreign table 3036 "*"*) continue ;; # foreign table
3027 *) type=\"string\" ;; 3037 *) type=\"string\" ;;
3028 esac 3038 esac
3029 case `gettbl_coltype $tbl/$col` in 3039 case `gettbl_coltype $tbl/$col` in
3117 if [ -n "$rowid" ]; then 3127 if [ -n "$rowid" ]; then
3118 # err genform2a: Seeking for "$2.$name, type=$type" 3128 # err genform2a: Seeking for "$2.$name, type=$type"
3119 rawval=`getvalbyid $2 $name $rowid $td` 3129 rawval=`getvalbyid $2 $name $rowid $td`
3120 val=`echo "$rawval"|htmlescape` 3130 val=`echo "$rawval"|htmlescape`
3121 err genform3a: getvalbyid $2 $name $rowid $td 3131 err genform3a: getvalbyid $2 $name $rowid $td
3122 err genform3b: val="[$val]" 3132 err genform3b: val="[$val]" type="$type"
3123 fi 3133 fi
3124 if [ -n "$GF_VIEWONLY" ]; then 3134 if [ -n "$GF_VIEWONLY" ]; then
3125 is_hidden "$2" "$name" && continue 3135 is_hidden "$2" "$name" && continue
3126 fi 3136 fi
3127 case "$type" in 3137 case "$type" in
3159 fi 3169 fi
3160 form="$fh$form</select>" 3170 form="$fh$form</select>"
3161 ;; 3171 ;;
3162 [Ii][Mm][Aa][Gg][Ee]|[Dd][Oo][Cc][Uu][Mm][Ee][Nn][Tt]|[Bb]inary) 3172 [Ii][Mm][Aa][Gg][Ee]|[Dd][Oo][Cc][Uu][Mm][Ee][Nn][Tt]|[Bb]inary)
3163 if [ -s $td/$name.count ]; then 3173 if [ -s $td/$name.count ]; then
3174 err Calling cgi_multi_file: "nae=[$name] td=[$td] args=[$args]"
3164 form=`cgi_multi_file $name $td "$args"` 3175 form=`cgi_multi_file $name $td "$args"`
3165 if [ -n "$val" ]; then 3176 if [ -n "$val" ]; then
3166 hrfb="$myname?showattc+$2_m" 3177 hrfb="$myname?showattc+$2_m"
3167 val=$(echo "$rawval" \ 3178 val=$(echo "$rawval" \
3168 | while read fn; do 3179 | while read fn; do
3169 data=`percenthex $td/$fn` 3180 data=`percenthex "$td/$fn"`
3170 #ct=`cat $td/$fn.content-type` 3181 #ct=`cat $td/$fn.content-type`
3171 ct=`file --mime-type $td/$fn|cut -d' ' -f2` 3182 ct=`file --mime-type - < "$td/$fn"|cut -d' ' -f2`
3172 ri=`cat $td/$fn.rowid` 3183 ri=`cat "$td/$fn.rowid"`
3173 ## err fn=$fn, name=$name, ri=$ri; ls -lF $td 1>&3 3184 err fn=$fn, name=$name, ri=$ri; ls -lF "$td/" 1>&3
3174 #imgsrc="<img src=\"data:$ct,$data\">" 3185 #imgsrc="<img src=\"data:$ct,$data\">"
3175 #echo "<a href=\"$hrfb+$ri\">$imgsrc</a><br>" 3186 #echo "<a href=\"$hrfb+$ri\">$imgsrc</a><br>"
3176 iconhref $td/$fn "$hrfb+$ri" "" 3187 iconhref "$td/$fn" "$hrfb+$ri" ""
3177 done) 3188 done)
3178 fi 3189 fi
3179 else 3190 else
3180 form="<input type=\"file\" name=\"$name\" $args>" 3191 form="<input type=\"file\" name=\"$name\" $args>"
3181 if [ -n "$val" ]; then 3192 if [ -n "$val" ]; then
3182 imgs=$(echo "$val"\ 3193 imgs=$(echo "$rawval"\
3183 |while read fn;do 3194 |while read fn;do
3184 data=`percenthex $td/$fn` 3195 data=`percenthex "$td/$fn"`
3185 echo "<img src=\"data:image/png,$data\">$fn<br>" 3196 echo "<img src=\"data:image/png,$data\">$fn<br>"
3186 done) 3197 done)
3187 form=$form"<br>$imgs" 3198 form=$form"<br>$imgs"
3188 val=$imgs # 2015-06-15 3199 val=$imgs # 2015-06-15
3189 else 3200 else
3284 # tmpfiles=$tmpfiles"${tmpfiles+ }$idir" 3295 # tmpfiles=$tmpfiles"${tmpfiles+ }$idir"
3285 bin=$idir/$myname-$$.bin 3296 bin=$idir/$myname-$$.bin
3286 sql="select quote(bin) from $1 where rowid='$2';" 3297 sql="select quote(bin) from $1 where rowid='$2';"
3287 err showattc: sql: $sql 3298 err showattc: sql: $sql
3288 sq $db "$sql" | unhexize > $bin 3299 sq $db "$sql" | unhexize > $bin
3289 tv=`query "select type,val from $1 where rowid='$2';"` 3300 tv=`query "select type||'//'||val from $1 where rowid='$2';"`
3290 type=${tv%\|*} fn=${tv#*\|} 3301 type=${tv%//*} fn=${tv#*//}
3291 err tv=$tv type=$type fn=$fn, tp2=${tv%\|*} 3302 err tv=$tv type=$type fn=$fn, tp2=${tv%\|*}
3292 ct=${type#file:} 3303 ct=${type#file:}
3293 case $ct in # all text/* changed to text/plain 3304 case $ct in # all text/* changed to text/plain
3294 text/*) 3305 text/*)
3295 charset=`nkf -g $bin|cut -d' ' -f1` 3306 charset=`nkf -g $bin|cut -d' ' -f1`

yatex.org