resizing 1.3.1 → 1.4.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 +4 -4
- data/.devcontainer/compose.yaml +7 -4
- data/.github/dependabot.yml +23 -0
- data/.github/tools/actionlint/Dockerfile +3 -0
- data/.github/tools/gitleaks/Dockerfile +2 -0
- data/.github/workflows/actionlint.yml +27 -0
- data/.github/workflows/actions.lock +54 -0
- data/.github/workflows/labeler.yml +3 -1
- data/.github/workflows/lint.yml +2 -2
- data/.github/workflows/secret-scan.yml +29 -0
- data/.github/workflows/test.yml +37 -6
- data/.github/workflows/verify-lockfile.yml +28 -0
- data/CLAUDE.md +2 -2
- data/Gemfile +3 -0
- data/README.ja.md +241 -0
- data/README.md +83 -1
- data/docker-compose.yml +7 -0
- data/git-hooks/pre-push-ruby.sh +16 -11
- data/lib/resizing/carrier_wave/storage/file.rb +21 -2
- data/lib/resizing/carrier_wave/storage/remote.rb +18 -13
- data/lib/resizing/carrier_wave.rb +75 -7
- data/lib/resizing/client.rb +47 -3
- data/lib/resizing/mock_client.rb +25 -2
- data/lib/resizing/version.rb +1 -1
- data/lib/resizing.rb +4 -0
- data/test/rails_app/app/views/users/new.html.erb +6 -0
- data/test/rails_app/rails_app.rb +86 -0
- data/test/resizing/carrier_wave/storage/file_test.rb +40 -0
- data/test/resizing/carrier_wave/storage/remote_test.rb +66 -9
- data/test/resizing/carrier_wave_rails_integration_test.rb +94 -0
- data/test/resizing/carrier_wave_temporary_image_test.rb +280 -0
- data/test/resizing/carrier_wave_test.rb +15 -6
- data/test/resizing/carrier_wave_uploader_test.rb +14 -0
- data/test/resizing/client_test.rb +99 -9
- data/test/resizing/mock_client_test.rb +34 -0
- data/test/resizing_module_test.rb +16 -0
- data/test/test_helper.rb +23 -2
- data/test/vcr/carrier_wave_test/confirm.yml +23 -0
- data/test/vcr/carrier_wave_test/retrieve_from_cache.yml +23 -0
- data/test/vcr/carrier_wave_test/save.yml +1 -1
- data/test/vcr/carrier_wave_test/update_image.yml +42 -2
- metadata +16 -2
data/README.md
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
[](https://github.com/jksy/resizing-gem/actions/workflows/test.yml)
|
|
5
5
|
[](https://codecov.io/gh/jksy/resizing-gem)
|
|
6
6
|
|
|
7
|
+
English | [日本語](README.ja.md)
|
|
8
|
+
|
|
7
9
|
Client and utilities for [Resizing](https://www.resizing.net/) - an image hosting and transformation service.
|
|
8
10
|
|
|
9
11
|
## Requirements
|
|
@@ -42,7 +44,9 @@ Resizing.configure = {
|
|
|
42
44
|
}
|
|
43
45
|
```
|
|
44
46
|
|
|
45
|
-
`video_host`
|
|
47
|
+
`video_host` is deprecated because the video API has been discontinued. It is ignored
|
|
48
|
+
when given, and referring to `Configuration#video_host` or
|
|
49
|
+
`Configuration::DEFAULT_VIDEO_HOST` emits a warning. It will be removed in a future version.
|
|
46
50
|
|
|
47
51
|
## Usage
|
|
48
52
|
|
|
@@ -72,6 +76,25 @@ transformation_url = Resizing.url_from_image_id(image_id, nil, ['w_200', 'h_300'
|
|
|
72
76
|
# => "https://img.resizing.net/projects/.../upload/images/.../w_200,h_300"
|
|
73
77
|
```
|
|
74
78
|
|
|
79
|
+
#### Temporary images
|
|
80
|
+
|
|
81
|
+
An image uploaded with `temporary: true` is deleted by Resizing at its `expires_at`
|
|
82
|
+
unless it is confirmed before then. `expires_in` is the number of seconds until it
|
|
83
|
+
expires (Resizing's default when omitted).
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
response = client.post(file, content_type: 'image/jpeg', temporary: true, expires_in: 3600)
|
|
87
|
+
response['temporary'] # => true
|
|
88
|
+
response['expires_at'] # => "2020-05-15T16:33:10.711Z"
|
|
89
|
+
|
|
90
|
+
# Keep the image
|
|
91
|
+
client.confirm(response['id'])
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
A temporary image can be fetched and transformed like any other image until it expires.
|
|
95
|
+
Confirming an image that is already confirmed succeeds without changing it; confirming
|
|
96
|
+
an expired or deleted image raises `Resizing::APIError`.
|
|
97
|
+
|
|
75
98
|
### CarrierWave Integration
|
|
76
99
|
|
|
77
100
|
```ruby
|
|
@@ -126,6 +149,65 @@ extension of the file name is used as in plain CarrierWave.
|
|
|
126
149
|
Versions are not uploaded separately: a version only changes the transformation in the
|
|
127
150
|
generated URL, so a single upload backs every version.
|
|
128
151
|
|
|
152
|
+
#### Cache
|
|
153
|
+
|
|
154
|
+
Assigning a file to the mounted column uploads it to Resizing as a temporary image, and
|
|
155
|
+
saving the model confirms it (after commit with ActiveRecord). When the model is never
|
|
156
|
+
saved, e.g. because a validation fails or the transaction is rolled back, the image is
|
|
157
|
+
left unconfirmed and Resizing deletes it when it expires, so no orphan image is left
|
|
158
|
+
behind.
|
|
159
|
+
|
|
160
|
+
The temporary image expires after Resizing's default period. Override `cache_expires_in`
|
|
161
|
+
(in seconds) to change it, e.g. when a form may be kept open for a long time:
|
|
162
|
+
|
|
163
|
+
```ruby
|
|
164
|
+
class ImageUploader < CarrierWave::Uploader::Base
|
|
165
|
+
include Resizing::CarrierWave
|
|
166
|
+
|
|
167
|
+
def cache_expires_in
|
|
168
|
+
2.days.to_i
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
##### Making uploads work across form redisplays
|
|
174
|
+
|
|
175
|
+
As in plain CarrierWave, send `<column>_cache` back with the form, so that the image does
|
|
176
|
+
not need to be uploaded again when the form is shown again after a validation error.
|
|
177
|
+
Permit it along with the column in the strong parameters, or it is dropped:
|
|
178
|
+
|
|
179
|
+
```erb
|
|
180
|
+
<%= form_with model: @user do |form| %>
|
|
181
|
+
<%= form.file_field :image %>
|
|
182
|
+
<%= form.hidden_field :image_cache %>
|
|
183
|
+
<%= form.submit %>
|
|
184
|
+
<% end %>
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
```ruby
|
|
188
|
+
class UsersController < ApplicationController
|
|
189
|
+
def create
|
|
190
|
+
@user = User.new(user_params)
|
|
191
|
+
if @user.save
|
|
192
|
+
redirect_to @user
|
|
193
|
+
else
|
|
194
|
+
render :new, status: :unprocessable_entity
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
private
|
|
199
|
+
|
|
200
|
+
def user_params
|
|
201
|
+
params.require(:user).permit(:image, :image_cache)
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
`<column>_cache` returns the public id of the temporary image while it is not confirmed.
|
|
207
|
+
Only a temporary image of the configured project that has not expired is restored from
|
|
208
|
+
it; anything else (such as a confirmed image) is ignored, as CarrierWave ignores an
|
|
209
|
+
invalid cache name.
|
|
210
|
+
|
|
129
211
|
## Development
|
|
130
212
|
|
|
131
213
|
### Dev Container (recommended)
|
data/docker-compose.yml
CHANGED
|
@@ -4,6 +4,13 @@ services:
|
|
|
4
4
|
mysql:
|
|
5
5
|
image: mysql:5.7.29
|
|
6
6
|
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_bin
|
|
7
|
+
# MySQL 5.7 sizes its file descriptor tables from the nofile limit. On hosts
|
|
8
|
+
# where containerd runs with LimitNOFILE=infinity, containers inherit about
|
|
9
|
+
# 2^31, and mysqld then allocates tens of GB of memory and never gets ready.
|
|
10
|
+
ulimits:
|
|
11
|
+
nofile:
|
|
12
|
+
soft: 65536
|
|
13
|
+
hard: 65536
|
|
7
14
|
ports:
|
|
8
15
|
- 3306:3306
|
|
9
16
|
environment:
|
data/git-hooks/pre-push-ruby.sh
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
#
|
|
6
6
|
# The checks run wherever the gems are installed: the current shell when it
|
|
7
7
|
# already has them (inside the devcontainer, or a host with `bundle install`
|
|
8
|
-
# done), otherwise the devcontainer
|
|
8
|
+
# done), otherwise the devcontainer started for this checkout.
|
|
9
9
|
#
|
|
10
10
|
# PRE_PUSH_SKIP_TESTS=1 git push # lint only, skip the test suite
|
|
11
11
|
# git push --no-verify # skip every check (not recommended)
|
|
@@ -14,22 +14,27 @@ cd "$ROOT" || exit 1
|
|
|
14
14
|
|
|
15
15
|
if bundle check >/dev/null 2>&1; then
|
|
16
16
|
MODE=local
|
|
17
|
-
elif command -v devcontainer >/dev/null 2>&1; then
|
|
18
|
-
MODE=devcontainer
|
|
19
|
-
elif docker compose -f .devcontainer/compose.yaml ps --status running --quiet app 2>/dev/null | grep -q .; then
|
|
20
|
-
MODE=compose
|
|
21
17
|
else
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
# The devcontainer CLI and VS Code label the container with the host folder it
|
|
19
|
+
# was started for. Looking it up by that label never picks a container started
|
|
20
|
+
# for another worktree, which mounts other files: the checks would then pass
|
|
21
|
+
# or fail for code that is not the one being pushed.
|
|
22
|
+
CONTAINER="$(docker ps --quiet --filter "label=devcontainer.local_folder=$ROOT" 2>/dev/null | head -n 1)"
|
|
23
|
+
if [ -z "$CONTAINER" ]; then
|
|
24
|
+
echo "No environment with the gems installed was found."
|
|
25
|
+
echo "No devcontainer is running for $ROOT."
|
|
26
|
+
echo "Run 'devcontainer up --workspace-folder .' (or 'bundle install') first."
|
|
27
|
+
echo "To skip this check (not recommended): git push --no-verify"
|
|
28
|
+
exit 1
|
|
29
|
+
fi
|
|
30
|
+
MODE=devcontainer
|
|
26
31
|
fi
|
|
27
32
|
|
|
28
33
|
run() {
|
|
29
34
|
case "$MODE" in
|
|
30
35
|
local) bundle exec "$@" ;;
|
|
31
|
-
|
|
32
|
-
|
|
36
|
+
# User and folder as in .devcontainer/devcontainer.json (remoteUser, workspaceFolder)
|
|
37
|
+
devcontainer) docker exec -u vscode -w /workspaces/resizing-gem "$CONTAINER" bundle exec "$@" ;;
|
|
33
38
|
esac
|
|
34
39
|
}
|
|
35
40
|
|
|
@@ -114,9 +114,17 @@ module Resizing
|
|
|
114
114
|
end
|
|
115
115
|
alias path current_path
|
|
116
116
|
|
|
117
|
+
# Uploads new_file to Resizing and writes its public id to the model column.
|
|
118
|
+
#
|
|
119
|
+
# === Options
|
|
120
|
+
#
|
|
121
|
+
# [:temporary] uploads it as a temporary image, which Resizing deletes at its
|
|
122
|
+
# expires_at unless it is confirmed with #confirm
|
|
123
|
+
# [:expires_in] seconds until the temporary image expires (Resizing's default when nil)
|
|
124
|
+
#
|
|
117
125
|
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
118
126
|
# rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
119
|
-
def store(new_file)
|
|
127
|
+
def store(new_file, options = {})
|
|
120
128
|
if new_file.is_a?(self.class)
|
|
121
129
|
# new_file.copy_to(path)
|
|
122
130
|
raise NotImplementedError, 'new file is required duplicating'
|
|
@@ -144,7 +152,9 @@ module Resizing
|
|
|
144
152
|
new_file
|
|
145
153
|
end
|
|
146
154
|
|
|
147
|
-
|
|
155
|
+
post_options = { content_type: @content_type, filename: original_filename }
|
|
156
|
+
post_options.merge!(temporary: true, expires_in: options[:expires_in]) if options[:temporary]
|
|
157
|
+
@response = Resizing.post(content, post_options)
|
|
148
158
|
@public_id = Resizing::PublicId.new(@response['public_id'])
|
|
149
159
|
@content_type = @response['content_type']
|
|
150
160
|
|
|
@@ -164,6 +174,15 @@ module Resizing
|
|
|
164
174
|
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
165
175
|
# rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
166
176
|
|
|
177
|
+
# Confirms the temporary image uploaded by #store with temporary: true, so that
|
|
178
|
+
# Resizing keeps it after its expires_at. Raises APIError when it has already
|
|
179
|
+
# expired or been deleted.
|
|
180
|
+
def confirm
|
|
181
|
+
return if @public_id.empty?
|
|
182
|
+
|
|
183
|
+
client.confirm(@public_id.image_id)
|
|
184
|
+
end
|
|
185
|
+
|
|
167
186
|
def name(_options = {})
|
|
168
187
|
@public_id = PublicId.new(model.send(:read_attribute, serialization_column))
|
|
169
188
|
CGI.unescape(@public_id.filename)
|
|
@@ -6,7 +6,14 @@ module Resizing
|
|
|
6
6
|
# ref.
|
|
7
7
|
# https://github.com/carrierwaveuploader/carrierwave/blob/master/lib/carrierwave/storage/abstract.rb
|
|
8
8
|
class Remote < ::CarrierWave::Storage::Abstract
|
|
9
|
+
# A file cached by #cache! has already been uploaded as a temporary image, so
|
|
10
|
+
# storing it only confirms it instead of uploading it again.
|
|
9
11
|
def store!(file)
|
|
12
|
+
if file.is_a?(Resizing::CarrierWave::Storage::File)
|
|
13
|
+
file.confirm
|
|
14
|
+
return file
|
|
15
|
+
end
|
|
16
|
+
|
|
10
17
|
f = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
11
18
|
f.store(file)
|
|
12
19
|
f
|
|
@@ -30,33 +37,31 @@ module Resizing
|
|
|
30
37
|
f
|
|
31
38
|
end
|
|
32
39
|
|
|
40
|
+
# Uploads the file as a temporary image. It is confirmed by #store! when the
|
|
41
|
+
# model is saved; otherwise Resizing deletes it at its expires_at, so a file
|
|
42
|
+
# assigned to a model that is never saved (e.g. on a validation error) does not
|
|
43
|
+
# leave an orphan image behind.
|
|
33
44
|
def cache!(new_file)
|
|
34
45
|
f = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
35
|
-
f.store(new_file)
|
|
46
|
+
f.store(new_file, temporary: true, expires_in: uploader.cache_expires_in)
|
|
36
47
|
f
|
|
37
48
|
end
|
|
38
49
|
|
|
50
|
+
# The cache name of a Resizing uploader is the public id of the temporary image
|
|
51
|
+
# (see Resizing::CarrierWave#cache_name).
|
|
39
52
|
def retrieve_from_cache!(identifier)
|
|
40
|
-
|
|
41
|
-
|
|
53
|
+
return nil if identifier.blank?
|
|
54
|
+
|
|
55
|
+
Resizing::CarrierWave::Storage::File.new(uploader, identifier)
|
|
42
56
|
end
|
|
43
57
|
|
|
44
58
|
def delete_dir!(path)
|
|
45
59
|
# do nothing, because there's no such things as 'empty directory'
|
|
46
60
|
end
|
|
47
61
|
|
|
62
|
+
# Resizing deletes the expired temporary images by itself.
|
|
48
63
|
def clean_cache!(seconds)
|
|
49
64
|
# do nothing
|
|
50
|
-
#
|
|
51
|
-
# connection.directories.new(
|
|
52
|
-
# :key => uploader.fog_directory,
|
|
53
|
-
# :public => uploader.fog_public
|
|
54
|
-
# ).files.all(:prefix => uploader.cache_dir).each do |file|
|
|
55
|
-
# # generate_cache_id returns key formated TIMEINT-PID(-COUNTER)-RND
|
|
56
|
-
# time = file.key.scan(/(\d+)-\d+-\d+(?:-\d+)?/).first.map { |t| t.to_i }
|
|
57
|
-
# time = Time.at(*time)
|
|
58
|
-
# file.destroy if time < (Time.now.utc - seconds)
|
|
59
|
-
# end
|
|
60
65
|
end
|
|
61
66
|
end
|
|
62
67
|
end
|
|
@@ -105,8 +105,11 @@ module Resizing
|
|
|
105
105
|
@transform.push(:resize_to_fit, *args)
|
|
106
106
|
end
|
|
107
107
|
|
|
108
|
-
# Resizing has no cache storage, so the file is uploaded to Resizing
|
|
109
|
-
# instead of being copied to a local workfile
|
|
108
|
+
# Resizing has no cache storage, so the file is uploaded to Resizing here as a
|
|
109
|
+
# temporary image instead of being copied to a local workfile. The temporary image
|
|
110
|
+
# is confirmed by #store! when the model is saved (after commit with ActiveRecord);
|
|
111
|
+
# when the model is never saved, e.g. on a validation error, Resizing deletes it at
|
|
112
|
+
# its expires_at (see #cache_expires_in).
|
|
110
113
|
#
|
|
111
114
|
# The :cache callbacks are still run, so that the uploader settings hooked on them
|
|
112
115
|
# (extension_allowlist / extension_denylist / content_type_allowlist /
|
|
@@ -124,16 +127,53 @@ module Resizing
|
|
|
124
127
|
return if sanitized.empty?
|
|
125
128
|
|
|
126
129
|
with_callbacks(:cache, sanitized) do
|
|
127
|
-
file = storage.
|
|
128
|
-
#
|
|
129
|
-
#
|
|
130
|
-
|
|
131
|
-
|
|
130
|
+
file = storage.cache!(new_file)
|
|
131
|
+
# The public id of the temporary image serves as the cache id, so that
|
|
132
|
+
# store! confirms it and #cache_name can restore it.
|
|
133
|
+
@cache_id = file.public_id.to_s
|
|
134
|
+
@staged = true
|
|
132
135
|
@filename = file.public_id.to_s
|
|
133
136
|
@file = file
|
|
134
137
|
end
|
|
135
138
|
end
|
|
136
139
|
|
|
140
|
+
# The public id of the temporary image while it is cached, nil otherwise.
|
|
141
|
+
# It is what `<column>_cache` returns, to be sent back with a form that is shown
|
|
142
|
+
# again (e.g. on a validation error) so that the image need not be uploaded again.
|
|
143
|
+
def cache_name
|
|
144
|
+
@cache_id
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# Restores the temporary image from its cache name (see #cache_name).
|
|
148
|
+
#
|
|
149
|
+
# The cache name comes from the request, so only a temporary image of the
|
|
150
|
+
# configured project that has not expired is accepted; a confirmed image, which
|
|
151
|
+
# may belong to another record, must not be attached through it. Anything else
|
|
152
|
+
# raises CarrierWave::InvalidParameter, which CarrierWave ignores as it does an
|
|
153
|
+
# invalid cache name.
|
|
154
|
+
def retrieve_from_cache!(cache_name)
|
|
155
|
+
with_callbacks(:retrieve_from_cache, cache_name) do
|
|
156
|
+
public_id = temporary_public_id_from(cache_name)
|
|
157
|
+
@cache_id = public_id
|
|
158
|
+
@staged = true
|
|
159
|
+
@file = storage.retrieve_from_cache!(public_id)
|
|
160
|
+
model.send :write_attribute, serialization_column, public_id
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Seconds until a temporary image uploaded by #cache! expires. nil uses the default
|
|
165
|
+
# of Resizing. Override this to change it, e.g. when a form may be kept open for a
|
|
166
|
+
# long time before it is submitted.
|
|
167
|
+
def cache_expires_in
|
|
168
|
+
nil
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# The file cached by #cache! is the temporary image, which becomes the stored image
|
|
172
|
+
# when it is confirmed, so it must not be deleted after storing it.
|
|
173
|
+
def delete_tmp_file_after_storage
|
|
174
|
+
false
|
|
175
|
+
end
|
|
176
|
+
|
|
137
177
|
# process! is called before :cache
|
|
138
178
|
# Disable on Resizing, because `process` is repurposed as a definition of the
|
|
139
179
|
# transformation applied when the browser fetches the image URL (see #transform_string),
|
|
@@ -151,6 +191,14 @@ module Resizing
|
|
|
151
191
|
# NOP
|
|
152
192
|
end
|
|
153
193
|
|
|
194
|
+
# retrieve_versions_from_cache! is called after :retrieve_from_cache
|
|
195
|
+
# Disable on Resizing, because the versions have no image of their own; each version
|
|
196
|
+
# would only look up the same temporary image again.
|
|
197
|
+
# https://github.com/carrierwaveuploader/carrierwave/blob/28190e99299a6131c0424a5d10205f471e39f3cd/lib/carrierwave/uploader/versions.rb#L20
|
|
198
|
+
def retrieve_versions_from_cache!(*args)
|
|
199
|
+
# NOP
|
|
200
|
+
end
|
|
201
|
+
|
|
154
202
|
def filename
|
|
155
203
|
read_column
|
|
156
204
|
end
|
|
@@ -192,6 +240,26 @@ module Resizing
|
|
|
192
240
|
|
|
193
241
|
private
|
|
194
242
|
|
|
243
|
+
def temporary_public_id_from(cache_name)
|
|
244
|
+
public_id = begin
|
|
245
|
+
Resizing::PublicId.new(cache_name)
|
|
246
|
+
rescue StandardError
|
|
247
|
+
nil
|
|
248
|
+
end
|
|
249
|
+
raise_invalid_cache_name if public_id.nil? || public_id.empty? ||
|
|
250
|
+
public_id.project_id != Resizing.configure.project_id
|
|
251
|
+
|
|
252
|
+
# Resizing answers 404 for an expired temporary image even before deleting it
|
|
253
|
+
metadata = Resizing.metadata(public_id.image_id, {})
|
|
254
|
+
raise_invalid_cache_name unless metadata['temporary'] == true && metadata['public_id'].present?
|
|
255
|
+
|
|
256
|
+
metadata['public_id']
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def raise_invalid_cache_name
|
|
260
|
+
raise ::CarrierWave::InvalidParameter, 'invalid cache name'
|
|
261
|
+
end
|
|
262
|
+
|
|
195
263
|
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
196
264
|
def transform_string_from(processor)
|
|
197
265
|
action = processor.first
|
data/lib/resizing/client.rb
CHANGED
|
@@ -29,6 +29,10 @@ module Resizing
|
|
|
29
29
|
include Resizing::Configurable
|
|
30
30
|
include Resizing::HttpClientable
|
|
31
31
|
|
|
32
|
+
DEPRECATED_PUT_MESSAGE = '[resizing] Resizing::Client#put is deprecated: ' \
|
|
33
|
+
'the image update API is deprecated on Resizing. ' \
|
|
34
|
+
'Upload a new image with #post instead. It will be deleted in a future version.'
|
|
35
|
+
|
|
32
36
|
def initialize(*attrs)
|
|
33
37
|
initialize_config(*attrs)
|
|
34
38
|
end
|
|
@@ -37,15 +41,23 @@ module Resizing
|
|
|
37
41
|
raise NotImplementedError
|
|
38
42
|
end
|
|
39
43
|
|
|
44
|
+
# Uploads an image.
|
|
45
|
+
#
|
|
46
|
+
# === Options
|
|
47
|
+
#
|
|
48
|
+
# [:content_type] content type of the image (required)
|
|
49
|
+
# [:filename] file name sent to Resizing (defaults to the basename of the path)
|
|
50
|
+
# [:temporary] uploads the image as a temporary image when true. It is deleted by
|
|
51
|
+
# Resizing at its expires_at unless it is confirmed with #confirm.
|
|
52
|
+
# [:expires_in] seconds until a temporary image expires (Integer or
|
|
53
|
+
# ActiveSupport::Duration). Resizing applies its default when omitted.
|
|
40
54
|
def post(filename_or_io, options = {})
|
|
41
55
|
ensure_content_type(options)
|
|
42
56
|
ensure_filename_or_io(filename_or_io)
|
|
43
57
|
filename = gather_filename filename_or_io, options
|
|
44
58
|
|
|
45
59
|
url = build_post_url
|
|
46
|
-
params =
|
|
47
|
-
image: Faraday::Multipart::FilePart.new(filename_or_io, options[:content_type], filename)
|
|
48
|
-
}
|
|
60
|
+
params = build_post_params(filename_or_io, options, filename)
|
|
49
61
|
|
|
50
62
|
response = handle_faraday_error do
|
|
51
63
|
http_client.post(url, params) do |request|
|
|
@@ -56,7 +68,9 @@ module Resizing
|
|
|
56
68
|
handle_create_response(response)
|
|
57
69
|
end
|
|
58
70
|
|
|
71
|
+
# @deprecated Use {#post} to upload a new image instead.
|
|
59
72
|
def put(image_id, filename_or_io, options)
|
|
73
|
+
warn DEPRECATED_PUT_MESSAGE
|
|
60
74
|
ensure_content_type(options)
|
|
61
75
|
ensure_filename_or_io(filename_or_io)
|
|
62
76
|
filename = gather_filename filename_or_io, options
|
|
@@ -75,6 +89,21 @@ module Resizing
|
|
|
75
89
|
handle_create_response(response)
|
|
76
90
|
end
|
|
77
91
|
|
|
92
|
+
# Confirms a temporary image, so that Resizing no longer deletes it at its expires_at.
|
|
93
|
+
# Confirming an image that is already confirmed succeeds without changing it.
|
|
94
|
+
# Raises APIError when the image is not found, deleted or already expired.
|
|
95
|
+
def confirm(image_id)
|
|
96
|
+
url = build_confirm_url(image_id)
|
|
97
|
+
|
|
98
|
+
response = handle_faraday_error do
|
|
99
|
+
http_client.put(url) do |request|
|
|
100
|
+
request.headers['X-ResizingToken'] = config.generate_auth_header
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
handle_create_response(response)
|
|
105
|
+
end
|
|
106
|
+
|
|
78
107
|
def delete(image_id)
|
|
79
108
|
url = build_delete_url(image_id)
|
|
80
109
|
|
|
@@ -125,6 +154,21 @@ module Resizing
|
|
|
125
154
|
"#{config.image_host}/projects/#{config.project_id}/upload/images/#{image_id}/metadata"
|
|
126
155
|
end
|
|
127
156
|
|
|
157
|
+
def build_confirm_url(image_id)
|
|
158
|
+
"#{config.image_host}/projects/#{config.project_id}/upload/images/#{image_id}/confirm"
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def build_post_params(filename_or_io, options, filename)
|
|
162
|
+
params = {
|
|
163
|
+
image: Faraday::Multipart::FilePart.new(filename_or_io, options[:content_type], filename)
|
|
164
|
+
}
|
|
165
|
+
return params unless options[:temporary]
|
|
166
|
+
|
|
167
|
+
params[:temporary] = 'true'
|
|
168
|
+
params[:expires_in] = options[:expires_in].to_i.to_s unless options[:expires_in].nil?
|
|
169
|
+
params
|
|
170
|
+
end
|
|
171
|
+
|
|
128
172
|
def ensure_content_type(options)
|
|
129
173
|
raise ArgumentError, "need options[:content_type] for #{options.inspect}" unless options[:content_type]
|
|
130
174
|
end
|
data/lib/resizing/mock_client.rb
CHANGED
|
@@ -4,9 +4,27 @@ require 'securerandom'
|
|
|
4
4
|
|
|
5
5
|
module Resizing
|
|
6
6
|
class MockClient
|
|
7
|
-
|
|
7
|
+
# Resizing の既定の一時画像の有効期限 (秒)
|
|
8
|
+
DEFAULT_TEMPORARY_EXPIRES_IN = 24 * 60 * 60
|
|
9
|
+
|
|
10
|
+
def post(_file_or_binary, options = {})
|
|
8
11
|
r = load_yaml('test/vcr/client/post.yml')
|
|
9
|
-
JSON.parse(r['string'])
|
|
12
|
+
result = JSON.parse(r['string'])
|
|
13
|
+
# temporary を指定したときは一時画像として返す
|
|
14
|
+
result['temporary'] = options[:temporary] ? true : false
|
|
15
|
+
result['expires_at'] = (temporary_expires_at(options[:expires_in]) if options[:temporary])
|
|
16
|
+
result
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def confirm(name)
|
|
20
|
+
r = load_yaml('test/vcr/client/metadata.yml')
|
|
21
|
+
result = JSON.parse(r['string'])
|
|
22
|
+
# カセット内の値に依存せず、name で id / public_id を組み立て直す
|
|
23
|
+
result['id'] = name
|
|
24
|
+
result['public_id'] = build_public_id(result['public_id'], image_id: name)
|
|
25
|
+
result['temporary'] = false
|
|
26
|
+
result['expires_at'] = nil
|
|
27
|
+
result
|
|
10
28
|
end
|
|
11
29
|
|
|
12
30
|
def put(name, _file_or_binary, _options)
|
|
@@ -49,6 +67,11 @@ module Resizing
|
|
|
49
67
|
version.nil? ? identifier : "#{identifier}/v#{version}"
|
|
50
68
|
end
|
|
51
69
|
|
|
70
|
+
def temporary_expires_at(expires_in)
|
|
71
|
+
seconds = expires_in.nil? ? DEFAULT_TEMPORARY_EXPIRES_IN : expires_in.to_i
|
|
72
|
+
(Time.now + seconds).utc.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
|
|
73
|
+
end
|
|
74
|
+
|
|
52
75
|
def generate_version
|
|
53
76
|
SecureRandom.alphanumeric(32)
|
|
54
77
|
end
|
data/lib/resizing/version.rb
CHANGED
data/lib/resizing.rb
CHANGED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# A minimal Rails application that mounts a Resizing uploader the way README.md
|
|
4
|
+
# shows ("Making uploads work across form redisplays"), so that the integration
|
|
5
|
+
# with the view helpers, strong parameters and controllers is tested on every
|
|
6
|
+
# Rails version in CI.
|
|
7
|
+
#
|
|
8
|
+
# ActiveRecord is not loaded as a railtie: test_helper.rb has already connected
|
|
9
|
+
# to the test database, and the railtie would connect again from its own config.
|
|
10
|
+
require 'action_controller/railtie'
|
|
11
|
+
require 'action_view/railtie'
|
|
12
|
+
|
|
13
|
+
class ResizingRailsApp < Rails::Application
|
|
14
|
+
config.root = __dir__
|
|
15
|
+
config.eager_load = false
|
|
16
|
+
config.logger = Logger.new(nil)
|
|
17
|
+
config.secret_key_base = 'resizing-rails-app-secret-key-base'
|
|
18
|
+
# Integration tests request www.example.com, which the development defaults block
|
|
19
|
+
config.hosts.clear
|
|
20
|
+
config.action_controller.allow_forgery_protection = false
|
|
21
|
+
# Raise errors in the tests instead of rendering an error page
|
|
22
|
+
config.action_dispatch.show_exceptions = Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR.zero? ? false : :none
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
ResizingRailsApp.initialize!
|
|
26
|
+
|
|
27
|
+
ResizingRailsApp.routes.draw do
|
|
28
|
+
resources :users, only: %i[new create show]
|
|
29
|
+
post '/users_without_cache' => 'users_without_cache#create'
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
ActiveRecord::Schema.define do
|
|
33
|
+
self.verbose = false
|
|
34
|
+
|
|
35
|
+
connection.execute 'drop table if exists users'
|
|
36
|
+
create_table :users do |t|
|
|
37
|
+
t.string :name
|
|
38
|
+
t.string :image
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
class ImageUploader < CarrierWave::Uploader::Base
|
|
43
|
+
include Resizing::CarrierWave
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
class User < ActiveRecord::Base
|
|
47
|
+
mount_uploader :image, ImageUploader
|
|
48
|
+
|
|
49
|
+
validates :name, presence: true
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
class ApplicationController < ActionController::Base; end
|
|
53
|
+
|
|
54
|
+
class UsersController < ApplicationController
|
|
55
|
+
def new
|
|
56
|
+
@user = User.new
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def create
|
|
60
|
+
@user = User.new(user_params)
|
|
61
|
+
if @user.save
|
|
62
|
+
redirect_to @user
|
|
63
|
+
else
|
|
64
|
+
render :new, status: :unprocessable_entity
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def show
|
|
69
|
+
head :ok
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
def user_params
|
|
75
|
+
params.require(:user).permit(:name, :image, :image_cache)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# The same controller with <column>_cache left out of the strong parameters
|
|
80
|
+
class UsersWithoutCacheController < UsersController
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
def user_params
|
|
84
|
+
params.require(:user).permit(:name, :image)
|
|
85
|
+
end
|
|
86
|
+
end
|