diff s4-funcs.sh @ 322:18be0c210dc8

First implementation of icon display in blog_replies.
author HIROSE Yuuji <yuuji@gentei.org>
date Thu, 20 Oct 2016 17:20:58 +0859
parents a868f03970b5
children d19e85a72a29
line wrap: on
line diff
--- a/s4-funcs.sh	Mon Oct 17 09:54:21 2016 +0859
+++ b/s4-funcs.sh	Thu Oct 20 17:20:58 2016 +0859
@@ -25,7 +25,8 @@
 dumpcollen=22
 #thumbxy=120x120
 thumbxy=96x96
-maxiconxy=400x400
+iconxy_S=80x80
+iconxy_M=400x400
 maximagexy=1600x1600
 ### maximagexy=400x400
 file_accept='accept="image/*,text/*,audio/*,application/vnd.oasis.*,application/pdf,application/x-*"'
@@ -36,6 +37,7 @@
 conftbl=_tblconf
 nl="
 "
+iconcachekey="profimgcache_S"
 . ./s4-cgi.sh
 
 : <<EOF
@@ -755,6 +757,16 @@
   fi
   $sha1 "$@" | cut -d' ' -f1
 }
+enjpeg() {
+  if [ -z "$cjpeg" ]; then
+    if type cjpeg >/dev/null 2>&1; then
+      cjpeg="cjpeg"
+    else
+      cjpeg="convert - jpeg:-"
+    fi
+  fi
+  $cjpeg "$@"
+}
 mycrypt() (
   key=$1 salt=$2
 err \$2=$2
@@ -1481,33 +1493,71 @@
 
 imgsrc_cache() (
   # $1 = directory for cache'ing
-  # $2 = table
-  # $3 = condition for choosingowner
-  dir="$1" tbl="$2" whos="$3 ORDER BY rowid LIMIT 1"
+  # $2 = table (user_m or grp_m)
+  # $3 = keycond (was: condition for choosingowner)
+  # $4 = size : S = Small, M = Medium, O = Original
+  dir="$1" tbl="$2"
+  keycond="$3"
+  whos="$keycond AND key='profimg' AND type LIKE 'file:image%'
+	 ORDER BY rowid LIMIT 1"
   [ -d "$dir" ] || mkdir $dir
+  case "$4" in
+    [Ss]) size=S ;;
+    [Oo]) size=O ;;
+    *)    size=M ;;
+  esac
+  # ImageCache filename storing schema:
+  # <table_s>.{key, val}={"profimgcache_S", "$cacheimg_S"}
   sql0="SELECT val || '//' || type FROM $tbl WHERE $whos;"
   sql1="SELECT hex(bin) FROM $tbl WHERE $whos;"
   valtype=`query "$sql0"`
   filename=${valtype%%//*}
   filetype=${valtype##*//file:}
-  cacheimg=$dir/$filename
-  sumfile="$cacheimg.sum"
+  cacheimg_S=$dir/S_$filename
+  cacheimg_M=$dir/M_$filename
+  cacheimg_O=$dir/$filename
+  cacheimg=$dir/${size}_$filename
+  sumfile="$dir/$filename.sum"
   tmpf=$tmpd/imgsrc_cache.$$
   sum=`query "$sql1" | tee $tmpf | encode`	# encode() is maybe sha1
-  if [ x"`cat $sumfile`" = x"$sum" ]; then
+  if test -s "$sumfile" && [ x"`cat $sumfile`" = x"$sum" ]; then
     # if cache is fresh,
     echo "<img src=\"$cacheimg\">"
   else
+    fifo=$tmpd/fifo; mkfifo $fifo
     fmt=${filename##*.}
+    ## [[ NOTE ]]
+    ## a. convert oldimage newimage
+    ## b. convert oldimage fmt:- | convert - newimage
+    ## b is much smaller than a
     cat $tmpf | unhexize \
-	| convert -define ${fmt}:size=${maxiconxy}x${maxiconxy} \
-		  -resize ${maxiconxy}x${maxiconxy}'>' - $cacheimg &
+	| tee $fifo \
+	| convert -define ${fmt}:size=${iconxy_M}x${iconxy_M} \
+		  -resize ${iconxy_M}x${iconxy_M}'>' - pnm:- \
+	| convert - $cacheimg_M &
+    cat $fifo | convert -define ${fmt}:size=${iconxy_S}x${iconxy_S} \
+			-resize ${iconxy_S}x${iconxy_S}'>' - pnm:- \
+	| convert - $cacheimg_S &
     printf '%s' "<img src=\"data:${filetype},"
     cat $tmpf | sed 's/\(..\)/%\1/g'
     echo '">'
     echo "$sum" > $sumfile
   fi
+  ## Now preparing cache image, done.
+  ## Store this information to DB
+  stbl=${tbl%_m}_s		# user_s or grp_s
+  pkey=${keycond%%=*}		# Primary Key name
+  pval=${keycond#*=}		# Primary Key value
+  query <<-EOF
+	REPLACE INTO $stbl($pkey, key, type, val)
+	VALUES($pval, '$iconcachekey', 'string', `sqlquote "$cacheimg_S"`);
+	EOF
 )
+imgsrc_cache_clear() {
+  # $1=table
+  # $2=keycond
+  query "DELETE FROM $1 WHERE $2 AND key='$iconcachekey';"
+}    
 
 showhome() {
   # $1=userRowIdToShow
@@ -1553,8 +1603,8 @@
   if [ -n "$img" ]; then
     if true; then
       tbl=user_m
-      enticond="name='$uname' AND key='profimg' AND type LIKE 'file:image%'"
-      imgsrc_cache "$td/main" user_m "$enticond"
+      enticond="name='$uname'"
+      imgsrc_cache "$td/main" user_m "$enticond" M
     else
     { printf '%s' "<IMG src=\"data:${img#file:},"
       query "SELECT hex(bin) FROM user_m $sqcond ORDER BY rowid LIMIT 1;" \
@@ -1933,12 +1983,12 @@
     return
   fi
   val=`getvalbyid grp profimg $rowid $tmpd`
-  enticond="gname=$qgrp AND key='profimg' AND type LIKE 'file:image%'"
+  enticond="gname=$qgrp"
   img=`query "SELECT type FROM grp_m WHERE $enticond LIMIT 1;"`
   if [ -n "$img" ]; then
     cat<<-EOF
 	<p class="groupimg">
-	`imgsrc_cache $td/main grp_m "$enticond"`</p>
+	`imgsrc_cache $td/main grp_m "$enticond" M`</p>
 	EOF
   fi
   echo "<div class=\"noprofimg\">"
@@ -2849,7 +2899,6 @@
 	## XX: DIRTY Hack Ends here
 	## We should keep blog's owner as a single column which has
 	## foreign key constraint with primary key of grp/user.
-	aa
        else
 	 query "$sql"
        fi
@@ -2917,11 +2966,11 @@
 		  file=$tmpd/`getparfilename $v`
 		  err type=file=$file
 		  [ -z "$file" ] && continue
-		  bn=${file##*/}
+		  bn=`sqlquotestr "${file##*/}"`
 		  bin="X'"$(hexize $file)"'"
 		  ct=`file --mime-type $file|cut -d' ' -f2`
 		  type=\"file:$ct\"
-		  newsql="update $tb2 set val='$bn', type=$type, bin=$bin"
+		  newsql="update $tb2 set val=$bn, type=$type, bin=$bin"
 		  cachedir=`getcachedir "$tbl/$rowid"`
 		  err getcache tbl/rowid=$tbl/$rowid, rm -r $cachedir
 		  rm -r $cachedir
@@ -3067,7 +3116,8 @@
     form="" val=""
     if [ -n "$rowid" ]; then
       # err genform2a: Seeking for "$2.$name, type=$type"
-      val=`getvalbyid $2 $name $rowid $td|htmlescape`
+      rawval=`getvalbyid $2 $name $rowid $td`
+      val=`echo "$rawval"|htmlescape`
 err genform3a: getvalbyid $2 $name $rowid $td
 err genform3b: val="[$val]"
     fi
@@ -3114,7 +3164,7 @@
 	  form=`cgi_multi_file $name $td "$args"`
 	  if [ -n "$val" ]; then
 	    hrfb="$myname?showattc+$2_m"
-	    val=$(echo "$val" \
+	    val=$(echo "$rawval" \
 		       | while read fn; do
 			   data=`percenthex $td/$fn`
 			   #ct=`cat $td/$fn.content-type`

yatex.org