Class: Bio::BaseSpace::UploadTask

Inherits:
Object
  • Object
show all
Defined in:
lib/basespace/model/multipart_upload.rb

Overview

Multipart file upload helper class.

TODO This file is not yet ported as the multipartFileUpload class is just mentioned in the comment section of the BaseSpaceAPI file.

Instance Method Summary collapse

Constructor Details

#initialize(api, bs_file_id, part, total, myfile, attempt) ⇒ UploadTask

Create a new upload task object.

api

BaseSpaceAPI instance.

bs_file_id

BaseSpace file ID.

part

Part number of the multi-part upload.

total

Total number of parts in the multi-part upload.

myfile

Local file to be uploaded.

attempt

Number of attempts that the file was previously uploaded (upload tries).



34
35
36
37
38
39
40
41
42
# File 'lib/basespace/model/multipart_upload.rb', line 34

def initialize(api, bs_file_id, part, total, myfile, attempt)
  @api         = api
  @part        = part       # part number
  @total       = total       # out of total part count
  @file        = myfile      # the local file to be uploaded
  @bs_file_id  = bs_file_id  # the baseSpace fileId
  @attempt     = attempt     # the # of attempts we've made to upload this guy
  @state       = 0           # 0=pending, 1=ran, 2=error
end

Instance Method Details

#callObject

Upload a part of the file.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/basespace/model/multipart_upload.rb', line 50

def call
  # read the byte string in
  @attempt += 1
  trans_file = @file + @part.to_s
  cmd = "split -d -n #{@part}/#{@total} #{@file}"
  out = `#{cmd}`
  File.open(trans_file, "w") do |f|
    f.write(out)
  end
  # [TODO] confirm whether md5(out).digest is equivalent to MD5.digest (or MD5.hexdigest?)
  @md5 = Base64.encode64(Digest::MD5.digest(out))
  res = self.api.upload_multipart_unit(@bs_file_id, @part, @md5, trans_file)
  # puts "my result #{res}"
  File.delete(trans_file)
  if res['Response'].has_key?('ETag')
    @state = 1          # case things went well
  else
    @state = 2
  end
  return self
end

#to_sObject

Returns information about which part of which file is uploaded, including the total number of parts.



73
74
75
# File 'lib/basespace/model/multipart_upload.rb', line 73

def to_s
  return "#{@part} / #{@total} - #{@file}"
end

#upload_file_nameObject

Returns the filename (without path) if the file to be uploaded.



45
46
47
# File 'lib/basespace/model/multipart_upload.rb', line 45

def upload_file_name
  return @file.split('/').last + '_' + @part.to_s
end