resizing 1.0.2 → 1.1.0.pre
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 +4 -4
- data/.github/workflows/test.yml +2 -7
- data/Gemfile +2 -6
- data/README.md +32 -59
- data/lib/resizing/active_storage/service/resizing_service.rb +2 -6
- data/lib/resizing/carrier_wave/storage/file.rb +16 -35
- data/lib/resizing/carrier_wave/storage/remote.rb +3 -7
- data/lib/resizing/carrier_wave.rb +11 -10
- data/lib/resizing/client.rb +24 -40
- data/lib/resizing/configurable.rb +1 -1
- data/lib/resizing/configuration.rb +16 -6
- data/lib/resizing/http_clientable.rb +3 -3
- data/lib/resizing/mock_client.rb +5 -6
- data/lib/resizing/public_id.rb +4 -5
- data/lib/resizing/version.rb +1 -1
- data/lib/resizing/video/client.rb +116 -0
- data/lib/resizing/video.rb +8 -0
- data/lib/resizing.rb +12 -15
- data/resizing.gemspec +11 -8
- data/test/resizing/carrier_wave/storage/file_test.rb +8 -149
- data/test/resizing/carrier_wave/storage/remote_test.rb +0 -75
- data/test/resizing/carrier_wave_test.rb +37 -99
- data/test/resizing/client_test.rb +11 -96
- data/test/resizing/configuration_test.rb +2 -118
- data/test/resizing/public_id_test.rb +1 -1
- data/test/resizing/video/client_test.rb +158 -0
- data/test/test_helper.rb +9 -89
- data/test/vcr/video/metadata/success.yml +47 -0
- data/test/vcr/video/prepare/success.yml +47 -0
- data/test/vcr/video/upload_completed/success.yml +47 -0
- metadata +53 -45
- data/lib/resizing/active_storage/service.rb +0 -9
- data/lib/resizing/active_storage.rb +0 -7
- data/test/resizing/active_storage_service_test.rb +0 -98
- data/test/resizing/configurable_test.rb +0 -82
- data/test/resizing/constants_test.rb +0 -25
- data/test/resizing/error_test.rb +0 -73
- data/test/resizing/http_clientable_test.rb +0 -84
- data/test/resizing/mock_client_test.rb +0 -75
- data/test/resizing_module_test.rb +0 -206
- /data/{bin → exe}/console +0 -0
- /data/{bin → exe}/generate-changelog +0 -0
- /data/{bin → exe}/setup +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2d601d6b8f87f6440de0c1c4d4dcbb74e7bc386dd3e217b66b8ac259d35939f0
|
|
4
|
+
data.tar.gz: 3f2ce39dcb8879dee7a9717142bfa794e6cad5671e19cdd01afbb5f7dacc6787
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c221c9290ccf562d804bb9f5dca6657568c891ce4c14d825fb8a19627feec1982c09773bfb55304cd5a13b8c4bb786a57d1abe3589684da312fd0167ecfc9a28
|
|
7
|
+
data.tar.gz: ee2dde3b29accd86dae059daf0e164fef634476886fb254b2145e3be33764d3a81308d9eea382a557817951714cbc8d4e98b02318417042a38590a4109334630
|
data/.github/workflows/test.yml
CHANGED
|
@@ -7,23 +7,18 @@ on:
|
|
|
7
7
|
- master
|
|
8
8
|
|
|
9
9
|
permissions:
|
|
10
|
-
id-token: write # This is required for
|
|
10
|
+
id-token: write # This is required for requesting the JWT
|
|
11
11
|
contents: read # This is required for actions/checkout
|
|
12
12
|
|
|
13
13
|
jobs:
|
|
14
14
|
test:
|
|
15
|
-
name: Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }}
|
|
16
15
|
strategy:
|
|
17
16
|
fail-fast: false
|
|
18
17
|
matrix:
|
|
19
|
-
ruby: ['3.1']
|
|
20
|
-
rails: ['6.1', '7.0']
|
|
18
|
+
ruby: ['2.7.6', '3.1', '3.2','3.3']
|
|
21
19
|
|
|
22
20
|
runs-on: ubuntu-22.04
|
|
23
21
|
|
|
24
|
-
env:
|
|
25
|
-
RAILS_VERSION: ${{ matrix.rails }}
|
|
26
|
-
|
|
27
22
|
steps:
|
|
28
23
|
- uses: actions/checkout@v4
|
|
29
24
|
|
data/Gemfile
CHANGED
|
@@ -5,12 +5,8 @@ source 'https://rubygems.org'
|
|
|
5
5
|
# Specify your gem's dependencies in resizing.gemspec
|
|
6
6
|
gemspec
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
rails_version = ENV['RAILS_VERSION'] || '7.0'
|
|
10
|
-
gem 'rails', "~> #{rails_version}"
|
|
11
|
-
|
|
12
|
-
gem 'byebug'
|
|
8
|
+
gem 'rake', '~> 13.0'
|
|
13
9
|
gem 'github_changelog_generator'
|
|
14
10
|
gem 'mysql2'
|
|
11
|
+
gem 'byebug'
|
|
15
12
|
gem 'pry-byebug'
|
|
16
|
-
gem 'rake', '~> 13.0'
|
data/README.md
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
# Resizing
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[](https://github.com/jksy/resizing-gem/actions/workflows/test.yml)
|
|
5
|
-
[](https://codecov.io/gh/jksy/resizing-gem)
|
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/resizing`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
## Requirements
|
|
10
|
-
|
|
11
|
-
- Ruby 3.1.0 or later
|
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
|
12
6
|
|
|
13
7
|
## Installation
|
|
14
8
|
|
|
@@ -26,59 +20,37 @@ Or install it yourself as:
|
|
|
26
20
|
|
|
27
21
|
$ gem install resizing
|
|
28
22
|
|
|
29
|
-
## Configuration
|
|
30
|
-
|
|
31
|
-
```ruby
|
|
32
|
-
Resizing.configure = {
|
|
33
|
-
image_host: 'https://img.resizing.net',
|
|
34
|
-
video_host: 'https://video.resizing.net',
|
|
35
|
-
project_id: 'your-project-id',
|
|
36
|
-
secret_token: 'your-secret-token'
|
|
37
|
-
}
|
|
38
|
-
```
|
|
39
|
-
|
|
40
23
|
## Usage
|
|
41
24
|
|
|
42
|
-
### Basic Client Usage
|
|
43
|
-
|
|
44
|
-
```ruby
|
|
45
|
-
# Initialize client
|
|
46
|
-
client = Resizing::Client.new
|
|
47
|
-
|
|
48
|
-
# Upload image to resizing
|
|
49
|
-
file = File.open('sample.jpg', 'r')
|
|
50
|
-
response = client.post(file)
|
|
51
|
-
# => {
|
|
52
|
-
# "id"=>"a4ed2bf0-a4cf-44fa-9c82-b53e581cb469",
|
|
53
|
-
# "project_id"=>"098a2a0d-0000-0000-0000-000000000000",
|
|
54
|
-
# "content_type"=>"image/jpeg",
|
|
55
|
-
# "latest_version_id"=>"LJY5bxBF7Ryxfr5kC1F.63W8bzp3pcUm",
|
|
56
|
-
# "latest_etag"=>"\"190143614e6c342637584f46f18f8c58\"",
|
|
57
|
-
# "created_at"=>"2020-05-15T15:33:10.711Z",
|
|
58
|
-
# "updated_at"=>"2020-05-15T15:33:10.711Z",
|
|
59
|
-
# "url"=>"/projects/098a2a0d-0000-0000-0000-000000000000/upload/images/a4ed2bf0-a4cf-44fa-9c82-b53e581cb469"
|
|
60
|
-
# }
|
|
61
|
-
|
|
62
|
-
# Generate transformation URL
|
|
63
|
-
image_id = response['id']
|
|
64
|
-
transformation_url = Resizing.url_from_image_id(image_id, nil, ['w_200', 'h_300'])
|
|
65
|
-
# => "https://img.resizing.net/projects/.../upload/images/.../w_200,h_300"
|
|
66
25
|
```
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
26
|
+
# initialize client
|
|
27
|
+
options = {
|
|
28
|
+
project_id: '098a2a0d-0000-0000-0000-000000000000',
|
|
29
|
+
secret_token: '4g1cshg......rbs6'
|
|
30
|
+
}
|
|
31
|
+
client = Resizing::Client.new(options)
|
|
32
|
+
|
|
33
|
+
# upload image to resizing
|
|
34
|
+
file = File.open('sample.jpg', 'r')
|
|
35
|
+
response = client.post(file)
|
|
36
|
+
=> {
|
|
37
|
+
"id"=>"a4ed2bf0-a4cf-44fa-9c82-b53e581cb469",
|
|
38
|
+
"project_id"=>"098a2a0d-0000-0000-0000-000000000000",
|
|
39
|
+
"content_type"=>"image/jpeg",
|
|
40
|
+
"latest_version_id"=>"LJY5bxBF7Ryxfr5kC1F.63W8bzp3pcUm",
|
|
41
|
+
"latest_etag"=>"\"190143614e6c342637584f46f18f8c58\"",
|
|
42
|
+
"created_at"=>"2020-05-15T15:33:10.711Z",
|
|
43
|
+
"updated_at"=>"2020-05-15T15:33:10.711Z",
|
|
44
|
+
"url"=>"/projects/098a2a0d-0000-0000-0000-000000000000/upload/images/a4ed2bf0-a4cf-44fa-9c82-b53e581cb469"
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
name = response['url']
|
|
48
|
+
# get transformation url
|
|
49
|
+
name = response['url']
|
|
50
|
+
transform = {width: 200, height: 300}
|
|
51
|
+
|
|
52
|
+
transformation_url = Resizing.url(name, transform)
|
|
53
|
+
=> "https://www.resizing.net/projects/098a2a0d-0000-0000-0000-000000000000/upload/images/a4ed2bf0-a4cf-44fa-9c82-b53e581cb469/width_200,height_300"
|
|
82
54
|
```
|
|
83
55
|
|
|
84
56
|
## Development
|
|
@@ -89,7 +61,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
89
61
|
|
|
90
62
|
## Contributing
|
|
91
63
|
|
|
92
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/jksy/resizing
|
|
64
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jksy/resizing.
|
|
65
|
+
|
|
93
66
|
|
|
94
67
|
## License
|
|
95
68
|
|
|
@@ -1,19 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'active_storage/service'
|
|
4
|
-
|
|
5
3
|
module Resizing
|
|
6
4
|
module ActiveStorage
|
|
7
5
|
module Service
|
|
8
6
|
# ref.
|
|
9
7
|
# https://github.com/rails/rails/blob/master/activestorage/lib/active_storage/service/s3_service.rb
|
|
10
8
|
#
|
|
11
|
-
# rubocop:disable Metrics/ParameterLists
|
|
9
|
+
# rubocop:disable Lint/UnusedMethodArgument,Metrics/ParameterLists
|
|
12
10
|
class ResizingService < ::ActiveStorage::Service
|
|
13
11
|
# def initialize(bucket:, upload: {}, public: false, **options)
|
|
14
|
-
# rubocop:disable Lint/MissingSuper
|
|
15
12
|
def initialize; end
|
|
16
|
-
# rubocop:enable Lint/MissingSuper
|
|
17
13
|
|
|
18
14
|
def upload(_key, _io, checksum: nil, filename: nil, content_type: nil, disposition: nil, **)
|
|
19
15
|
raise NotImplementedError, 'upload is not implemented'
|
|
@@ -55,7 +51,7 @@ module Resizing
|
|
|
55
51
|
raise NotImplementedError, 'public_url is not implemented'
|
|
56
52
|
end
|
|
57
53
|
end
|
|
58
|
-
# rubocop:enable Metrics/ParameterLists
|
|
54
|
+
# rubocop:enable Lint/UnusedMethodArgument,Metrics/ParameterLists
|
|
59
55
|
end
|
|
60
56
|
end
|
|
61
57
|
end
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
module Resizing
|
|
4
4
|
module CarrierWave
|
|
5
5
|
module Storage
|
|
6
|
-
# rubocop:disable Metrics/ClassLength
|
|
7
6
|
class File
|
|
8
7
|
include ::CarrierWave::Utilities::Uri
|
|
9
8
|
|
|
@@ -29,24 +28,10 @@ module Resizing
|
|
|
29
28
|
@content_type || file.try(:content_type)
|
|
30
29
|
end
|
|
31
30
|
|
|
32
|
-
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
33
31
|
def delete
|
|
34
|
-
|
|
35
|
-
if @public_id.present?
|
|
36
|
-
# Already set from constructor or retrieve
|
|
37
|
-
elsif model.respond_to?(:attribute_was)
|
|
38
|
-
# Try to get the value before changes (for remove! scenario)
|
|
39
|
-
column_value = model.attribute_was(serialization_column) || model.send(:read_attribute,
|
|
40
|
-
serialization_column)
|
|
41
|
-
@public_id = Resizing::PublicId.new(column_value)
|
|
42
|
-
else
|
|
43
|
-
column_value = model.send(:read_attribute, serialization_column)
|
|
44
|
-
@public_id = Resizing::PublicId.new(column_value)
|
|
45
|
-
end
|
|
46
|
-
|
|
32
|
+
@public_id = Resizing::PublicId.new(model.send :read_attribute, serialization_column)
|
|
47
33
|
return if @public_id.empty? # do nothing
|
|
48
34
|
|
|
49
|
-
# 以下、既存のコード(変更なし)
|
|
50
35
|
resp = client.delete(@public_id.image_id)
|
|
51
36
|
if resp['error'] == 'ActiveRecord::RecordNotFound' # 404 not found
|
|
52
37
|
model.send :write_attribute, serialization_column, nil unless model.destroyed?
|
|
@@ -60,7 +45,6 @@ module Resizing
|
|
|
60
45
|
|
|
61
46
|
raise APIError, "raise someone error:#{resp.inspect}"
|
|
62
47
|
end
|
|
63
|
-
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
64
48
|
|
|
65
49
|
def extension
|
|
66
50
|
raise NotImplementedError, 'this method is do not used. maybe'
|
|
@@ -99,30 +83,30 @@ module Resizing
|
|
|
99
83
|
end
|
|
100
84
|
alias path current_path
|
|
101
85
|
|
|
102
|
-
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
103
|
-
# rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
104
86
|
def store(new_file)
|
|
105
87
|
if new_file.is_a?(self.class)
|
|
106
88
|
# new_file.copy_to(path)
|
|
107
89
|
raise NotImplementedError, 'new file is required duplicating'
|
|
108
90
|
end
|
|
109
91
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
92
|
+
if new_file.respond_to? :content_type
|
|
93
|
+
@content_type ||= new_file.content_type
|
|
94
|
+
else
|
|
95
|
+
# guess content-type from extension
|
|
96
|
+
@content_type ||= MIME::Types.type_for(new_file.path).first.content_type
|
|
97
|
+
end
|
|
116
98
|
|
|
117
99
|
original_filename = new_file.try(:original_filename) || new_file.try(:filename) || new_file.try(:path)
|
|
118
|
-
|
|
100
|
+
if original_filename.present?
|
|
101
|
+
original_filename = ::File.basename(original_filename)
|
|
102
|
+
end
|
|
119
103
|
|
|
120
104
|
content = if new_file.respond_to?(:to_io)
|
|
121
105
|
new_file.to_io.tap(&:rewind)
|
|
122
106
|
elsif new_file.respond_to?(:read) && new_file.respond_to?(:rewind)
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
107
|
+
new_file.read.tap do
|
|
108
|
+
new_file.rewind
|
|
109
|
+
end
|
|
126
110
|
else
|
|
127
111
|
new_file
|
|
128
112
|
end
|
|
@@ -141,11 +125,9 @@ module Resizing
|
|
|
141
125
|
|
|
142
126
|
true
|
|
143
127
|
end
|
|
144
|
-
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
145
|
-
# rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
146
128
|
|
|
147
|
-
def name(
|
|
148
|
-
@public_id = PublicId.new(model.send
|
|
129
|
+
def name(options = {})
|
|
130
|
+
@public_id = PublicId.new(model.send :read_attribute, serialization_column)
|
|
149
131
|
CGI.unescape(@public_id.filename)
|
|
150
132
|
end
|
|
151
133
|
|
|
@@ -156,7 +138,6 @@ module Resizing
|
|
|
156
138
|
def retrieve(identifier)
|
|
157
139
|
@public_id = Resizing::PublicId.new(identifier)
|
|
158
140
|
end
|
|
159
|
-
|
|
160
141
|
private
|
|
161
142
|
|
|
162
143
|
attr_reader :uploader
|
|
@@ -214,6 +195,7 @@ module Resizing
|
|
|
214
195
|
parameters.count == 2 && parameters[1].include?(:options)
|
|
215
196
|
end
|
|
216
197
|
|
|
198
|
+
|
|
217
199
|
# def retrieve_from_cache!(identifier)
|
|
218
200
|
# raise NotImplementedError,
|
|
219
201
|
# "Need to implement #retrieve_from_cache! if you want to use #{self.class.name} as a cache storage."
|
|
@@ -229,7 +211,6 @@ module Resizing
|
|
|
229
211
|
# "Need to implement #clean_cache! if you want to use #{self.class.name} as a cache storage."
|
|
230
212
|
# end
|
|
231
213
|
end
|
|
232
|
-
# rubocop:enable Metrics/ClassLength
|
|
233
214
|
end
|
|
234
215
|
end
|
|
235
216
|
end
|
|
@@ -18,13 +18,9 @@ module Resizing
|
|
|
18
18
|
f
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def retrieve!(identifier)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
f = Resizing::CarrierWave::Storage::File.new(uploader, identifier)
|
|
25
|
-
f.retrieve(identifier)
|
|
26
|
-
f
|
|
27
|
-
end
|
|
21
|
+
# def retrieve!(identifier)
|
|
22
|
+
# super
|
|
23
|
+
# end
|
|
28
24
|
|
|
29
25
|
def cache!(new_file)
|
|
30
26
|
f = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
@@ -4,7 +4,6 @@ require 'resizing/carrier_wave/storage/file'
|
|
|
4
4
|
require 'resizing/carrier_wave/storage/remote'
|
|
5
5
|
|
|
6
6
|
module Resizing
|
|
7
|
-
# rubocop:disable Metrics/ModuleLength
|
|
8
7
|
module CarrierWave
|
|
9
8
|
class Railtie < ::Rails::Railtie
|
|
10
9
|
# Railtie skelton codes
|
|
@@ -33,12 +32,11 @@ module Resizing
|
|
|
33
32
|
end
|
|
34
33
|
|
|
35
34
|
def file
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return nil if file_identifier.blank?
|
|
35
|
+
return if identifier.nil?
|
|
36
|
+
return @file unless defined? @file
|
|
39
37
|
|
|
40
38
|
@file ||= Resizing::CarrierWave::Storage::File.new(self)
|
|
41
|
-
@file.retrieve(
|
|
39
|
+
@file.retrieve(identifier)
|
|
42
40
|
@file
|
|
43
41
|
end
|
|
44
42
|
|
|
@@ -47,8 +45,7 @@ module Resizing
|
|
|
47
45
|
|
|
48
46
|
transforms = args.map do |version|
|
|
49
47
|
version = version.intern
|
|
50
|
-
raise "No version is found: #{version}, #{versions.keys} are exists." unless versions.
|
|
51
|
-
|
|
48
|
+
raise "No version is found: #{version}, #{versions.keys} are exists." unless versions.has_key? version
|
|
52
49
|
versions[version].transform_string
|
|
53
50
|
end.compact
|
|
54
51
|
|
|
@@ -111,6 +108,11 @@ module Resizing
|
|
|
111
108
|
read_column
|
|
112
109
|
end
|
|
113
110
|
|
|
111
|
+
def store!
|
|
112
|
+
# DO NOTHING
|
|
113
|
+
super
|
|
114
|
+
end
|
|
115
|
+
|
|
114
116
|
def serialization_column
|
|
115
117
|
model.send(:_mounter, mounted_as).send(:serialization_column)
|
|
116
118
|
end
|
|
@@ -148,7 +150,7 @@ module Resizing
|
|
|
148
150
|
|
|
149
151
|
private
|
|
150
152
|
|
|
151
|
-
# rubocop:disable Metrics/AbcSize
|
|
153
|
+
# rubocop:disable Metrics/AbcSize
|
|
152
154
|
def transform_string_from(processor)
|
|
153
155
|
action = processor.first
|
|
154
156
|
value = processor.second
|
|
@@ -171,7 +173,6 @@ module Resizing
|
|
|
171
173
|
"#{key}_#{value}"
|
|
172
174
|
end.compact.join(',')
|
|
173
175
|
end
|
|
174
|
-
# rubocop:enable Metrics/AbcSize
|
|
176
|
+
# rubocop:enable Metrics/AbcSize
|
|
175
177
|
end
|
|
176
|
-
# rubocop:enable Metrics/ModuleLength
|
|
177
178
|
end
|
data/lib/resizing/client.rb
CHANGED
|
@@ -24,7 +24,6 @@ module Resizing
|
|
|
24
24
|
# }
|
|
25
25
|
#
|
|
26
26
|
#++
|
|
27
|
-
# rubocop:disable Metrics/ClassLength
|
|
28
27
|
class Client
|
|
29
28
|
include Resizing::Constants
|
|
30
29
|
include Resizing::Configurable
|
|
@@ -38,14 +37,13 @@ module Resizing
|
|
|
38
37
|
raise NotImplementedError
|
|
39
38
|
end
|
|
40
39
|
|
|
41
|
-
def post(
|
|
40
|
+
def post(file_or_binary, options = {})
|
|
42
41
|
ensure_content_type(options)
|
|
43
|
-
|
|
44
|
-
filename = gather_filename filename_or_io, options
|
|
42
|
+
filename = gather_filename file_or_binary, options
|
|
45
43
|
|
|
46
44
|
url = build_post_url
|
|
47
45
|
params = {
|
|
48
|
-
image: Faraday::Multipart::FilePart.new(
|
|
46
|
+
image: Faraday::Multipart::FilePart.new(file_or_binary, options[:content_type], filename)
|
|
49
47
|
}
|
|
50
48
|
|
|
51
49
|
response = handle_faraday_error do
|
|
@@ -54,17 +52,17 @@ module Resizing
|
|
|
54
52
|
end
|
|
55
53
|
end
|
|
56
54
|
|
|
57
|
-
handle_create_response(response)
|
|
55
|
+
result = handle_create_response(response)
|
|
56
|
+
result
|
|
58
57
|
end
|
|
59
58
|
|
|
60
|
-
def put(image_id,
|
|
59
|
+
def put(image_id, file_or_binary, options)
|
|
61
60
|
ensure_content_type(options)
|
|
62
|
-
|
|
63
|
-
filename = gather_filename filename_or_io, options
|
|
61
|
+
filename = gather_filename file_or_binary, options
|
|
64
62
|
|
|
65
63
|
url = build_put_url(image_id)
|
|
66
64
|
params = {
|
|
67
|
-
image: Faraday::Multipart::FilePart.new(
|
|
65
|
+
image: Faraday::Multipart::FilePart.new(file_or_binary, options[:content_type], filename)
|
|
68
66
|
}
|
|
69
67
|
|
|
70
68
|
response = handle_faraday_error do
|
|
@@ -73,7 +71,8 @@ module Resizing
|
|
|
73
71
|
end
|
|
74
72
|
end
|
|
75
73
|
|
|
76
|
-
handle_create_response(response)
|
|
74
|
+
result = handle_create_response(response)
|
|
75
|
+
result
|
|
77
76
|
end
|
|
78
77
|
|
|
79
78
|
def delete(image_id)
|
|
@@ -85,7 +84,8 @@ module Resizing
|
|
|
85
84
|
end
|
|
86
85
|
end
|
|
87
86
|
|
|
88
|
-
handle_delete_response(response)
|
|
87
|
+
result = handle_delete_response(response)
|
|
88
|
+
result
|
|
89
89
|
end
|
|
90
90
|
|
|
91
91
|
def metadata(image_id, options = {})
|
|
@@ -97,7 +97,8 @@ module Resizing
|
|
|
97
97
|
end
|
|
98
98
|
end
|
|
99
99
|
|
|
100
|
-
handle_metadata_response(response, options)
|
|
100
|
+
result = handle_metadata_response(response, options)
|
|
101
|
+
result
|
|
101
102
|
end
|
|
102
103
|
|
|
103
104
|
private
|
|
@@ -110,8 +111,9 @@ module Resizing
|
|
|
110
111
|
"#{config.image_host}/projects/#{config.project_id}/upload/images/"
|
|
111
112
|
end
|
|
112
113
|
|
|
113
|
-
def gather_filename
|
|
114
|
-
options[:filename]
|
|
114
|
+
def gather_filename file_or_binary, options
|
|
115
|
+
filename = options[:filename]
|
|
116
|
+
filename ||= file_or_binary.respond_to?(:path) ? File.basename(file_or_binary.path) : nil
|
|
115
117
|
end
|
|
116
118
|
|
|
117
119
|
def build_put_url(image_id)
|
|
@@ -130,20 +132,8 @@ module Resizing
|
|
|
130
132
|
raise ArgumentError, "need options[:content_type] for #{options.inspect}" unless options[:content_type]
|
|
131
133
|
end
|
|
132
134
|
|
|
133
|
-
def ensure_filename_or_io(filename_or_io)
|
|
134
|
-
return if filename_or_io.is_a?(File)
|
|
135
|
-
|
|
136
|
-
# Accept IO-like objects (StringIO, Tempfile, etc.)
|
|
137
|
-
return if filename_or_io.respond_to?(:read) && filename_or_io.respond_to?(:rewind)
|
|
138
|
-
|
|
139
|
-
return if filename_or_io.is_a?(String) && File.exist?(filename_or_io)
|
|
140
|
-
|
|
141
|
-
raise ArgumentError,
|
|
142
|
-
"filename_or_io must be a File object, an IO-like object, or a path to a file (#{filename_or_io.class})"
|
|
143
|
-
end
|
|
144
|
-
|
|
145
135
|
def handle_create_response(response)
|
|
146
|
-
raise APIError,
|
|
136
|
+
raise APIError, "No response is returned" if response.nil?
|
|
147
137
|
|
|
148
138
|
case response.status
|
|
149
139
|
when HTTP_STATUS_OK, HTTP_STATUS_CREATED
|
|
@@ -154,7 +144,7 @@ module Resizing
|
|
|
154
144
|
end
|
|
155
145
|
|
|
156
146
|
def handle_delete_response(response)
|
|
157
|
-
raise APIError,
|
|
147
|
+
raise APIError, "No response is returned" if response.nil?
|
|
158
148
|
|
|
159
149
|
case response.status
|
|
160
150
|
when HTTP_STATUS_OK, HTTP_STATUS_NOT_FOUND
|
|
@@ -167,30 +157,24 @@ module Resizing
|
|
|
167
157
|
def handle_metadata_response(response, options = {})
|
|
168
158
|
when_not_found = options[:when_not_found] || nil
|
|
169
159
|
|
|
170
|
-
raise APIError,
|
|
160
|
+
raise APIError, "No response is returned" if response.nil?
|
|
171
161
|
|
|
172
162
|
case response.status
|
|
173
|
-
when HTTP_STATUS_OK
|
|
163
|
+
when HTTP_STATUS_OK, HTTP_STATUS_NOT_FOUND
|
|
174
164
|
JSON.parse(response.body)
|
|
175
165
|
when HTTP_STATUS_NOT_FOUND
|
|
176
166
|
raise decode_error_from(response) if when_not_found == :raise
|
|
177
|
-
|
|
178
|
-
JSON.parse(response.body)
|
|
167
|
+
nil
|
|
179
168
|
else
|
|
180
169
|
raise decode_error_from(response)
|
|
181
170
|
end
|
|
182
171
|
end
|
|
183
172
|
|
|
184
|
-
def decode_error_from
|
|
185
|
-
result =
|
|
186
|
-
JSON.parse(response.body)
|
|
187
|
-
rescue StandardError
|
|
188
|
-
{}
|
|
189
|
-
end
|
|
173
|
+
def decode_error_from response
|
|
174
|
+
result = JSON.parse(response.body) rescue {}
|
|
190
175
|
err = APIError.new(result['message'] || "invalid http status code #{response.status}")
|
|
191
176
|
err.decoded_body = result
|
|
192
177
|
err
|
|
193
178
|
end
|
|
194
179
|
end
|
|
195
|
-
# rubocop:enable Metrics/ClassLength
|
|
196
180
|
end
|
|
@@ -27,8 +27,12 @@ module Resizing
|
|
|
27
27
|
def initialize(*attrs)
|
|
28
28
|
case attr = attrs.first
|
|
29
29
|
when Hash
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
if attr[:project_id].nil? || attr[:secret_token].nil?
|
|
31
|
+
raise_configiration_error
|
|
32
|
+
end
|
|
33
|
+
if attr[:host].present?
|
|
34
|
+
raise_configiration_error
|
|
35
|
+
end
|
|
32
36
|
|
|
33
37
|
initialize_by_hash attr
|
|
34
38
|
return
|
|
@@ -37,6 +41,11 @@ module Resizing
|
|
|
37
41
|
raise_configiration_error
|
|
38
42
|
end
|
|
39
43
|
|
|
44
|
+
def host
|
|
45
|
+
Kernel.warn "[DEPRECATED] The Configuration#host is deprecated. Use Configuration#image_host."
|
|
46
|
+
self.image_host
|
|
47
|
+
end
|
|
48
|
+
|
|
40
49
|
def generate_auth_header
|
|
41
50
|
current_timestamp = Time.now.to_i
|
|
42
51
|
data = [current_timestamp, secret_token].join('|')
|
|
@@ -93,11 +102,13 @@ module Resizing
|
|
|
93
102
|
raise ConfigurationError, 'need hash and some keys like :image_host, video_host, :project_id, :secret_token'
|
|
94
103
|
end
|
|
95
104
|
|
|
96
|
-
# rubocop:disable Metrics/AbcSize
|
|
97
105
|
def initialize_by_hash(attr)
|
|
98
|
-
raise 'The host on configuration is deprecated. Use image_host, video_host' if attr[:host].present?
|
|
99
|
-
|
|
100
106
|
@image_host = attr[:image_host].dup.freeze || DEFAULT_IMAGE_HOST
|
|
107
|
+
if attr[:host].present?
|
|
108
|
+
Kernel.warn "[DEPRECATED] The host on configration is deprecated. Use image_host, video_host" if attr[:host].present?
|
|
109
|
+
@image_host ||= attr[:host].dup.freeze || DEFAULT_HOST # for backward compatible
|
|
110
|
+
end
|
|
111
|
+
|
|
101
112
|
@video_host = attr[:video_host].dup.freeze || DEFAULT_VIDEO_HOST
|
|
102
113
|
@project_id = attr[:project_id].dup.freeze
|
|
103
114
|
@secret_token = attr[:secret_token].dup.freeze
|
|
@@ -105,6 +116,5 @@ module Resizing
|
|
|
105
116
|
@response_timeout = attr[:response_timeout] || DEFAULT_RESPONSE_TIMEOUT
|
|
106
117
|
@enable_mock = attr[:enable_mock] || false
|
|
107
118
|
end
|
|
108
|
-
# rubocop:enable Metrics/AbcSize
|
|
109
119
|
end
|
|
110
120
|
end
|
|
@@ -12,14 +12,14 @@ module Resizing
|
|
|
12
12
|
end
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
def handle_faraday_error
|
|
15
|
+
def handle_faraday_error &block
|
|
16
16
|
yield
|
|
17
17
|
rescue Faraday::TimeoutError => e
|
|
18
18
|
handle_timeout_error e
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def handle_timeout_error
|
|
22
|
-
raise APIError
|
|
21
|
+
def handle_timeout_error error
|
|
22
|
+
raise APIError.new("TimeoutError: #{error.inspect}")
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
25
|
end
|
data/lib/resizing/mock_client.rb
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module Resizing
|
|
4
4
|
class MockClient
|
|
5
|
-
def post(
|
|
5
|
+
def post(file_or_binary, options = {})
|
|
6
6
|
r = load_yaml('test/vcr/client/post.yml')
|
|
7
7
|
JSON.parse(r['string'])
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
def put(name,
|
|
10
|
+
def put(name, file_or_binary, options)
|
|
11
11
|
r = load_yaml('test/vcr/client/put.yml')
|
|
12
12
|
result = JSON.parse(r['string'])
|
|
13
13
|
# replace name, public_id and version by name argument
|
|
@@ -26,7 +26,7 @@ module Resizing
|
|
|
26
26
|
result
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
def metadata(name
|
|
29
|
+
def metadata(name)
|
|
30
30
|
r = load_yaml('test/vcr/client/metadata.yml')
|
|
31
31
|
result = JSON.parse(r['string'])
|
|
32
32
|
# replace name and public_id by name argument
|
|
@@ -34,16 +34,15 @@ module Resizing
|
|
|
34
34
|
result['public_id'].gsub!(/bfdaf2b3-7ec5-41f4-9caa-d53247dd9666/, name)
|
|
35
35
|
result
|
|
36
36
|
end
|
|
37
|
-
|
|
38
37
|
private
|
|
39
38
|
|
|
40
|
-
def load_yaml
|
|
39
|
+
def load_yaml filename
|
|
41
40
|
path = "#{library_root}/#{filename}"
|
|
42
41
|
YAML.load_file(path)['http_interactions'].first['response']['body']
|
|
43
42
|
end
|
|
44
43
|
|
|
45
44
|
def library_root
|
|
46
|
-
@library_root ||= File.expand_path('
|
|
45
|
+
@library_root ||= File.expand_path('../../../', __FILE__)
|
|
47
46
|
end
|
|
48
47
|
end
|
|
49
48
|
end
|