Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 63 lines (48 sloc) 1.53 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#!/usr/local/bin/ruby
require 'rubygems'
require 'sinatra'
require 'base64'
require 'kconv'
require 'net/http'
require 'uri'

#require 'sinatra/base'
#require 'sinatra/reloader' if development?


before do
  if request.path_info == '/' && params[:url] then
    uri = URI.parse(URI.encode(params[:url]))
    begin
      Net::HTTP.start(uri.host) {|http|
        res = http.request_get(uri.path)
        redirect "/" unless res['content-type'].include?("image/")

        @api_dataURL = "data:" + res['content-type'] + ";base64," + Base64.encode64(res.body).gsub(/\n/,"")
      }
      @api_fileName = File.basename(uri.path).tosjis
    rescue
      redirect '/'
    end
  end
end


get '/' do
  erb :index
end


post '/' do
  redirect "/" unless params[:imagedata] && params[:filename]

  #dataURIをバイナリに変換
  imagedata = Base64.decode64(params[:imagedata].gsub(/^data:image\/png;base64,/, ""))

  #拡張子を除いたファイル名取得し、_c.pngを付け足す。日本語ファイル名文字化け対策にtosjis。
  filename = (File.basename(params[:filename], ".*") + "_c.png").tosjis

  #tmp/imagesフォルダに保存
  open("tmp/#{filename}","wb") do |fh|
    fh.write imagedata
  end

  #?秒後に削除。
  Thread.new do
    sleep(5);
    File.delete("tmp/#{filename}")
  end

  #send_fileは内部でhaltするので最後に書く。
  send_file("tmp/#{filename}", :disposition => "attachment")
end


not_found do
  redirect '/'
end
Something went wrong with that request. Please try again.