Skip to content

Commit a6d8943

Browse files
committed
fixed video upload issue
1 parent d959699 commit a6d8943

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
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: 2 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,7 +71,7 @@ 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))
7777
end

lib/imagekitio/api_service/file.rb

Lines changed: 17 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
@@ -39,7 +40,16 @@ def upload(file: nil, file_name: nil, **options)
3940
raise ArgumentError, "Invalid Upload option"
4041
else
4142
headers = @req_obj.create_headers
42-
payload = {multipart: true, file: file, fileName: file_name}.merge(options)
43+
content_type = options.delete(:content_type)
44+
if(content_type && image_format?(content_type))
45+
payload = { multipart: true, file: file, fileName: file_name }.merge(options)
46+
else
47+
payload = {
48+
'file' => ::UploadIO.new(file, content_type, file_name),
49+
'fileName' => file_name
50+
}
51+
end
52+
payload.merge!(options)
4353
url = "#{constants.BASE_URL}#{constants.UPLOAD}"
4454
@req_obj.request("post", url, headers, payload)
4555
end
@@ -157,6 +167,12 @@ def rename(file_path: nil, new_file_name: nil, **options)
157167
payload = { 'filePath': file_path, 'newFileName': new_file_name }.merge(request_formatter(options)).to_json
158168
@req_obj.request('put', url, @req_obj.create_headers, payload)
159169
end
170+
171+
172+
private
173+
def image_format?(type)
174+
%(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)
175+
end
160176
end
161177
end
162178
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: 17 additions & 8 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
@@ -35,18 +36,26 @@ def request(method, url, headers = create_headers, payload = nil)
3536
headers ||= create_headers
3637
response = {response: nil, error: nil}
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+
else
46+
resp = RestClient::Request.new(method: method,
47+
url: url,
48+
headers: headers,
49+
payload: payload).execute
50+
end
51+
if (resp.code.to_i >= 200) && (resp.code.to_i < 204)
52+
content_type = resp.respond_to?(:headers) ? resp.headers[:content_type] : resp.content_type
53+
if (content_type.include? "application/json")
4554
response[:response] = JSON.parse(resp.body.to_s)
4655
else
4756
raise =RestClient::ExceptionWithResponse
4857
end
49-
elsif resp.code == 204
58+
elsif resp.code.to_i == 204
5059
response[:response] = {'success': true}
5160
end
5261

0 commit comments

Comments
 (0)