uploadcare-ruby 3.3.2 → 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +2 -1
- data/.gitignore +2 -0
- data/CHANGELOG.md +30 -0
- data/README.md +198 -36
- data/lib/uploadcare/client/addons_client.rb +56 -0
- data/lib/uploadcare/client/conversion/base_conversion_client.rb +1 -1
- data/lib/uploadcare/client/conversion/document_conversion_client.rb +1 -1
- data/lib/uploadcare/client/conversion/video_conversion_client.rb +1 -1
- data/lib/uploadcare/client/file_client.rb +14 -10
- data/lib/uploadcare/client/file_list_client.rb +4 -4
- data/lib/uploadcare/client/file_metadata_client.rb +36 -0
- data/lib/uploadcare/client/group_client.rb +4 -4
- data/lib/uploadcare/client/multipart_upload/chunks_client.rb +1 -0
- data/lib/uploadcare/client/multipart_upload_client.rb +7 -6
- data/lib/uploadcare/client/project_client.rb +1 -1
- data/lib/uploadcare/client/rest_client.rb +6 -5
- data/lib/uploadcare/client/rest_group_client.rb +24 -4
- data/lib/uploadcare/client/upload_client.rb +4 -1
- data/lib/uploadcare/client/uploader_client.rb +11 -10
- data/lib/uploadcare/client/webhook_client.rb +3 -3
- data/lib/uploadcare/entity/addons.rb +14 -0
- data/lib/uploadcare/entity/conversion/base_converter.rb +1 -1
- data/lib/uploadcare/entity/file.rb +19 -37
- data/lib/uploadcare/entity/file_list.rb +1 -0
- data/lib/uploadcare/entity/file_metadata.rb +30 -0
- data/lib/uploadcare/entity/group.rb +11 -2
- data/lib/uploadcare/entity/uploader.rb +13 -13
- data/lib/uploadcare/param/authentication_header.rb +2 -2
- data/lib/uploadcare/param/secure_auth_header.rb +1 -1
- data/lib/uploadcare/param/upload/upload_params_generator.rb +25 -8
- data/lib/uploadcare/ruby/version.rb +1 -1
- data/lib/uploadcare.rb +1 -0
- data/uploadcare-ruby.gemspec +10 -3
- metadata +27 -10
@@ -13,8 +13,8 @@ module Uploadcare
|
|
13
13
|
|
14
14
|
# Upload a big file by splitting it into parts and sending those parts into assigned buckets
|
15
15
|
# object should be File
|
16
|
-
def upload(object,
|
17
|
-
response = upload_start(object,
|
16
|
+
def upload(object, options = {}, &block)
|
17
|
+
response = upload_start(object, options)
|
18
18
|
return response unless response.success[:parts] && response.success[:uuid]
|
19
19
|
|
20
20
|
links = response.success[:parts]
|
@@ -24,9 +24,10 @@ module Uploadcare
|
|
24
24
|
end
|
25
25
|
|
26
26
|
# Asks Uploadcare server to create a number of storage bin for uploads
|
27
|
-
def upload_start(object,
|
27
|
+
def upload_start(object, options = {})
|
28
|
+
options.merge!(store: options[:store] || false)
|
28
29
|
body = HTTP::FormData::Multipart.new(
|
29
|
-
Param::Upload::UploadParamsGenerator.call(
|
30
|
+
Param::Upload::UploadParamsGenerator.call(options).merge(form_data_for(object))
|
30
31
|
)
|
31
32
|
post(path: 'multipart/start/',
|
32
33
|
headers: { 'Content-Type': body.content_type },
|
@@ -56,8 +57,8 @@ module Uploadcare
|
|
56
57
|
end
|
57
58
|
|
58
59
|
alias api_struct_post post
|
59
|
-
def post(
|
60
|
-
handle_throttling { api_struct_post(
|
60
|
+
def post(args = {})
|
61
|
+
handle_throttling { api_struct_post(args) }
|
61
62
|
end
|
62
63
|
end
|
63
64
|
end
|
@@ -9,7 +9,7 @@ module Uploadcare
|
|
9
9
|
class ProjectClient < RestClient
|
10
10
|
# get information about current project
|
11
11
|
# current project is determined by public and secret key combination
|
12
|
-
# @see https://uploadcare.com/api-refs/rest-api/v0.
|
12
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/Project
|
13
13
|
def show
|
14
14
|
get(uri: '/project/')
|
15
15
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative 'rest_client'
|
4
|
+
require 'dry/monads'
|
4
5
|
require 'api_struct'
|
5
6
|
require 'uploadcare/concern/error_handler'
|
6
7
|
require 'uploadcare/concern/throttle_handler'
|
@@ -32,19 +33,19 @@ module Uploadcare
|
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
35
|
-
def get(
|
36
|
+
def get(options = {})
|
36
37
|
request(method: 'GET', **options)
|
37
38
|
end
|
38
39
|
|
39
|
-
def post(
|
40
|
+
def post(options = {})
|
40
41
|
request(method: 'POST', **options)
|
41
42
|
end
|
42
43
|
|
43
|
-
def put(
|
44
|
+
def put(options = {})
|
44
45
|
request(method: 'PUT', **options)
|
45
46
|
end
|
46
47
|
|
47
|
-
def delete(
|
48
|
+
def delete(options = {})
|
48
49
|
request(method: 'DELETE', **options)
|
49
50
|
end
|
50
51
|
|
@@ -55,7 +56,7 @@ module Uploadcare
|
|
55
56
|
def headers
|
56
57
|
{
|
57
58
|
'Content-Type': 'application/json',
|
58
|
-
'Accept': 'application/vnd.uploadcare-v0.
|
59
|
+
'Accept': 'application/vnd.uploadcare-v0.7+json',
|
59
60
|
'User-Agent': Uploadcare::Param::UserAgent.call
|
60
61
|
}
|
61
62
|
end
|
@@ -4,20 +4,40 @@ require_relative 'rest_client'
|
|
4
4
|
|
5
5
|
module Uploadcare
|
6
6
|
module Client
|
7
|
-
# @see https://uploadcare.com/api-refs/rest-api/v0.
|
7
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/Group/paths/~1groups~1%3Cuuid%3E~1storage~1/put
|
8
8
|
class RestGroupClient < RestClient
|
9
9
|
# store all files in a group
|
10
|
-
# @see https://uploadcare.com/api-refs/rest-api/v0.
|
10
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/storeFile
|
11
11
|
def store(uuid)
|
12
|
-
|
12
|
+
files = info(uuid).success[:files].compact
|
13
|
+
client = ::Uploadcare::Client::FileClient.new
|
14
|
+
files.each_slice(Uploadcare.config.file_chunk_size) do |file_chunk|
|
15
|
+
file_chunk.each do |file|
|
16
|
+
client.store(file[:uuid])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Dry::Monads::Success(nil)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Get a file group by its ID.
|
24
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/groupInfo
|
25
|
+
def info(uuid)
|
26
|
+
get(uri: "/groups/#{uuid}/")
|
13
27
|
end
|
14
28
|
|
15
29
|
# return paginated list of groups
|
16
|
-
# @see https://uploadcare.com/api-refs/rest-api/v0.
|
30
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/groupsList
|
17
31
|
def list(options = {})
|
18
32
|
query = options.empty? ? '' : "?#{URI.encode_www_form(options)}"
|
19
33
|
get(uri: "/groups/#{query}")
|
20
34
|
end
|
35
|
+
|
36
|
+
# Delete a file group by its ID.
|
37
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/deleteGroup
|
38
|
+
def delete(uuid)
|
39
|
+
request(method: 'DELETE', uri: "/groups/#{uuid}/")
|
40
|
+
end
|
21
41
|
end
|
22
42
|
end
|
23
43
|
end
|
@@ -1,9 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'dry/monads'
|
3
4
|
require 'api_struct'
|
4
5
|
require 'param/user_agent'
|
5
6
|
require 'uploadcare/concern/error_handler'
|
6
7
|
require 'uploadcare/concern/throttle_handler'
|
8
|
+
require 'mimemagic'
|
7
9
|
|
8
10
|
module Uploadcare
|
9
11
|
module Client
|
@@ -30,7 +32,8 @@ module Uploadcare
|
|
30
32
|
|
31
33
|
def form_data_for(file)
|
32
34
|
filename = file.original_filename if file.respond_to?(:original_filename)
|
33
|
-
mime_type =
|
35
|
+
mime_type = MimeMagic.by_magic(file)&.type
|
36
|
+
mime_type = file.content_type if mime_type.nil? && file.respond_to?(:content_type)
|
34
37
|
options = { filename: filename, content_type: mime_type }.compact
|
35
38
|
HTTP::FormData::File.new(file, options)
|
36
39
|
end
|
@@ -12,8 +12,8 @@ module Uploadcare
|
|
12
12
|
class UploaderClient < UploadClient
|
13
13
|
# @see https://uploadcare.com/api-refs/upload-api/#operation/baseUpload
|
14
14
|
|
15
|
-
def upload_many(arr,
|
16
|
-
body = upload_many_body(arr,
|
15
|
+
def upload_many(arr, options = {})
|
16
|
+
body = upload_many_body(arr, options)
|
17
17
|
post(path: 'base/',
|
18
18
|
headers: { 'Content-Type': body.content_type },
|
19
19
|
body: body)
|
@@ -22,8 +22,8 @@ module Uploadcare
|
|
22
22
|
# syntactic sugar for upload_many
|
23
23
|
# There is actual upload method for one file, but it is redundant
|
24
24
|
|
25
|
-
def upload(file,
|
26
|
-
upload_many([file],
|
25
|
+
def upload(file, options = {})
|
26
|
+
upload_many([file], options)
|
27
27
|
end
|
28
28
|
|
29
29
|
# Upload files from url
|
@@ -33,8 +33,9 @@ module Uploadcare
|
|
33
33
|
# - filename
|
34
34
|
# - save_URL_duplicates
|
35
35
|
# - async - returns upload token instead of upload data
|
36
|
-
|
37
|
-
|
36
|
+
# - metadata - file metadata, hash
|
37
|
+
def upload_from_url(url, options = {})
|
38
|
+
body = upload_from_url_body(url, options)
|
38
39
|
token_response = post(path: 'from_url/', headers: { 'Content-Type': body.content_type }, body: body)
|
39
40
|
return token_response if options[:async]
|
40
41
|
|
@@ -55,7 +56,7 @@ module Uploadcare
|
|
55
56
|
private
|
56
57
|
|
57
58
|
alias api_struct_post post
|
58
|
-
def post(
|
59
|
+
def post(args = {})
|
59
60
|
handle_throttling { api_struct_post(**args) }
|
60
61
|
end
|
61
62
|
|
@@ -71,13 +72,13 @@ module Uploadcare
|
|
71
72
|
end
|
72
73
|
|
73
74
|
# Prepares body for upload_many method
|
74
|
-
def upload_many_body(arr,
|
75
|
+
def upload_many_body(arr, options = {})
|
75
76
|
files_formdata = arr.map do |file|
|
76
77
|
[HTTP::FormData::File.new(file).filename,
|
77
78
|
form_data_for(file)]
|
78
79
|
end.to_h
|
79
80
|
HTTP::FormData::Multipart.new(
|
80
|
-
Param::Upload::UploadParamsGenerator.call(options
|
81
|
+
Param::Upload::UploadParamsGenerator.call(options).merge(files_formdata)
|
81
82
|
)
|
82
83
|
end
|
83
84
|
|
@@ -87,7 +88,7 @@ module Uploadcare
|
|
87
88
|
}.freeze
|
88
89
|
|
89
90
|
# Prepare upload_from_url initial request body
|
90
|
-
def upload_from_url_body(url,
|
91
|
+
def upload_from_url_body(url, options = {})
|
91
92
|
HTTP::FormData::Multipart.new(
|
92
93
|
options.merge(
|
93
94
|
'pub_key' => Uploadcare.config.public_key,
|
@@ -5,7 +5,7 @@ require_relative 'rest_client'
|
|
5
5
|
module Uploadcare
|
6
6
|
module Client
|
7
7
|
# client for webhook management
|
8
|
-
# @see https://uploadcare.com/api-refs/rest-api/v0.
|
8
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/Webhook
|
9
9
|
class WebhookClient < RestClient
|
10
10
|
# Create webhook
|
11
11
|
# @see https://uploadcare.com/docs/api_reference/rest/webhooks/#subscribe
|
@@ -30,14 +30,14 @@ module Uploadcare
|
|
30
30
|
# @see https://uploadcare.com/docs/api_reference/rest/webhooks/#unsubscribe
|
31
31
|
def delete(target_url)
|
32
32
|
body = { 'target_url': target_url }.to_json
|
33
|
-
|
33
|
+
request(method: 'DELETE', uri: '/webhooks/unsubscribe/', content: body)
|
34
34
|
end
|
35
35
|
|
36
36
|
# Updates webhook
|
37
37
|
# @see https://uploadcare.com/docs/api_reference/rest/webhooks/#subscribe-update
|
38
38
|
def update(id, options = {})
|
39
39
|
body = options.to_json
|
40
|
-
|
40
|
+
put(uri: "/webhooks/#{id}/", content: body)
|
41
41
|
end
|
42
42
|
|
43
43
|
alias create_webhook create
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uploadcare
|
4
|
+
module Entity
|
5
|
+
# This serializer is responsible for addons handling
|
6
|
+
#
|
7
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/Add-Ons
|
8
|
+
class Addons < Entity
|
9
|
+
client_service AddonsClient
|
10
|
+
|
11
|
+
attr_entity :request_id, :status, :result
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -4,7 +4,7 @@ module Uploadcare
|
|
4
4
|
module Entity
|
5
5
|
module Conversion
|
6
6
|
# This serializer lets a user convert uploaded documents
|
7
|
-
# @see https://uploadcare.com/api-refs/rest-api/v0.
|
7
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/documentConvert
|
8
8
|
class BaseConverter < Entity
|
9
9
|
class << self
|
10
10
|
# Converts files
|
@@ -6,11 +6,14 @@ module Uploadcare
|
|
6
6
|
#
|
7
7
|
# @see https://uploadcare.com/docs/api_reference/rest/handling_projects/
|
8
8
|
class File < Entity
|
9
|
+
RESPONSE_PARAMS = %i[
|
10
|
+
datetime_removed datetime_stored datetime_uploaded is_image is_ready mime_type original_file_url
|
11
|
+
original_filename size url uuid variations content_info metadata appdata source
|
12
|
+
].freeze
|
13
|
+
|
9
14
|
client_service FileClient
|
10
15
|
|
11
|
-
attr_entity
|
12
|
-
:mime_type, :original_file_url, :original_filename, :size, :url, :uuid, :variations, :video_info,
|
13
|
-
:source, :rekognition_info
|
16
|
+
attr_entity(*RESPONSE_PARAMS)
|
14
17
|
|
15
18
|
# gets file's uuid - even if it's only initialized with url
|
16
19
|
# @returns [String]
|
@@ -40,64 +43,43 @@ module Uploadcare
|
|
40
43
|
convert_file(params, converter, options)
|
41
44
|
end
|
42
45
|
|
43
|
-
# 'copy' method is used to copy original files or their modified versions to default storage.
|
44
|
-
#
|
45
|
-
# Source files MAY either be stored or just uploaded and MUST NOT be deleted.
|
46
|
-
#
|
47
|
-
# @param [String] source uuid or uploadcare link to file.
|
48
|
-
# @param [Hash] args
|
49
|
-
# @option args [Boolean] :store Whether to store the file
|
50
|
-
# @option args [Boolean] :strip_operations Copies file without transformations (if source has them)
|
51
|
-
# @option args [String] :target points to a target custom storage.
|
52
|
-
# @option args [Boolean] :make_public make files on custom storage available via public links.
|
53
|
-
# @option args [String] :pattern define file naming pattern for the custom storage scenario.
|
54
|
-
#
|
55
|
-
# @see https://uploadcare.com/api-refs/rest-api/v0.5.0/#operation/copyFile
|
56
|
-
def self.copy(source, **args)
|
57
|
-
response = FileClient.new.copy(source: source, **args).success[:result]
|
58
|
-
File.new(response)
|
59
|
-
end
|
60
|
-
|
61
46
|
# Copies file to current project
|
62
47
|
#
|
63
48
|
# source can be UID or full CDN link
|
64
49
|
#
|
65
|
-
# @see .
|
66
|
-
def self.local_copy(source,
|
67
|
-
|
50
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File/operation/createLocalCopy
|
51
|
+
def self.local_copy(source, args = {})
|
52
|
+
response = FileClient.new.local_copy(source: source, **args).success[:result]
|
53
|
+
File.new(response)
|
68
54
|
end
|
69
55
|
|
70
56
|
# copy file to different project
|
71
57
|
#
|
72
58
|
# source can be UID or full CDN link
|
73
59
|
#
|
74
|
-
# @see .
|
75
|
-
def self.remote_copy(source, target,
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
# Instance version of #{copy}. Copies current file.
|
80
|
-
def copy(**args)
|
81
|
-
File.copy(uuid, **args)
|
60
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File/operation/createRemoteCopy
|
61
|
+
def self.remote_copy(source, target, args = {})
|
62
|
+
response = FileClient.new.remote_copy(source: source, target: target, **args).success[:result]
|
63
|
+
File.new(response)
|
82
64
|
end
|
83
65
|
|
84
66
|
# Instance version of {internal_copy}
|
85
|
-
def local_copy(
|
67
|
+
def local_copy(args = {})
|
86
68
|
File.local_copy(uuid, **args)
|
87
69
|
end
|
88
70
|
|
89
71
|
# Instance version of {external_copy}
|
90
|
-
def remote_copy(target,
|
91
|
-
File.
|
72
|
+
def remote_copy(target, args = {})
|
73
|
+
File.remote_copy(uuid, target, **args)
|
92
74
|
end
|
93
75
|
|
94
76
|
# Store a single file, preventing it from being deleted in 2 weeks
|
95
|
-
# @see https://uploadcare.com/api-refs/rest-api/v0.
|
77
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/storeFile
|
96
78
|
def store
|
97
79
|
File.store(uuid)
|
98
80
|
end
|
99
81
|
|
100
|
-
# @see https://uploadcare.com/api-refs/rest-api/v0.
|
82
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/deleteFileStorage
|
101
83
|
def delete
|
102
84
|
File.delete(uuid)
|
103
85
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uploadcare
|
4
|
+
module Entity
|
5
|
+
# This serializer is responsible for file metadata handling
|
6
|
+
#
|
7
|
+
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File-metadata
|
8
|
+
class FileMetadata < Entity
|
9
|
+
client_service FileMetadataClient
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def index(uuid)
|
13
|
+
::Uploadcare::Client::FileMetadataClient.new.index(uuid).success
|
14
|
+
end
|
15
|
+
|
16
|
+
def show(uuid, key)
|
17
|
+
::Uploadcare::Client::FileMetadataClient.new.show(uuid, key).success
|
18
|
+
end
|
19
|
+
|
20
|
+
def update(uuid, key, value)
|
21
|
+
::Uploadcare::Client::FileMetadataClient.new.update(uuid, key, value).success
|
22
|
+
end
|
23
|
+
|
24
|
+
def delete(uuid, key)
|
25
|
+
::Uploadcare::Client::FileMetadataClient.new.delete(uuid, key).success || '200 OK'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -10,7 +10,7 @@ module Uploadcare
|
|
10
10
|
#
|
11
11
|
# @see https://uploadcare.com/docs/api_reference/upload/groups/
|
12
12
|
class Group < Entity
|
13
|
-
client_service RestGroupClient, prefix: 'rest', only:
|
13
|
+
client_service RestGroupClient, prefix: 'rest', only: %i[store info delete]
|
14
14
|
client_service GroupClient
|
15
15
|
|
16
16
|
attr_entity :id, :datetime_created, :datetime_stored, :files_count, :cdn_url, :url
|
@@ -19,7 +19,16 @@ module Uploadcare
|
|
19
19
|
# Remove these lines and bump api_struct version when this PR is accepted:
|
20
20
|
# @see https://github.com/rubygarage/api_struct/pull/15
|
21
21
|
def self.store(uuid)
|
22
|
-
rest_store(uuid)
|
22
|
+
rest_store(uuid).success || '200 OK'
|
23
|
+
end
|
24
|
+
|
25
|
+
# Get a file group by its ID.
|
26
|
+
def self.group_info(uuid)
|
27
|
+
rest_info(uuid)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.delete(uuid)
|
31
|
+
rest_delete(uuid).success || '200 OK'
|
23
32
|
end
|
24
33
|
|
25
34
|
# gets groups's id - even if it's only initialized with cdn_url
|
@@ -16,42 +16,42 @@ module Uploadcare
|
|
16
16
|
# @param object [Array], [String] or [File]
|
17
17
|
# @param [Hash] options options for upload
|
18
18
|
# @option options [Boolean] :store (false) whether to store file on servers.
|
19
|
-
def self.upload(object,
|
19
|
+
def self.upload(object, options = {})
|
20
20
|
if big_file?(object)
|
21
|
-
multipart_upload(object,
|
21
|
+
multipart_upload(object, options)
|
22
22
|
elsif file?(object)
|
23
|
-
upload_file(object,
|
23
|
+
upload_file(object, options)
|
24
24
|
elsif object.is_a?(Array)
|
25
|
-
upload_files(object,
|
25
|
+
upload_files(object, options)
|
26
26
|
elsif object.is_a?(String)
|
27
|
-
upload_from_url(object,
|
27
|
+
upload_from_url(object, options)
|
28
28
|
else
|
29
29
|
raise ArgumentError, "Expected input to be a file/Array/URL, given: `#{object}`"
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
# upload single file
|
34
|
-
def self.upload_file(file,
|
35
|
-
response = UploaderClient.new.upload_many([file],
|
34
|
+
def self.upload_file(file, options = {})
|
35
|
+
response = UploaderClient.new.upload_many([file], options)
|
36
36
|
Uploadcare::Entity::File.info(response.success.to_a.flatten[-1])
|
37
37
|
end
|
38
38
|
|
39
39
|
# upload multiple files
|
40
|
-
def self.upload_files(arr,
|
41
|
-
response = UploaderClient.new.upload_many(arr,
|
40
|
+
def self.upload_files(arr, options = {})
|
41
|
+
response = UploaderClient.new.upload_many(arr, options)
|
42
42
|
response.success.map { |pair| Uploadcare::Entity::File.new(uuid: pair[1], original_filename: pair[0]) }
|
43
43
|
end
|
44
44
|
|
45
45
|
# upload file of size above 10mb (involves multipart upload)
|
46
|
-
def self.multipart_upload(file,
|
47
|
-
response = MultipartUploaderClient.new.upload(file,
|
46
|
+
def self.multipart_upload(file, options = {}, &block)
|
47
|
+
response = MultipartUploaderClient.new.upload(file, options, &block)
|
48
48
|
Uploadcare::Entity::File.new(response.success)
|
49
49
|
end
|
50
50
|
|
51
51
|
# upload files from url
|
52
52
|
# @param url [String]
|
53
|
-
def self.upload_from_url(url,
|
54
|
-
response = UploaderClient.new.upload_from_url(url,
|
53
|
+
def self.upload_from_url(url, options = {})
|
54
|
+
response = UploaderClient.new.upload_from_url(url, options)
|
55
55
|
return response.success[:token] unless response.success[:files]
|
56
56
|
|
57
57
|
response.success[:files].map { |file_data| Uploadcare::Entity::File.new(file_data) }
|
@@ -10,10 +10,10 @@ module Uploadcare
|
|
10
10
|
# This authentication method is more secure, but more tedious
|
11
11
|
class AuthenticationHeader
|
12
12
|
# @see https://uploadcare.com/docs/api_reference/rest/requests_auth/#auth-uploadcare
|
13
|
-
def self.call(
|
13
|
+
def self.call(options = {})
|
14
14
|
case Uploadcare.config.auth_type
|
15
15
|
when 'Uploadcare'
|
16
|
-
SecureAuthHeader.call(
|
16
|
+
SecureAuthHeader.call(options)
|
17
17
|
when 'Uploadcare.Simple'
|
18
18
|
SimpleAuthHeader.call
|
19
19
|
else
|
@@ -8,7 +8,7 @@ module Uploadcare
|
|
8
8
|
# This authentication method is more secure, but more tedious
|
9
9
|
class SecureAuthHeader
|
10
10
|
# @see https://uploadcare.com/docs/api_reference/rest/requests_auth/#auth-uploadcare
|
11
|
-
def self.call(
|
11
|
+
def self.call(options = {})
|
12
12
|
@method = options[:method]
|
13
13
|
@body = options[:content] || ''
|
14
14
|
@content_type = options[:content_type]
|
@@ -8,14 +8,31 @@ module Uploadcare
|
|
8
8
|
# This class generates body params for uploads
|
9
9
|
class UploadParamsGenerator
|
10
10
|
# @see https://uploadcare.com/docs/api_reference/upload/request_based/
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
11
|
+
class << self
|
12
|
+
def call(options = {})
|
13
|
+
{
|
14
|
+
'UPLOADCARE_PUB_KEY' => Uploadcare.config.public_key,
|
15
|
+
'UPLOADCARE_STORE' => store(options[:store]),
|
16
|
+
'signature' => (Upload::SignatureGenerator.call if Uploadcare.config.sign_uploads)
|
17
|
+
}.merge(metadata(options)).compact
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def store(store)
|
23
|
+
store = 'auto' if store.nil?
|
24
|
+
store = '0' if store == false
|
25
|
+
store = '1' if store == true
|
26
|
+
store
|
27
|
+
end
|
28
|
+
|
29
|
+
def metadata(options = {})
|
30
|
+
return {} if options[:metadata].nil?
|
31
|
+
|
32
|
+
options[:metadata].each_with_object({}) do |(k, v), res|
|
33
|
+
res.merge!("metadata[#{k}]" => v)
|
34
|
+
end
|
35
|
+
end
|
19
36
|
end
|
20
37
|
end
|
21
38
|
end
|
data/lib/uploadcare.rb
CHANGED
data/uploadcare-ruby.gemspec
CHANGED
@@ -7,8 +7,7 @@ require 'uploadcare/ruby/version'
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
8
|
spec.name = 'uploadcare-ruby'
|
9
9
|
spec.version = Uploadcare::VERSION
|
10
|
-
spec.authors = ['
|
11
|
-
spec.email = ['stepan.redka@railsmuffin.com']
|
10
|
+
spec.authors = ['@dmitrijivanchenko (Dmitrij Ivanchenko), @T0mbery (Andrey Aksenov)']
|
12
11
|
|
13
12
|
spec.summary = 'Ruby wrapper for uploadcare API'
|
14
13
|
spec.description = spec.summary
|
@@ -38,10 +37,18 @@ Gem::Specification.new do |spec|
|
|
38
37
|
spec.require_paths = ['lib', 'lib/uploadcare', 'lib/uploadcare/rest']
|
39
38
|
|
40
39
|
spec.add_dependency 'api_struct', '~> 1.0.1'
|
41
|
-
spec.add_dependency '
|
40
|
+
spec.add_dependency 'mimemagic'
|
42
41
|
spec.add_dependency 'parallel'
|
43
42
|
spec.add_dependency 'retries'
|
44
43
|
|
44
|
+
# rubocop:disable Gemspec/RubyVersionGlobalsUsage
|
45
|
+
if RUBY_VERSION.start_with?('3')
|
46
|
+
spec.add_development_dependency 'dry-configurable', '0.13.0'
|
47
|
+
else
|
48
|
+
spec.add_development_dependency 'dry-configurable'
|
49
|
+
end
|
50
|
+
# rubocop:enable Gemspec/RubyVersionGlobalsUsage
|
51
|
+
|
45
52
|
spec.add_development_dependency 'byebug'
|
46
53
|
spec.add_development_dependency 'rake', '~> 13.0'
|
47
54
|
spec.add_development_dependency 'rspec', '~> 3.0'
|