changeset 11:3565d93c2fb1

add mpsplit.pl
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 20 Jul 2015 18:09:20 +0900
parents 63c0e22870bb
children 262bbdea72e2
files mpsplit.pl mpsplit.rb y4-funcs.sh y4.cgi
diffstat 4 files changed, 50 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mpsplit.pl	Mon Jul 20 18:09:20 2015 +0900
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+$sep = "--" . $ARGV[0];
+$dir = ($ARGV[1] || "tmp");
+
+#print "sep=".$sep, "dir=$dir\n";
+#binmode STDIN;
+$/ = undef;
+$stream = (<STDIN>);
+$n=0;
+@slices = split($sep, $stream);
+shift(@slices);
+pop(@slices);
+foreach $item (@slices) {
+  $item =~ s/^\n//;
+  ($header = $item) =~ s/\r\n\r\n.*//s;
+  ($body = $item) =~ s/.*?\r\n\r\n//s;
+  $body =~ s/\r\n$//;
+  #print "h=[$header]\n";
+  #print "b=[$body]\n";
+  unless ($header =~ /\bname=([\"']?)(.*?)\1/) {
+    next;
+  }
+  $name = $2;
+  #print "name=$name\n";
+  if ($header =~ /filename=(['\"]?)(.*?)\1/ && $2 gt "") {
+    $fn = $2;
+    open(OUT, ">$dir/$fn");
+      print OUT $body;
+    close(OUT);
+    printf("%s:filename=%s\n", $name, unpack("H*", $fn));
+  } else {
+    printf("%s=%s\n", $name, unpack("H*", $body));
+  }
+}
--- a/mpsplit.rb	Mon Jul 20 11:53:05 2015 +0900
+++ b/mpsplit.rb	Mon Jul 20 18:09:20 2015 +0900
@@ -20,10 +20,12 @@
     open(File.expand_path(fn, tmpdir).untaint, "w") do |out|
       out.write body
     end
-    printf("%s:filename=%s\n", name, fn)
+##    printf("%s:filename=%s\n", name, fn)
+    printf("%s:filename=%s\n", name, fn.unpack("H*")[0])
   elsif /name=(['\"]?)(.*?)\1/ =~ header
     v=$2
-    out=NKF::nkf("-w -MQ", body).gsub(/([^=])\n/, "\\1=0a").gsub(/=\n/, "")
-    printf("%s=%s\n", v, out)
+#    out=NKF::nkf("-w -MQ", body).gsub(/([^=])\n/, "\\1=0a").gsub(/=\n/, "")
+#    printf("%s=%s\n", v, out)
+    printf("%s=%s\n", v, body.unpack("H*")[0])
   end
 end
--- a/y4-funcs.sh	Mon Jul 20 11:53:05 2015 +0900
+++ b/y4-funcs.sh	Mon Jul 20 18:09:20 2015 +0900
@@ -442,7 +442,7 @@
    esac)
 }
 mktempd() {
-  TMPDIR=$tmpdir mktemp -d -t $session
+  TMPDIR=$tmpd mktemp -d -t $session
 }
 getval() {
   # $1=table $2=col $3(optional)=condition
@@ -939,7 +939,7 @@
 cgiinit() {
   session=`date +%F-$$`
   tmpf=tmp/stream
-  tmpd=`mktempd`
+  tmpd=`tmpd=$tmpdir mktempd`
   tmpfiles=$tmpfiles" $tmpd"
   addsession $session
   getcookie
@@ -954,12 +954,13 @@
   case "$CONTENT_TYPE" in
     *boundary*)
       bndry=${CONTENT_TYPE#*boundary=}
-      for us in `LC_CTYPE=C ./mpsplit.rb "$bndry" $tmpdir < $tmpf`
+      #for us in `LC_CTYPE=C ./mpsplit.rb "$bndry" $tmpdir < $tmpf`
+      for us in `LC_CTYPE=C ./mpsplit.pl "$bndry" $tmpdir < $tmpf`
       do
 	k=${us%%\=*}
 	#echo u=$us
-	# v="`echo ${us#*=}|nkf -Ww -mQ|sed -e 's/=/=3d/g' -e 's/\"/=22/g'`"
-	v="`echo ${us#*=}|nkf -Ww -mQ|sed -e 's/\"/\"\"/g'`"
+	#v="`echo ${us#*=}|nkf -Ww -mQ|sed -e 's/\"/\"\"/g'`"
+	v="`echo ${us#*=}|unhexize`"
  # err k=$k v=$v
 	case "$k" in
 	  *:filename)
--- a/y4.cgi	Mon Jul 20 11:53:05 2015 +0900
+++ b/y4.cgi	Mon Jul 20 18:09:20 2015 +0900
@@ -254,13 +254,11 @@
       putfooter; exit
     fi
     idir=`umask 002; mktempd` || exit 1
-    tmpfiles=$tmpfiles"${tmpfiles+ }$idir"
+    # tmpfiles=$tmpfiles"${tmpfiles+ }$idir"
     bin=$idir/$myname-$$.bin
-    ##sql="select quote(bin) from $2 where id='$3' and val='$4'"
-    sql="select quote(bin) from $2 where rowid='$3'"
-    sq $db "$sql" | xxd -r -p > $bin
-    # type=`sq $db "select type from $2 where id='$3' and val='$4'"`
-    tv=`sq $db "select type,val from $2 where rowid='$3'"`
+    sql="select quote(bin) from $2 where rowid='$3';"
+    sq $db "$sql" | unhexize > $bin
+    tv=`query "select type,val from $2 where rowid='$3';"`
     type=${tv%|*} fn=${tv#*|}
     err tv=$tv type=$type fn=$fn
     ct=${type#file:}
@@ -284,7 +282,7 @@
     ## contenttype "Image/jpeg"
     sql="select quote(b) from te where a='${2:-mikan.jpg}'"
     idir=`umask 002; mktempd` || exit 1
-    tmpfiles=$tmpfiles"${tmpfiles+ }$idir"
+    # tmpfiles=$tmpfiles"${tmpfiles+ }$idir"
     img=$idir/$myname-$$.bin
     db=b.sq3
     sqlite3 $db "$sql" | xxd -r -p > $img

yatex.org