1
1
require 'goliath'
2
2
require 'goliath/constants'
3
- require 'rubytus/error'
4
- require 'rubytus/helpers'
5
- require 'rubytus/request'
6
- require 'rubytus/storage'
7
3
require 'rubytus/constants'
8
- require 'rubytus/rack/handler'
4
+ require 'rubytus/request'
5
+ require 'rubytus/helpers'
6
+ require 'rubytus/error'
9
7
require 'stringio'
10
8
11
9
module Rubytus
@@ -14,109 +12,68 @@ class API < Goliath::API
14
12
include Rubytus ::Constants
15
13
include Rubytus ::Helpers
16
14
17
- use Rubytus ::Rack ::Handler
18
-
19
15
def on_headers ( env , headers )
20
- request = Rubytus ::Request . new ( env )
21
-
22
16
env [ 'api.options' ] = @options
23
- env [ 'api.headers' ] = COMMON_HEADERS . merge ( {
24
- 'Date' => Time . now . httpdate
25
- } )
26
-
27
- begin
28
- if request . collection? && request . post?
29
- uid = generate_uid
30
-
31
- env [ 'api.action' ] = :create
32
- env [ 'api.uid' ] = uid
33
- env [ 'api.final_length' ] = request . final_length
34
- env [ 'api.resource_url' ] = request . resource_url ( uid )
35
- end
36
-
37
- if request . resource? && request . head?
38
- env [ 'api.action' ] = :head
39
- env [ 'api.uid' ] = request . resource_uid
40
- end
41
-
42
- if request . resource? && request . patch?
43
- unless request . resumable_content_type?
44
- raise HeaderError , "Content-Type must be '#{ RESUMABLE_CONTENT_TYPE } '"
45
- end
46
-
47
- uid = request . resource_uid
48
- info = storage . read_info ( uid )
49
-
50
- if request . offset > info . offset
51
- raise UploadError , "Offset: #{ request . offset } exceeds current offset: #{ info . offset } "
52
- end
53
-
54
- if request . content_length > info . remaining_length
55
- raise UploadError , "Content-Length: #{ request . content_length } exceeded remaining length: #{ info . remaining_length } "
56
- end
57
-
58
- env [ 'api.action' ] = :patch
59
- env [ 'api.uid' ] = uid
60
- env [ 'api.file' ] = storage . open_file ( uid , request . offset )
61
- end
62
-
63
- if request . resource? && request . get?
64
- env [ 'api.action' ] = :get
65
- env [ 'api.uid' ] = request . resource_uid
66
- end
67
-
68
- rescue HeaderError => e
69
- raise Goliath ::Validation ::Error . new ( STATUS_BAD_REQUEST , e . message )
70
-
71
- rescue UploadError => e
72
- raise Goliath ::Validation ::Error . new ( STATUS_FORBIDDEN , e . message )
73
- end
17
+ env [ 'api.headers' ] = COMMON_HEADERS . merge ( { 'Date' => Time . now . httpdate } )
18
+ prepare_headers ( env , headers )
74
19
end
75
20
76
21
def on_body ( env , data )
77
22
if env [ 'api.action' ] == :patch
78
- storage . patch_file ( env [ 'api.file' ] , data )
23
+ env [ 'api.buffers' ] << data
79
24
else
80
25
body = StringIO . new ( data )
81
26
env [ RACK_INPUT ] = body
82
27
end
83
28
end
84
29
85
- def on_close ( env )
86
- file = env [ 'api.file' ]
87
-
88
- if file
89
- size = file . size
90
- file . close unless file . closed?
91
- storage . update_info ( env [ 'api.uid' ] , size )
92
- end
93
- end
94
-
95
30
def response ( env )
96
31
status = STATUS_OK
97
32
headers = env [ 'api.headers' ]
98
- action = env [ 'api.action' ]
99
33
body = [ ]
100
34
101
- begin
102
- case action
103
- when :create
104
- status = STATUS_CREATED
105
- headers [ 'Location' ] = env [ 'api.resource_url' ]
106
- storage . create_file ( env [ 'api.uid' ] , env [ 'api.final_length' ] )
35
+ [ status , headers , body ]
36
+ end
37
+
38
+ def default_setup
39
+ @options [ :max_size ] = validates_max_size ( @options [ :max_size ] )
40
+ @options [ :base_path ] = validates_base_path ( @options [ :base_path ] )
41
+ end
107
42
108
- when :head
109
- info = storage . read_info ( env [ 'api.uid' ] )
110
- headers [ 'Offset' ] = info . offset . to_s
43
+ def default_options
44
+ {
45
+ :base_path => ENV [ ENV_BASE_PATH ] || DEFAULT_BASE_PATH ,
46
+ :max_size => ENV [ ENV_MAX_SIZE ] || DEFAULT_MAX_SIZE
47
+ }
48
+ end
111
49
112
- when :get
113
- body = storage . read_file ( env [ 'api.uid' ] )
50
+ def default_parser ( opts , options )
51
+ opts . separator ""
52
+ opts . separator "TUSD options:"
53
+
54
+ args = [
55
+ {
56
+ :name => :base_path ,
57
+ :short => '-b' ,
58
+ :long => '--base-path BASE_PATH' ,
59
+ :desc => "Url path used for handling uploads (default: #{ options [ :base_path ] } )"
60
+ } ,
61
+ {
62
+ :name => :max_size ,
63
+ :short => '-m' ,
64
+ :long => '--max-size MAX_SIZE' ,
65
+ :desc => "Maximum bytes may be stored inside storage (default: #{ options [ :max_size ] } )"
66
+ }
67
+ ]
68
+
69
+ args . each do |arg |
70
+ opts . on ( arg [ :short ] , arg [ :long ] , arg [ :desc ] ) do |value |
71
+ options [ arg [ :name ] ] = value
114
72
end
115
- rescue PermissionError => e
116
- raise Goliath ::Validation ::Error . new ( 500 , e . message )
117
73
end
118
74
119
- [ status , headers , body ]
75
+ # save into global options
76
+ @options = options
120
77
end
121
78
end
122
79
end
0 commit comments