#!/usr/bin/env ruby # coding: utf-8 require 'cgi' cgi = CGI.new(:accept_charset => "UTF-8") files = cgi.params["image"] # 投稿された画像ファイル(群) title = cgi.params["title"][0] # 1個だけだが配列なので [0] を取り出す # msg = cgi["title"] としても同じこと h1 = title.read msg = cgi.params["msg"][0].read today = Time.now.strftime("%Y年%m月%d日") maxbytes = 1024*1024 # 最大1MBまで # HTTPヘッダ+HTMLの出力 print <<_EOF_ Content-type: text/html; charset=utf-8\n\n #{Time.now.to_s}

#{today} #{h1}

_EOF_ dir = "tmp/" filenames = [] # 処理したファイル名を記憶する配列 n = 0 files.each do |f| now = Time.now.strftime("%s") fn = "image#{now}#{n+=1}.jpg" # 保存ファイル名を自動的に決める if f.size > maxbytes printf("%sは大きすぎるのでパス(最大%dバイト)
", f.original_filename, maxbytes) next end open(dir+fn, "w") do |w| w.write f.read # POSTされたデータをファイルに保存 end thumbnail = dir+"tn_"+fn # サムネイル画像を自動生成 system "convert -geometry 160x120 #{dir+fn} #{thumbnail}" # サムネイルからもと画像へリンクする printf("\n", dir+fn, thumbnail) filenames << dir+fn # あとで消すファイルを記憶しておく filenames << thumbnail end print <<_EOF_

#{msg}

_EOF_ =begin fork { # ファイルを消す fork { STDOUT.close STDERR.close sleep 5 File.unlink(*filenames) } } Process.wait =end