Skip to content

Commit dc1c175

Browse files
committed
handling error and test configuration
1 parent 81fe36d commit dc1c175

File tree

10 files changed

+118
-25
lines changed

10 files changed

+118
-25
lines changed

.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+

.simplecov

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SimpleCov.start do
2+
add_filter '/test/'
3+
add_filter '/bin/'
4+
5+
add_group 'Libraries', 'lib'
6+
end

lib/rubytus.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'rubytus/version'
2+
require 'rubytus/error'
23
require 'rubytus/handler'
34
require 'rubytus/response'
45
require 'rubytus/server'

lib/rubytus/configuration.rb

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
11
module Rubytus
22
class Configuration
3-
attr_accessor :data_dir
4-
attr_accessor :max_size
5-
attr_accessor :base_path
6-
7-
def initialize
8-
@data_dir = ENV.fetch('TUSD_DATA_DIR', 'tus_data')
9-
@max_size = ENV.fetch('TUSD_DATASTORE_MAX_SIZE', 1024 * 1024 * 1024)
10-
@base_path = ENV.fetch('TUSD_BASEPATH', '/files/')
3+
attr_reader :data_dir
4+
attr_reader :max_size
5+
attr_reader :base_path
6+
7+
def initialize(options = {})
8+
@data_dir = options[:data_dir] || ENV['TUSD_DATA_DIR'] || 'tus_data'
9+
@base_path = options[:base_path] || ENV['TUSD_BASE_PATH'] || '/files/'
10+
@max_size = options[:max_size] || ENV['TUSD_MAX_SIZE'] || 1024 * 1024 * 1024
11+
end
12+
13+
def validate_data_dir
14+
data_dir = File.expand_path(@data_dir)
15+
16+
begin
17+
unless File.directory?(data_dir)
18+
Dir.mkdir(data_dir)
19+
end
20+
rescue SystemCallError => _
21+
raise PermissionError, "Couldn't create `data_dir` in #{data_dir}"
22+
end
23+
24+
File.chmod(0777, data_dir)
25+
@data_dir = data_dir
26+
end
27+
28+
def validate_max_size
29+
if @max_size.is_a? String
30+
@max_size = @max_size.to_i
31+
end
32+
33+
if @max_size <= 0
34+
raise ConfigurationError, "Invalid `max_size`, it should be > 0 bytes"
35+
end
1136
end
1237
end
1338
end

lib/rubytus/error.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Rubytus
2+
class ConfigurationError < StandardError; end
3+
class PermissionError < StandardError; end
4+
end

lib/rubytus/handler.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ class Handler
66

77
def initialize(configuration)
88
@configuration = configuration
9-
prepare
10-
end
11-
12-
def prepare
13-
# catch error if we can't make directory
14-
unless File.directory?(configuration.data_dir)
15-
Dir.mkdir(configuration.data_dir)
16-
File.chmod(0777, configuration.data_dir)
17-
end
189
end
1910

2011
def uid

lib/rubytus/server.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,36 @@ def call(env)
1313
@response = Rubytus::Response.new(body, status, header)
1414

1515
if collection_path?
16-
@response['Allow'] = 'POST'
17-
1816
if @request.post?
1917
# create resource
18+
else
19+
@response['Allow'] = 'POST'
2020
end
2121
end
2222

2323
if resource_path?
24-
if @request.patch?
25-
# continue previously uploaded file
26-
end
27-
2824
if @request.head?
2925
# fetch file metadata
26+
# response: 200, Offset
27+
end
28+
29+
if @request.patch?
30+
# continue previously uploaded file
31+
# request: Content-Length, Offset, (trim upload based on offset), Content-Type: application/offset+octet-stream
32+
# response: 200
3033
end
3134

3235
if @request.get?
3336
@response.write resource_path
3437
end
3538

36-
@response['Allow'] = 'HEAD,PATCH'
39+
unless @request.head? or @request.patch? or @request.get?
40+
@response['Allow'] = 'HEAD,PATCH'
41+
end
3742
end
3843

3944
# TODO: optimize!
40-
unless collection_path? or resource_path?
45+
if invalid_path?
4146
@response.status = 404
4247
@response.write('not found')
4348
end
@@ -65,6 +70,10 @@ def resource_path?
6570
resource_path =~ /^([a-z0-9]{32})$/
6671
end
6772

73+
def invalid_path?
74+
!collection_path? || !resource_path?
75+
end
76+
6877
def resource_path
6978
path = @request.path
7079
path.slice!(@@configuration.base_path)

rubytus.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
2525
spec.add_development_dependency "rack"
2626
spec.add_development_dependency "rack-test"
2727
spec.add_development_dependency "pry"
28+
spec.add_development_dependency "simplecov"
2829

2930
spec.add_runtime_dependency "rack"
3031
spec.add_runtime_dependency "puma"

test/rubytus/test_configuration.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'test_helper'
2+
require 'fileutils'
3+
4+
class TestConfiguration < MiniTest::Unit::TestCase
5+
def test_initialize_with_options
6+
configuration = Rubytus::Configuration.new(:base_path => '/uploads/')
7+
assert_equal '/uploads/', configuration.base_path
8+
end
9+
10+
def test_initialize_with_env
11+
ENV['TUSD_BASE_PATH'] = '/uploads/'
12+
13+
configuration = Rubytus::Configuration.new
14+
assert_equal '/uploads/', configuration.base_path
15+
end
16+
17+
def test_validate_data_dir
18+
random_name = "/tmp/rubytus-#{rand(10000)}"
19+
configuration = Rubytus::Configuration.new(:data_dir => random_name)
20+
configuration.validate_data_dir
21+
22+
assert_equal true, File.directory?(configuration.data_dir)
23+
24+
FileUtils.rm_rf configuration.data_dir # cleanup
25+
end
26+
27+
def test_validate_data_dir_permission_error
28+
configuration = Rubytus::Configuration.new(:data_dir => '/opt/rubytus')
29+
assert_raises(Rubytus::PermissionError) { configuration.validate_data_dir }
30+
end
31+
32+
def test_validate_max_size_blank
33+
configuration = Rubytus::Configuration.new(:max_size => '')
34+
assert_raises(Rubytus::ConfigurationError) { configuration.validate_max_size }
35+
end
36+
37+
def test_validate_max_size_with_env
38+
ENV['TUSD_MAX_SIZE'] = '102400'
39+
40+
configuration = Rubytus::Configuration.new
41+
configuration.validate_max_size
42+
assert_equal 102400, configuration.max_size
43+
end
44+
end

test/test_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
ENV['RACK_ENV'] ||= 'test'
22

3+
require 'simplecov'
34
require 'minitest/autorun'
45
require 'turn/autorun'
56
require 'rack'
67
require 'rack/test'
8+
require 'pry'
79
require 'rubytus'
810

911
Turn.config do |c|

0 commit comments

Comments
 (0)