imagekitio 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db316e0a4525fa5a4f72d3f993da7fdb12480e18957cf40f0a84ec40c255ab2e
4
- data.tar.gz: f24d4d783f368bb5ba90807f3dec82f677f943a9ccff4373dd6d56471e524517
3
+ metadata.gz: 0e277c2a9acd03cb3c09568a169a4634e7d67a6aa6e1950530f3c0c520912b2a
4
+ data.tar.gz: 3b937a53d53b95dda07d35a8161e6fa46a2a0d66246183b1cbc40d92f73f948f
5
5
  SHA512:
6
- metadata.gz: 5cd39012d46c350b0cbaefce9dfe60fc7bdf5e8fd110fc01e05b06fa95c9bff171c679dc0d724cc41ddf1b968ea2f3fef3dd30e867d6f2da1935d0e4cd02afd8
7
- data.tar.gz: 360f33ade97be7c1c72e7c6be026008688dfced40b4201ba17d33b180f93270070a77e23718072fb779386f1707abb3c7f1b2ba6ba4ff5102cc03ca4747ad809
6
+ metadata.gz: 7856a96dabd8e21f0ac68e519f2dbca74c20a9fbe9d6915b4ba74ba965609856bfe7ba6e15a96d6d92ab7692fa4002c2b9bd90794369e7436b54735358718cc9
7
+ data.tar.gz: 8d221bbef283cdd2c9578dfd590a2e7f570b6de0c29643b5b8b6cf86e36001227dbfb4aa89e0c35e5da5e6e0821da484581305780087a44bfc09b2658a614431
@@ -3,7 +3,7 @@ require_relative './ik_file'
3
3
  if defined? Rails
4
4
  # Overwrite the ActiveStorage::Downloader's open method and remove the file integrity check constraint method verify_integrity_of
5
5
  class DownloaderExtension < ::ActiveStorage::Downloader
6
- def open(key, checksum:, name: "ActiveStorage-", tmpdir: nil)
6
+ def open(key, checksum:, name: "ActiveStorage-", tmpdir: nil, **options)
7
7
  open_tempfile(name, tmpdir) do |file|
8
8
  download key, file
9
9
  # verify_integrity_of file, checksum: checksum
@@ -71,9 +71,11 @@ if defined? Rails
71
71
  def upload(key, io, checksum: nil, **options)
72
72
  instrument :upload, key: key, checksum: checksum do
73
73
  blob = storage_blob(key)
74
- response = client.upload_file(file: io, file_name: blob.filename.to_s)
74
+ response = client.upload_file(file: io, file_name: blob.filename.to_s, content_type: blob.content_type)
75
75
  if response[:error].nil?
76
76
  blob.update_columns(metadata: response[:response].transform_keys(&:to_sym))
77
+ else
78
+ raise Exception.new response[:error]
77
79
  end
78
80
  end
79
81
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  require_relative '../constant'
3
3
  require_relative '../utils/option_validator'
4
+ require 'net/http/post/multipart'
4
5
 
5
6
  module ImageKitIo
6
7
  module ApiService
@@ -32,6 +33,7 @@ module ImageKitIo
32
33
  raise ArgumentError, constants.MISSING_UPLOAD_FILE_PARAMETER unless file
33
34
  raise ArgumentError, constants.MISSING_UPLOAD_FILE_PARAMETER unless file_name
34
35
 
36
+ content_type = options.delete(:content_type) || ''
35
37
  options = format_to_json(options, :extensions, Array)
36
38
  options = format_to_json(options, :custom_metadata, Hash)
37
39
  options = validate_upload_options(options || {})
@@ -39,7 +41,12 @@ module ImageKitIo
39
41
  raise ArgumentError, "Invalid Upload option"
40
42
  else
41
43
  headers = @req_obj.create_headers
42
- payload = {multipart: true, file: file, fileName: file_name}.merge(options)
44
+ payload = {
45
+ multipart: true,
46
+ file: file.is_a?(String) ? file : ::UploadIO.new(file, content_type, file_name),
47
+ fileName: file_name
48
+ }
49
+ payload.merge!(options)
43
50
  url = "#{constants.BASE_URL}#{constants.UPLOAD}"
44
51
  @req_obj.request("post", url, headers, payload)
45
52
  end
@@ -157,6 +164,12 @@ module ImageKitIo
157
164
  payload = { 'filePath': file_path, 'newFileName': new_file_name }.merge(request_formatter(options)).to_json
158
165
  @req_obj.request('put', url, @req_obj.create_headers, payload)
