From dd2fdedb5faf606e99ddc0799dedc9a1bb7454fb Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Wed, 29 May 2024 00:36:16 +0530 Subject: [PATCH] Fix Uploadcare::File.remote_copy raising response errors --- CHANGELOG.md | 3 + .../rest_api/post_files_remote_copy.rb | 6 +- lib/uploadcare/entity/file.rb | 3 +- .../vcr_cassettes/rest_file_remote_copy.yml | 115 ++++++++++++++++++ spec/support/vcr.rb | 2 + spec/uploadcare/entity/file_spec.rb | 11 +- 6 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 spec/fixtures/vcr_cassettes/rest_file_remote_copy.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index f1d24b7f..b022cdd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +### Fixed +* Fixed that `Uploadcare::File.remote_copy` method raised `ApiStruct::EntityError: {url} must be Hash` error. Added string as response instead of `File` entity instance. + ### Added * `Document Info` API added in `DocumentConverter`. ## 4.4.1 — 2024-04-27 diff --git a/api_examples/rest_api/post_files_remote_copy.rb b/api_examples/rest_api/post_files_remote_copy.rb index 4c7356fe..f60c8beb 100644 --- a/api_examples/rest_api/post_files_remote_copy.rb +++ b/api_examples/rest_api/post_files_remote_copy.rb @@ -2,7 +2,7 @@ Uploadcare.config.public_key = 'YOUR_PUBLIC_KEY' Uploadcare.config.secret_key = 'YOUR_SECRET_KEY' -source = '1bac376c-aa7e-4356-861b-dd2657b5bfd2' +source_object = '1bac376c-aa7e-4356-861b-dd2657b5bfd2' target = 'custom_storage_connected_to_the_project' -copied_file = Uploadcare::File.remote_copy(source, target, make_public: true) -puts copied_file.uuid +copied_file_url = Uploadcare::File.remote_copy(source_object, target, make_public: true) +puts copied_file_url diff --git a/lib/uploadcare/entity/file.rb b/lib/uploadcare/entity/file.rb index 83b77a31..7750f810 100644 --- a/lib/uploadcare/entity/file.rb +++ b/lib/uploadcare/entity/file.rb @@ -64,8 +64,7 @@ def self.local_copy(source, args = {}) # # @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File/operation/createRemoteCopy def self.remote_copy(source, target, args = {}) - response = FileClient.new.remote_copy(source: source, target: target, **args).success[:result] - File.new(response) + FileClient.new.remote_copy(source: source, target: target, **args).success[:result] end # Instance version of {internal_copy} diff --git a/spec/fixtures/vcr_cassettes/rest_file_remote_copy.yml b/spec/fixtures/vcr_cassettes/rest_file_remote_copy.yml new file mode 100644 index 00000000..7d67ba7e --- /dev/null +++ b/spec/fixtures/vcr_cassettes/rest_file_remote_copy.yml @@ -0,0 +1,115 @@ +--- +http_interactions: +- request: + method: get + uri: https://api.uploadcare.com/files/1b959c59-9605-4879-946f-08fdb5ea3e9d/ + body: + encoding: ASCII-8BIT + string: '' + headers: + Content-Type: + - application/json + Accept: + - application/vnd.uploadcare-v0.7+json + User-Agent: + - UploadcareRuby/4.4.1/ (Ruby/3.3.0) + Date: + - Tue, 28 May 2024 14:25:46 GMT + Authorization: + - "" + Connection: + - close + Host: + - api.uploadcare.com + response: + status: + code: 200 + message: OK + headers: + Date: + - Tue, 28 May 2024 14:25:47 GMT + Content-Type: + - application/vnd.uploadcare-v0.7+json + Content-Length: + - '712' + Connection: + - close + Server: + - nginx + Access-Control-Allow-Origin: + - https://uploadcare.com + Vary: + - Accept + Allow: + - DELETE, GET, HEAD, OPTIONS + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Uploadcare-Request-Id: + - f015aae5-5123-4ec7-9a00-f6f168b8f27a + X-Frame-Options: + - SAMEORIGIN + X-Robots-Tag: + - noindex, nofollow, nosnippet, noarchive + body: + encoding: ASCII-8BIT + string: '{"datetime_removed":null,"datetime_stored":"2024-05-23T22:39:29.307033Z","datetime_uploaded":"2024-05-23T22:39:29.139028Z","is_image":true,"is_ready":true,"mime_type":"image/png","original_file_url":"https://ucarecdn.com/1b959c59-9605-4879-946f-08fdb5ea3e9d/image.png","original_filename":"image.png","size":1048620,"url":"https://api.uploadcare.com/files/1b959c59-9605-4879-946f-08fdb5ea3e9d/","uuid":"1b959c59-9605-4879-946f-08fdb5ea3e9d","variations":null,"content_info":{"mime":{"mime":"image/png","type":"image","subtype":"png"},"image":{"dpi":null,"width":2560,"format":"PNG","height":523,"sequence":false,"color_mode":"RGBA","orientation":null,"geo_location":null,"datetime_original":null}},"metadata":{}}' + recorded_at: Tue, 28 May 2024 14:25:47 GMT +- request: + method: post + uri: https://api.uploadcare.com/files/remote_copy/ + body: + encoding: UTF-8 + string: '{"source":"1b959c59-9605-4879-946f-08fdb5ea3e9d","target":"uploadcare-test"}' + headers: + Content-Type: + - application/json + Accept: + - application/vnd.uploadcare-v0.7+json + User-Agent: + - UploadcareRuby/4.4.1/ (Ruby/3.3.0) + Date: + - Tue, 28 May 2024 14:25:47 GMT + Authorization: + - "" + Connection: + - close + Host: + - api.uploadcare.com + response: + status: + code: 200 + message: OK + headers: + Date: + - Tue, 28 May 2024 14:25:48 GMT + Content-Type: + - application/vnd.uploadcare-v0.7+json + Content-Length: + - '99' + Connection: + - close + Server: + - nginx + Access-Control-Allow-Origin: + - https://uploadcare.com + Vary: + - Accept + Allow: + - OPTIONS, POST + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Uploadcare-Request-Id: + - aea85827-bab0-4760-865a-2dd43a830d5f + X-Frame-Options: + - SAMEORIGIN + X-Robots-Tag: + - noindex, nofollow, nosnippet, noarchive + body: + encoding: ASCII-8BIT + string: '{"type":"url","result":"s3://uploadcare-test/1b959c59-9605-4879-946f-08fdb5ea3e9d/image.png"}' + recorded_at: Tue, 28 May 2024 14:25:48 GMT +recorded_with: VCR 6.2.0 diff --git a/spec/support/vcr.rb b/spec/support/vcr.rb index 02841f9b..85a5dfc0 100644 --- a/spec/support/vcr.rb +++ b/spec/support/vcr.rb @@ -6,6 +6,8 @@ VCR.configure do |config| config.cassette_library_dir = 'spec/fixtures/vcr_cassettes' config.hook_into :webmock + config.filter_sensitive_data('') { Uploadcare.config.public_key } + config.filter_sensitive_data('') { Uploadcare.config.secret_key } config.before_record do |i| if i.request.body && i.request.body.size > 1024 * 1024 i.request.body = "Big string (#{i.request.body.size / (1024 * 1024)}) MB" diff --git a/spec/uploadcare/entity/file_spec.rb b/spec/uploadcare/entity/file_spec.rb index 8d0aab79..dccc75e4 100644 --- a/spec/uploadcare/entity/file_spec.rb +++ b/spec/uploadcare/entity/file_spec.rb @@ -44,7 +44,16 @@ module Entity end describe 'external_copy' do - it 'copies file to different project' do + it 'copies file to remote storage' do + VCR.use_cassette('rest_file_remote_copy') do + target = 'uploadcare-test' + uuid = '1b959c59-9605-4879-946f-08fdb5ea3e9d' + file = subject.file(uuid) + expect(file.remote_copy(target)).to match(%r{#{target}/#{uuid}/}) + end + end + + it 'raises an error when project does not have given storage' do VCR.use_cassette('rest_file_external_copy') do file = subject.file('5632fc94-9dff-499f-a373-f69ea6f67ff8') # I don't have custom storage, but this error recognises what this method tries to do