-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from 0sc/develop
add activestorage as dependencies refactor implementation to leverage more cloudinary sdk methods update download implementation to support chunked download bump version to 0.2.0
- Loading branch information
Showing
10 changed files
with
281 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'net/http' | ||
require 'openssl' | ||
|
||
module DownloadUtils | ||
def stream_download(source, chunk_size = 5_242_880) | ||
url = URI.parse(source) | ||
http, req = setup_connection(url) | ||
|
||
content_length = http.request_head(url).content_length | ||
upper_limit = content_length + (content_length % chunk_size) | ||
offset = 0 | ||
|
||
http.start do |agent| | ||
while offset < upper_limit | ||
lim = (offset + chunk_size) | ||
# QUESTION: is it relevant to set the last chunk | ||
# to the exact remaining bytes | ||
# lim = content_length if lim > content_length | ||
req.range = (offset..lim) | ||
|
||
chunk = agent.request(req).body | ||
yield chunk.force_encoding(Encoding::BINARY) | ||
|
||
offset += chunk_size + 1 | ||
end | ||
end | ||
end | ||
|
||
def download_range(source, range) | ||
url = URI.parse(source) | ||
http, req = setup_connection(url) | ||
req.range = range | ||
|
||
chunk = http.start { |agent| agent.request(req).body } | ||
chunk.force_encoding(Encoding::BINARY) | ||
end | ||
|
||
private | ||
|
||
def setup_connection(url) | ||
http = Net::HTTP.new(url.host, url.port) | ||
req = Net::HTTP::Get.new(url.request_uri) | ||
|
||
if url.port == 443 | ||
http.use_ssl = true | ||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
end | ||
[http, req] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module ActiveStorage | ||
module CloudinaryService | ||
VERSION = '0.1.0'.freeze | ||
VERSION = '0.2.0'.freeze | ||
end | ||
end |
Oops, something went wrong.