159
166
  end
167
+
168
+
169
+ private
170
+ def image_format?(type)
171
+ %(image/jpeg image/bmp image/apng image/avif image/gif image/ief image/svg+xml image/tiff image/x-icon image/rgb image/webp).include?(type)
172
+ end
160
173
  end
161
174
  end
162
175
  end
@@ -5,7 +5,7 @@ module ImageKitIo
5
5
 
6
6
  VALID_FILE_DETAIL_OPTIONS = ["fileID"]
7
7
 
8
- VALID_UPLOAD_OPTIONS = %w[file file_name use_unique_file_name tags folder is_private_file custom_coordinates response_fields extensions webhook_url overwrite_file overwrite_AI_tags overwrite_custom_metadata custom_metadata mime overwrite_tags ]
8
+ VALID_UPLOAD_OPTIONS = %w[file file_name use_unique_file_name tags folder is_private_file custom_coordinates response_fields extensions webhook_url overwrite_file overwrite_AI_tags overwrite_custom_metadata custom_metadata mime overwrite_tags content_type ]
9
9
  end
10
10
  end
11
11
  end
@@ -3,6 +3,7 @@
3
3
  require "base64"
4
4
  require "rest-client"
5
5
  require "json"
6
+ require 'net/http/post/multipart'
6
7
  require_relative './constant'
7
8
  # Request requests and sends data from server
8
9
  module ImageKitIo
@@ -33,28 +34,39 @@ module ImageKitIo
33
34
  # request method communicates with server
34
35
  def request(method, url, headers = create_headers, payload = nil)
35
36
  headers ||= create_headers
36
- response = {response: nil, error: nil}
37
+ response = {}
37
38
  begin
38
- resp = RestClient::Request.new(method: method,
39
- url: url,
40
- headers: headers,
41
- payload: payload).execute
42
-
43
- if (resp.code >= 200) && (resp.code < 204)
44
- if (resp.headers[:content_type].include? "application/json")
39
+ if(method.downcase.to_sym == :post)
40
+ uri = URI.parse(url)
41
+ http = Net::HTTP.new(uri.host, uri.port)
42
+ http.use_ssl = (uri.scheme == 'https')
43
+ req = Net::HTTP::Post::Multipart.new uri.path, payload, headers
44
+ resp = http.request(req)
45
+ if resp.code.to_i == 400
46
+ raise RestClient::ExceptionWithResponse, OpenStruct.new(code: 400, body: resp.body)
47
+ end
48
+ else
49
+ resp = RestClient::Request.new(method: method,
50
+ url: url,
51
+ headers: headers,
52
+ payload: payload).execute
53
+ end
54
+ if (resp.code.to_i >= 200) && (resp.code.to_i < 204)
55
+ content_type = resp.respond_to?(:headers) ? resp.headers[:content_type] : resp.content_type
56
+ if (content_type.include? "application/json")
45
57
  response[:response] = JSON.parse(resp.body.to_s)
46
58
  else
47
- raise =RestClient::ExceptionWithResponse
59
+ raise RestClient::ExceptionWithResponse, OpenStruct.new(code: 404, body: resp.body)
48
60
  end
49
- elsif resp.code == 204
61
+ elsif resp.code.to_i == 204
50
62
  response[:response] = {'success': true}
51
63
  end
52
64
 
53
65
  rescue RestClient::ExceptionWithResponse => err
54
- response[:error] = if err.http_code == 404
66
+ response[:error] = if err.http_code.to_i == 404
55
67
  {'message': err.response.to_s}
56
68
  else
57
- JSON.parse(err.response)
69
+ err.response.is_a?(OpenStruct) ? JSON.parse(err.response.body) : JSON.parse(err.response)
58
70
  end
59
71
  end
60
72
  response
@@ -1,5 +1,5 @@
1
1
  module ImageKitIo
2
2
  module Sdk
3
- VERSION = '2.0.1'
3
+ VERSION = '2.1.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imagekitio
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ImageKit.io team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-10 00:00:00.000000000 Z
11
+ date: 2022-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carrierwave
@@ -78,6 +78,20 @@ dependencies:
78
78
  - - ">="
79
79
  - !ruby/object:Gem::Version
80
80
  version: 5.2.0
81
+ - !ruby/object:Gem::Dependency
82
+ name: multipart-post
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 2.1.0
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.1.0
81
95
  - !ruby/object:Gem::Dependency
82
96
  name: rails
83
97
  requirement: !ruby/object:Gem::Requirement