This repository has been archived by the owner on Jul 24, 2019. It is now read-only.
forked from michelson/dante2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ru
64 lines (52 loc) · 1.41 KB
/
config.ru
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
64
require 'rubygems'
# require 'middleman/rack'
require "sinatra/base"
require "sinatra/json"
require "rack/cors"
require "pry"
require "open-uri"
class UploadServer < Sinatra::Base
use Rack::Static, :urls => ["/images"], :root => "tmp"
use Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
# Info
get "/info" do
"Dante Upload server, hello!"
end
# Handle POST-request (Receive and save the uploaded file)
post "/new.?:format?" do
content_type :json
# to test locking system let the upload sleep por a while before response
# sleep 10
if params[:file]
handleFile
else
handleUrl
end
json :url=> "http://localhost:9292/uploads/images/#{@name}"
end
def handleUrl
ext = File.extname(URI.parse(params["url"]).path)
@name = [Random.new_seed , ext].join("-")
path = File.join(File.dirname(__FILE__), 'tmp/images', @name)
File.open(path, "wb") do |f|
f.write(open(params["url"]).read)
end
end
def handleFile
@name = [Random.new_seed , params['file'][:filename]].join("-")
path = File.join(File.dirname(__FILE__), 'tmp/images', @name)
File.open(path, "wb") do |f|
f.write(params['file'][:tempfile].read)
end
end
end
#Mounts upload server & middleman app
run Rack::URLMap.new(
# "/" => Middleman.server,
"/uploads" => UploadServer.new
)