blob: d45b7737dca88b39bc21d2a7f280f00371602c6c (
plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#!/usr/bin/env ruby
require 'digest'
require 'rack'
require 'json'
class DataStorageServer
MAX_BYTES=1024
def initialize(storage: {})
@storage = storage
end
# Download an Object
#
# GET /data/{repository}/{objectID}
# Response
#
# Status: 200 OK
# {object data}
# Objects that are not on the server will return a 404 Not Found.
def get(path)
oid = id_from(path)
if @storage.key?(oid)
['200', {}, [@storage[oid]]]
else
['404', {}, []]
end
end
# Upload an Object
#
# Request
# PUT /data/{repository}
#
# my data
#
# Response
#
# Status: 201 Created
# {size, oid}
# Blobs can have a max size of 1024 bytes.
# If a blob exceeds this size then the blob is truncated
# and the first 1024 bytes are stored.
def put(io)
data = io.read(MAX_BYTES)
oid = Digest::SHA256.hexdigest(data)
@storage[oid] = data
['201', {}, [JSON.generate({ size: data.size, oid: oid })]]
end
# Delete an Object
#
# Request
# DELETE /data/{repository}/{oid}
#
# Response
#
# Status: 200 OK
# If the object identifer is unknown
# then a 404 status code is returned.
def destroy(path)
oid = id_from(path)
if @storage.key?(oid)
@storage.delete(oid)
['200', {}, []]
else
['404', {}, []]
end
end
def call(env)
case env['REQUEST_METHOD']
when 'GET'
get(env['PATH_INFO'])
when 'PUT'
put(env['rack.input'])
when 'DELETE'
destroy(env['PATH_INFO'])
else
raise env.inspect
end
end
private
def id_from(path)
path.split('/')[-1]
end
end
|