Class: ChinoRuby::Blobs
Instance Method Summary
collapse
-
#commit_upload(upload_id) ⇒ Object
-
#delete_blob(blob_id, force) ⇒ Object
-
#get(blob_id, destination) ⇒ Object
-
#init_upload(filename, document_id, field) ⇒ Object
-
#upload_blob(path, filename, document_id, field, chunk_size = 1024*32) ⇒ Object
-
#upload_chunk(upload_id, bytes, offset) ⇒ Object
#delete_resource, #get_resource, #initialize, #parse_response, #patch_resource, #post_resource, #post_resource_with_string_result, #put_resource, #return_uri
Methods inherited from CheckValues
#check_boolean, #check_int, #check_json, #check_string
Instance Method Details
#commit_upload(upload_id) ⇒ Object
1546
1547
1548
1549
1550
1551
1552
|
# File 'lib/chino_ruby/classes.rb', line 1546
def commit_upload(upload_id)
check_string(upload_id)
data = { upload_id: upload_id }.to_json
blob = Blob.new
blob.from_json(post_resource("/blobs/commit", data).to_json, true)
blob
end
|
#delete_blob(blob_id, force) ⇒ Object
1585
1586
1587
1588
1589
|
# File 'lib/chino_ruby/classes.rb', line 1585
def delete_blob(blob_id, force)
check_string(blob_id)
check_boolean(force)
delete_resource("/blobs/#{blob_id}", force)
end
|
#get(blob_id, destination) ⇒ Object
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
|
# File 'lib/chino_ruby/classes.rb', line 1554
def get(blob_id, destination)
check_string(blob_id)
check_string(destination)
uri = return_uri("/blobs/#{blob_id}")
req = Net::HTTP::Get.new(uri.path)
if @customer_id == "Bearer "
req.add_field("Authorization", @customer_id+@customer_key)
else
req.basic_auth @customer_id, @customer_key
end
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
http.request(req)
}
blob = GetBlobResponse.new
blob.blob_id = blob_id
filename = res.['Content-Disposition'].partition('=').last
blob.filename = filename
blob.path = destination
file_path = File.join Dir.pwd, destination
FileUtils.mkdir_p(file_path) unless File.exist?(file_path)
File.open(File.join(file_path+filename), 'wb') { |file|
file << res.body
blob.md5 = (Digest::MD5.file file).hexdigest
blob.sha1 = (Digest::SHA1.file file).hexdigest
blob.size = file.size
}
blob
end
|
#init_upload(filename, document_id, field) ⇒ Object
1516
1517
1518
1519
1520
1521
1522
1523
1524
|
# File 'lib/chino_ruby/classes.rb', line 1516
def init_upload(filename, document_id, field)
check_string(filename)
check_string(document_id)
check_string(field)
data = { file_name: filename, document_id: document_id, field: field }.to_json
blob = InitBlobResponse.new
blob.from_json(ActiveSupport::JSON.decode(post_resource("/blobs", data).to_json)['blob'].to_json)
blob
end
|
#upload_blob(path, filename, document_id, field, chunk_size = 1024*32) ⇒ Object
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
|
# File 'lib/chino_ruby/classes.rb', line 1495
def upload_blob(path, filename, document_id, field, chunk_size = 1024*32)
check_string(path)
check_string(document_id)
check_string(field)
check_string(filename)
blob = InitBlobResponse.new
blob = init_upload(filename, document_id, field)
bytes = []
offset = 0
file_path = File.join path, filename
File.open(file_path, 'rb') { |file|
while (buffer = file.read(chunk_size)) do
upload_chunk(blob.upload_id, buffer, offset)
offset = offset+buffer.length
end
commit_upload(blob.upload_id)
}
end
|
#upload_chunk(upload_id, bytes, offset) ⇒ Object
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
|
# File 'lib/chino_ruby/classes.rb', line 1526
def upload_chunk(upload_id, bytes, offset)
uri = return_uri("/blobs/#{upload_id}")
req = Net::HTTP::Put.new(uri)
req.body = bytes
req.add_field("length", bytes.length)
req.add_field("offset", offset)
req.add_field("Content-Type", "application/octet-stream")
if @customer_id == "Bearer "
req.add_field("Authorization", @customer_id+@customer_key)
else
req.basic_auth @customer_id, @customer_key
end
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
http.request(req)
}
blob = InitBlobResponse.new
blob.from_json(parse_response(res)['data'].to_json, true)
blob
end
|