Class: Rubytus::Command

Inherits:
API
  • Object
show all
Includes:
Constants, StorageHelper
Defined in:
lib/rubytus/command.rb

Constant Summary

Constants included from Constants

Rubytus::Constants::BASE_PATH_REGEX, Rubytus::Constants::COMMON_HEADERS, Rubytus::Constants::DEFAULT_BASE_PATH, Rubytus::Constants::DEFAULT_DATA_DIR, Rubytus::Constants::DEFAULT_MAX_SIZE, Rubytus::Constants::DEFAULT_STORAGE, Rubytus::Constants::ENV_BASE_PATH, Rubytus::Constants::ENV_DATA_DIR, Rubytus::Constants::ENV_MAX_SIZE, Rubytus::Constants::ENV_STORAGE, Rubytus::Constants::RESOURCE_UID_REGEX, Rubytus::Constants::RESUMABLE_CONTENT_TYPE, Rubytus::Constants::STATUS_BAD_REQUEST, Rubytus::Constants::STATUS_CREATED, Rubytus::Constants::STATUS_FORBIDDEN, Rubytus::Constants::STATUS_INTERNAL_ERROR, Rubytus::Constants::STATUS_NOT_ALLOWED, Rubytus::Constants::STATUS_NOT_FOUND, Rubytus::Constants::STATUS_OK

Instance Method Summary collapse

Methods included from StorageHelper

#file_path, #info_path, #validates_data_dir

Methods inherited from API

#default_options, #default_parser, #default_setup, #on_body, #response

Methods included from Helpers

#prepare_headers, #validates_base_path, #validates_length, #validates_max_size, #validates_offset

Methods included from Common

#error!, #generate_uid

Instance Method Details

#init_optionsObject



50
51
52
53
54
# File 'lib/rubytus/command.rb', line 50

def init_options
  options = default_options
  options[:data_dir] = ENV[ENV_DATA_DIR] || DEFAULT_DATA_DIR
  options
end

#on_close(env) ⇒ Object



36
37
38
39
40
# File 'lib/rubytus/command.rb', line 36

def on_close(env)
  if env['api.action'] == :patch
    storage.patch_file(env['api.uid'], env['api.buffers'], env['api.offset'])
  end
end

#on_headers(env, headers) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubytus/command.rb', line 16

def on_headers(env, headers)
  super(env, headers)

  request = Request.new(env)

  begin

    if env['api.action'] == :patch
      uid  = env['api.uid']
      info = storage.read_info(uid)

      validates_offset(request.offset, info.offset)
      validates_length(request.content_length, info.remaining_length)
    end

  rescue PermissionError => e
    error!(STATUS_INTERNAL_ERROR, e.message)
  end
end

#options_parser(opts, options) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/rubytus/command.rb', line 42

def options_parser(opts, options)
  options = init_options.merge(options)
  default_parser(opts, options)
  opts.on('-f', '--data-dir DATA_DIR', "Directory to store uploads, LOCAL storage only (default: #{options[:data_dir]})") do |value|
    options[:data_dir] = value
  end
end

#setupObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/rubytus/command.rb', line 56

def setup
  begin
    default_setup
    @options[:data_dir] = validates_data_dir(@options[:data_dir])
    @options[:storage]  = Storage.new(@options)
  rescue PermissionError, ConfigurationError => e
    puts '[ERROR] ' + e.message
    exit(1)
  end
end