Class: Uricp::Segmenter
- Inherits:
-
Object
show all
- Includes:
- Methadone::CLILogging, Methadone::SH, CurlPrimitives
- Defined in:
- lib/uricp/segmenter.rb
Instance Attribute Summary
#options
Instance Method Summary
collapse
#authentication, #curl_command, #curl_download_to_pipe, #curl_manifest, #curl_upload_from, #from, #from=, #http_authentication?, #to, #to=
Constructor Details
#initialize(options) ⇒ Segmenter
Returns a new instance of Segmenter.
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/uricp/segmenter.rb', line 9
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_manifest ⇒ Object
78
79
80
81
82
|
# File 'lib/uricp/segmenter.rb', line 78
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
46
47
48
|
# File 'lib/uricp/segmenter.rb', line 46
def large_upload?
stream? || @source.stat.size > segment_size
end
|
#manifest_suffix ⇒ Object
50
51
52
|
# File 'lib/uricp/segmenter.rb', line 50
def manifest_suffix
@manifest_suffix ||= [Time.now.to_f, @source.stat.size, segment_size].join('/')
end
|
#object_manifest ⇒ Object
54
55
56
|
# File 'lib/uricp/segmenter.rb', line 54
def object_manifest
@object_manifest ||= File.join(@container, @object_path, manifest_suffix) + '/'
end
|
#segment_size ⇒ Object
38
39
40
|
# File 'lib/uricp/segmenter.rb', line 38
def segment_size
options['segment-size'].to_i
end
|
#segmented_upload ⇒ Object
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/uricp/segmenter.rb', line 58
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_path ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/uricp/segmenter.rb', line 21
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
42
43
44
|
# File 'lib/uricp/segmenter.rb', line 42
def stream?
@stream
end
|
#upload ⇒ Object
29
30
31
32
33
34
35
36
|
# File 'lib/uricp/segmenter.rb', line 29
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
69
70
71
72
73
74
75
76
|
# File 'lib/uricp/segmenter.rb', line 69
def upload_segment(segment_number)
segment_name = File.join(to.to_s, manifest_suffix, '%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
|