Skip to content

Commit 1eee108

Browse files
authored
Merge pull request #39 from bishosilwal/fix-issue-with-video-upload
Fixes issue with video upload
2 parents d959699 + a8d189a commit 1eee108

File tree

9 files changed

+61
-20
lines changed

9 files changed

+61
-20
lines changed

imagekitio.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ Gem::Specification.new do |spec|
3030
spec.add_dependency 'rest-client', '~> 2.1', ">=2.1"
3131
spec.add_dependency 'addressable', '~> 2.8'
3232
spec.add_dependency 'activestorage', '>= 5.2.0'
33-
# spec.add_development_dependency "sqlite3"
33+
spec.add_dependency 'multipart-post', '>= 2.1.0'
3434
spec.add_development_dependency "rails", "~> 5.2.0", ">= 5.2.0"
3535
end

lib/active_storage/service/image_kit_io_service.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
if defined? Rails
44
# Overwrite the ActiveStorage::Downloader's open method and remove the file integrity check constraint method verify_integrity_of
55
class DownloaderExtension < ::ActiveStorage::Downloader
6-
def open(key, checksum:, name: "ActiveStorage-", tmpdir: nil)
6+
def open(key, checksum:, name: "ActiveStorage-", tmpdir: nil, **options)
77
open_tempfile(name, tmpdir) do |file|
88
download key, file
99
# verify_integrity_of file, checksum: checksum
@@ -71,9 +71,11 @@ def initialize(**options)
7171
def upload(key, io, checksum: nil, **options)
7272
instrument :upload, key: key, checksum: checksum do
7373
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)
7575
if response[:error].nil?
7676
blob.update_columns(metadata: response[:response].transform_keys(&:to_sym))
77+
else
78+
raise Exception.new response[:error]
7779
end
7880
end
7981
end

lib/imagekitio/api_service/file.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22
require_relative '../constant'
33
require_relative '../utils/option_validator'
4+
require 'net/http/post/multipart'
45

56
module ImageKitIo
67
module ApiService
@@ -32,14 +33,20 @@ def upload(file: nil, file_name: nil, **options)
3233
raise ArgumentError, constants.MISSING_UPLOAD_FILE_PARAMETER unless file
3334
raise ArgumentError, constants.MISSING_UPLOAD_FILE_PARAMETER unless file_name
3435

36+
content_type = options.delete(:content_type) || ''
3537
options = format_to_json(options, :extensions, Array)
3638
options = format_to_json(options, :custom_metadata, Hash)
3739
options = validate_upload_options(options || {})
3840
if options.is_a?(FalseClass)
3941
raise ArgumentError, "Invalid Upload option"
4042
else
4143
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)
4350
url = "#{constants.BASE_URL}#{constants.UPLOAD}"
4451
@req_obj.request("post", url, headers, payload)
4552
end
@@ -157,6 +164,12 @@ def rename(file_path: nil, new_file_name: nil, **options)
157164
payload = { 'filePath': file_path, 'newFileName': new_file_name }.merge(request_formatter(options)).to_json
158165
@req_obj.request('put', url, @req_obj.create_headers, payload)
159166
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
160173
end
161174
end
162175
end

lib/imagekitio/constants/file.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module File
55

66
VALID_FILE_DETAIL_OPTIONS = ["fileID"]
77

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 ]
99
end
1010
end
1111
end

lib/imagekitio/request.rb

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "base64"
44
require "rest-client"
55
require "json"
6+
require 'net/http/post/multipart'
67
require_relative './constant'
78
# Request requests and sends data from server
89
module ImageKitIo
@@ -33,28 +34,39 @@ def auth_headers
3334
# request method communicates with server
3435
def request(method, url, headers = create_headers, payload = nil)
3536
headers ||= create_headers
36-
response = {response: nil, error: nil}
37+
response = {}
3738
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")
4557
response[:response] = JSON.parse(resp.body.to_s)
4658
else
47-
raise =RestClient::ExceptionWithResponse
59+
raise RestClient::ExceptionWithResponse, OpenStruct.new(code: 404, body: resp.body)
4860
end
49-
elsif resp.code == 204
61+
elsif resp.code.to_i == 204
5062
response[:response] = {'success': true}
5163
end
5264

5365
rescue RestClient::ExceptionWithResponse => err
54-
response[:error] = if err.http_code == 404
66+
response[:error] = if err.http_code.to_i == 404
5567
{'message': err.response.to_s}
5668
else
57-
JSON.parse(err.response)
69+
err.response.is_a?(OpenStruct) ? JSON.parse(err.response.body) : JSON.parse(err.response)
5870
end
5971
end
6072
response

lib/imagekitio/sdk/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module ImageKitIo
22
module Sdk
3-
VERSION = '2.0.1'
3+
VERSION = '2.1.0'
44
end
55
end

test/imagekit/api_service/file_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
file = open("test/imagekit/dummy_data/sample.jpg", "rb")
7777
upload = SUT.upload(file: file, file_name: "my_file_name")
7878
expect(@ac[:payload].keys).to eq([:multipart, :file, :fileName])
79-
expect(@ac[:payload][:file]).to eq(file)
79+
expect(@ac[:payload][:file].io).to eq(file)
8080
expect(@ac[:payload][:fileName]).to eq('my_file_name')
8181
expect(upload[:code]).to eq(200)
8282

test/imagekit/client_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
SUT = ImageKitIo::Client.new(private_key, public_key, url_endpoint)
7070
SUT.set_ik_request(request_obj)
7171

72-
upload = SUT.upload_file(file: "fakefile.jpg", file_name: "fake")
72+
upload = SUT.upload_file(file: "fakefile.jpg", file_name: "fake", content_type: 'image/jpeg')
7373

7474
expect(upload[:code]).to eq(200)
7575
end

test/imagekit/request_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,21 @@
4949

5050
it "test_request_method_204" do
5151
response = @request_obj.request(:get, "https://www.example204.com")
52+
expect(response[:response]).to have_key(:success)
53+
expect(response).to_not have_key(:error)
54+
end
55+
56+
it 'test_request_method_non_JSON_fail_with_post' do
57+
stub_request(:post, 'https://www.exampleservererror/upload').to_return(status: 400, body: '{"message": "Server failed"}', headers: {content_type: 'application/json'})
58+
response = @request_obj.request(:post, 'https://www.exampleservererror/upload', nil, [])
5259
expect(response).to have_key(:error)
5360
end
61+
62+
it 'test_request_method_JSON_success_with_post' do
63+
stub_request(:post, 'https://www.exampleservererror/upload').to_return(status: 200, body: '{"id": "1"}', headers: {content_type: 'application/json'})
64+
response = @request_obj.request(:post, 'https://www.exampleservererror/upload', nil, [])
65+
expect(response).to_not have_key(:error)
66+
expect(response).to have_key(:response)
67+
end
5468
end
5569
end

0 commit comments

Comments
 (0)