-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
60 lines (49 loc) · 1.32 KB
/
app.rb
File metadata and controls
60 lines (49 loc) · 1.32 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
require "rubygems"
require "bundler"
Bundler.setup
require 'digest/sha1'
require 'digest/md5'
require "sinatra"
require "silo"
before do
@silo = Silo.new settings.silo_root
end
get '/' do
erb :index
end
post '/reserve' do
url = File.dirname(request.url) + "/" + @params["ieid"]
response = <<-RESPONSE
<?xml version="1.0" encoding="UTF-8"?>
<reserved location="#{url}" ieid="#{@params["ieid"]}"/>
RESPONSE
halt 201, response
end
get '/:name' do |name|
not_found unless @silo.has? name
atom = @silo[name]
response['Content-SHA1'] = atom.sha1
response['Content-MD5'] = atom.md5
send_file atom.data_path, :type => 'application/octet-stream'
end
put '/:name' do |name|
halt 400, "#{name} already exists" if @silo.has? name
@silo.write!(name) { |io| io.write request.body.read }
ieid = File.basename request.url
request.body.rewind
md5 = Digest::MD5.hexdigest(request.body.read)
request.body.rewind
sha1 = Digest::SHA1.hexdigest(request.body.read)
response = <<-RESPONSE
<?xml version="1.0" encoding="UTF-8"?>
<created location="#{request.url}" type="application/x-tar" md5="#{md5}" sha1="#{sha1}" size="#{request.body.size}" ieid="#{ieid}" name="#{ieid}"/>
RESPONSE
halt 201, response
end
delete '/' do
@silo.nuke!
end
delete '/:name' do |name|
not_found unless @silo.has? name
@silo.delete! name
end