Class: Bkblz::Task::UploadFile

Inherits:
BaseTask
  • Object
show all
Defined in:
lib/bkblz/task/upload_file.rb

Constant Summary

Constants included from ClassMethods

ClassMethods::PARAM_SPEC

Instance Attribute Summary

Attributes inherited from BaseTask

#config, #result

Instance Method Summary collapse

Methods inherited from BaseTask

#initialize, #run

Methods included from ClassMethods

#check_one_of, #check_params, #check_required, #run, #task_param, #task_params

Methods included from TaskHelpers

#find_bucket_by_name

Constructor Details

This class inherits a constructor from Bkblz::Task::BaseTask

Instance Method Details

#run_internal(session, params) ⇒ Object



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
# File 'lib/bkblz/task/upload_file.rb', line 10

def run_internal(session, params)
  file_body = if params[:file_path]
                f = ::File.new(params[:file_path], "r")
                f.read
              elsif params[:file_body]
                params[:file_body]
              else
                raise 'missing either :file_body or :file_path param'
              end

  file_name = if params[:file_name]
                params[:file_name]
              elsif params[:file_path]
                ::File.basename(params[:file_path])
              else
                raise 'missing either :file_name or :file_path param'
              end

  file_mtime = if params[:file_path]
                 ::File.mtime(params[:file_path])
               else
                 Time.now
               end.to_i * 1000

  bucket = find_bucket_by_name session, params[:bucket_name]
  upload_auth = session.send(
    Bkblz::V1::GetUploadUrlRequest.new bucket.bucket_id).to_model

  upload_file_info = session.send(
    Bkblz::V1::UploadFileRequest.new(
      upload_auth, file_body, file_name, nil, file_mtime)).to_model
end