Class: Uricp::Segmenter

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging, Methadone::SH, CurlPrimitives
Defined in:
lib/uricp/segmenter.rb

Constant Summary

Constants included from CurlPrimitives

CurlPrimitives::ORBIT_HOSTS

Instance Attribute Summary

Attributes included from CurlPrimitives

#options

Instance Method Summary collapse

Methods included from CurlPrimitives

#authentication, #curl_command, #curl_download_to_pipe, #curl_manifest, #curl_upload_from, #from, #from=, #http_authentication?, #orbit?, #temp_url?, #to, #to=

Constructor Details

#initialize(options) ⇒ Segmenter

Returns a new instance of Segmenter.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/uricp/segmenter.rb', line 11

def initialize(options)
  @options = options
  source = options['from']
  if @source = source && open(source)
    @stream = File.pipe?(source) || File.chardev?(source)
  else
    @source = STDIN
    @stream = true
  end
  split_path
end

Instance Method Details

#add_manifestObject



80
81
82
83
84
# File 'lib/uricp/segmenter.rb', line 80

def add_manifest
  debug "Adding DLO object_manifest #{object_manifest}"
  sh! curl_manifest(object_manifest),
      on_fail: "Upload to #{to} failed"
end

#large_upload?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/uricp/segmenter.rb', line 48

def large_upload?
  stream? || @source.stat.size > segment_size
end

#manifest_suffixObject



52
53
54
# File 'lib/uricp/segmenter.rb', line 52

def manifest_suffix
  @manifest_suffix ||= [Time.now.to_f, @source.stat.size, segment_size].join('/')
end

#object_manifestObject



56
57
58
# File 'lib/uricp/segmenter.rb', line 56

def object_manifest
  @object_manifest ||= File.join(@container, @object_path, manifest_suffix) + '/'
end

#segment_sizeObject



40
41
42
# File 'lib/uricp/segmenter.rb', line 40

def segment_size
  options['segment-size'].to_i
end

#segmented_uploadObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/uricp/segmenter.rb', line 60

def segmented_upload
  debug "#{self.class.name}: Large upload detected - segmenting into #{segment_size} byte chunks."
  suffix = 0
  until @source.eof?
    debug "#{self.class.name}: Uploading segment #{suffix}"
    upload_segment(suffix)
    suffix = suffix.next
  end
  add_manifest
end

#split_pathObject



23
24
25
26
27
28
29
# File 'lib/uricp/segmenter.rb', line 23

def split_path
  elements = Pathname.new(to.path).enum_for(:each_filename).to_a
  elements.shift
  @account = elements.shift
  @container = elements.shift
  @object_path = File.join(elements)
end

#stream?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/uricp/segmenter.rb', line 44

def stream?
  @stream
end

#uploadObject



31
32
33
34
35
36
37
38
# File 'lib/uricp/segmenter.rb', line 31

def upload
  if large_upload?
    segmented_upload
  else
    sh! curl_upload_from(options['from']),
        on_fail: "Upload to #{to} failed"
  end
end

#upload_segment(segment_number) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/uricp/segmenter.rb', line 71

def upload_segment(segment_number)
  segment_name = File.join(to.to_s, manifest_suffix, format('%08d', segment_number))
  debug "Uploading with #{curl_upload_from('-', segment_name)}"
  open('|' + curl_upload_from('-', segment_name), 'w') do |destination|
    copy_length = IO.copy_stream(@source, destination, segment_size)
    debug "#{self.class.name}: Uploaded #{copy_length} bytes to #{segment_name}"
  end
end