resizing 1.2.2 → 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/Dockerfile +73 -0
- data/.devcontainer/compose.yaml +61 -0
- data/.devcontainer/devcontainer-lock.json +9 -0
- data/.devcontainer/devcontainer.json +32 -0
- data/.devcontainer/post-create.sh +41 -0
- data/.github/copilot-instructions.md +1 -0
- data/.github/dependabot.yml +23 -0
- data/.github/labeler.yml +28 -0
- data/.github/release.yml +35 -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 +22 -0
- data/.github/workflows/lint.yml +37 -0
- data/.github/workflows/secret-scan.yml +29 -0
- data/.github/workflows/test.yml +51 -13
- data/.github/workflows/verify-lockfile.yml +28 -0
- data/AGENTS.md +1 -0
- data/CHANGELOG.md +3 -0
- data/CLAUDE.md +68 -0
- data/Gemfile +12 -3
- data/README.ja.md +241 -0
- data/README.md +196 -2
- data/Rakefile +5 -7
- data/bin/setup +3 -0
- data/docker-compose.yml +7 -0
- data/git-hooks/pre-push +6 -0
- data/git-hooks/pre-push-ruby.sh +53 -0
- data/lib/resizing/carrier_wave/callback_file.rb +88 -0
- data/lib/resizing/carrier_wave/storage/file.rb +105 -38
- data/lib/resizing/carrier_wave/storage/remote.rb +25 -16
- data/lib/resizing/carrier_wave.rb +109 -6
- data/lib/resizing/client.rb +47 -4
- data/lib/resizing/configurable.rb +1 -1
- data/lib/resizing/configuration.rb +40 -10
- data/lib/resizing/mock_client.rb +48 -9
- data/lib/resizing/public_id.rb +3 -2
- data/lib/resizing/version.rb +1 -1
- data/lib/resizing.rb +4 -1
- data/resizing.gemspec +3 -3
- 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 +495 -9
- data/test/resizing/carrier_wave/storage/remote_test.rb +151 -12
- data/test/resizing/carrier_wave_cache_callbacks_test.rb +224 -0
- data/test/resizing/carrier_wave_callback_file_test.rb +77 -0
- 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 +214 -30
- data/test/resizing/carrier_wave_uploader_test.rb +225 -0
- data/test/resizing/client_test.rb +419 -30
- data/test/resizing/configuration_test.rb +235 -10
- data/test/resizing/http_clientable_test.rb +27 -0
- data/test/resizing/mock_client_test.rb +145 -1
- data/test/resizing/public_id_test.rb +96 -0
- data/test/resizing_module_test.rb +126 -0
- data/test/resizing_test.rb +8 -0
- data/test/test_helper.rb +318 -9
- 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 +47 -9
data/CLAUDE.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# CLAUDE.md — resizing-gem
|
|
2
|
+
|
|
3
|
+
[Resizing](https://www.resizing.net/) の Ruby クライアント gem(rubygems.org に `resizing` として公開)。
|
|
4
|
+
|
|
5
|
+
## 開発環境
|
|
6
|
+
|
|
7
|
+
devcontainer(`.devcontainer/`)を用意してある。VS Code の "Reopen in Container"、または `devcontainer up --workspace-folder .` で、Ruby・MySQL・`bundle install` まで揃った状態になる。compose のプロジェクト名はチェックアウトのフォルダ名から決まる(`<フォルダ名>_devcontainer`)ので、git worktree ごとに別のコンテナと MySQL が立つ。gem や MySQL のデータの volume も worktree ごとに分かれる(使い終わった worktree の分は `docker compose -p <フォルダ名>_devcontainer -f .devcontainer/compose.yaml down -v` で消す)。
|
|
8
|
+
|
|
9
|
+
Ruby は rbenv 管理で `.ruby-version` に従う。別バージョンを試す場合はコンテナ内で `rbenv install <version>` → `rbenv local <version>` → `bundle install`(sudo 不要)。イメージに焼くバージョンを変える場合は `.devcontainer/compose.yaml` の `RUBY_VERSION` を変更してリビルドする。
|
|
10
|
+
|
|
11
|
+
devcontainer を使わない場合はテスト用に MySQL を自前で起動する(`docker compose up -d mysql`)。接続先は `MYSQL_HOST` / `MYSQL_PORT` / `MYSQL_USER` / `MYSQL_PASSWORD` で上書きでき、既定は `root:secret@127.0.0.1:3306`。
|
|
12
|
+
|
|
13
|
+
データベースはテストプロセスごとに `resizing_gem_test_<ホスト識別子>_<pid>` を作成し、終了時に破棄する(並行実行しても壊れないようにするため。強制終了で残ったものは次回実行時に掃除する)。`MYSQL_DATABASE` を指定した場合はそのデータベースをそのまま使い破棄もしないので、並行実行するときは名前を分ける。
|
|
14
|
+
|
|
15
|
+
## テスト・Lint
|
|
16
|
+
|
|
17
|
+
外部 API 呼び出しは `test/vcr/` のカセットを使い、実 API を叩くテストは追加しない。
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
bundle exec rake ci # RuboCop + テスト(push 前チェックと同じ)
|
|
21
|
+
bundle exec rake test # 全件
|
|
22
|
+
bundle exec ruby -Itest -Ilib test/resizing/client_test.rb # 単一ファイル
|
|
23
|
+
RAILS_VERSION=7.1 bundle exec rake test # Rails バージョンを変えて実行(既定は Ruby 3.2 以上で 8.1、3.1 で 7.2)
|
|
24
|
+
bundle exec rubocop
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
ローカルで実行できない場合、確認は CI に任せてよい。テストは `.github/workflows/test.yml`(Ruby 3.1〜4.0 × Rails 7.0〜8.1)、RuboCop は `.github/workflows/lint.yml`(Ruby 3.1)で走る。
|
|
28
|
+
|
|
29
|
+
## push 前チェック
|
|
30
|
+
|
|
31
|
+
`git push` すると `git-hooks/pre-push` が `bundle exec rake ci`(RuboCop + テスト)を実行し、失敗した時点で push を中断する。**CI が落ちる前に気づくための仕組みなので、`--no-verify` で飛ばさないこと**(飛ばした場合はその旨を報告する)。
|
|
32
|
+
|
|
33
|
+
有効化はクローンごとに一度だけ必要で、`bin/setup` が `git config core.hooksPath git-hooks` を設定する。設定済みかは `git config core.hooksPath` で確認できる(worktree でも設定は共有される)。
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
bin/setup # bundle install + git hooks の有効化
|
|
37
|
+
PRE_PUSH_SKIP_TESTS=1 git push # RuboCop だけにしてテストを省く
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
チェックは gem が入っている環境で走る。`bundle check` が通る環境(devcontainer の中、または `bundle install` 済みのホスト)ならそのまま、通らなければそのチェックアウト用に起動した devcontainer の中で実行する(コンテナの `devcontainer.local_folder` ラベルで探すので、別の worktree のコンテナは使わない)。見つからなければ `devcontainer up --workspace-folder .` を促して中断する。
|
|
41
|
+
|
|
42
|
+
## PR 運用
|
|
43
|
+
|
|
44
|
+
- `master` に直接 push しない。feature ブランチ(`claude/` プレフィックス可)から PR を出す
|
|
45
|
+
- **PR には種別ラベルを必ず 1 つ付ける**(`enhancement` / `improvement` / `bug` / `security` / `documentation` / `housekeeping` / `breaking change` のいずれか)。リリースノートのカテゴリ分け(`.github/release.yml`)に使われる
|
|
46
|
+
- `improvement` は gem の機能改善。CI・開発環境の整備など利用者に見えない変更は `housekeeping`
|
|
47
|
+
- バージョンバンプだけの PR には `release` を付ける(リリースノートから除外される)
|
|
48
|
+
- `ruby` / `tests` / `github_actions` / `documentation` / `dependencies` は `.github/labeler.yml` で自動付与される。種別ラベルは自動では付かない
|
|
49
|
+
- Issue 番号は PR タイトルに含めず、PR 説明に `Closes #N` と書く
|
|
50
|
+
- 公開 API を変える場合は `breaking change` ラベルを付け、README も更新する
|
|
51
|
+
|
|
52
|
+
## リリース手順
|
|
53
|
+
|
|
54
|
+
1. `lib/resizing/version.rb` を更新する PR に `release` ラベルを付けてマージする
|
|
55
|
+
2. `master` で `bundle exec rake release`(rubygems.org への公開と `v<version>` タグの push)
|
|
56
|
+
3. GitHub の Releases で該当タグを選び、"Generate release notes" で本文を生成して公開する
|
|
57
|
+
|
|
58
|
+
`CHANGELOG.md` は v0.8.2 で更新を止めており、リリースノートは GitHub Releases に一本化している。
|
|
59
|
+
|
|
60
|
+
## 言語ルール
|
|
61
|
+
|
|
62
|
+
| 場所 | 言語 |
|
|
63
|
+
|------|------|
|
|
64
|
+
| コミットメッセージ | 英語(Issue 番号は含めない) |
|
|
65
|
+
| ソースコード内コメント | 英語 |
|
|
66
|
+
| PR タイトル・説明 | 日本語 |
|
|
67
|
+
| コードレビューコメント | 日本語 |
|
|
68
|
+
| 人間とのやり取り全般 | 日本語 |
|
data/Gemfile
CHANGED
|
@@ -5,12 +5,21 @@ source 'https://rubygems.org'
|
|
|
5
5
|
# Specify your gem's dependencies in resizing.gemspec
|
|
6
6
|
gemspec
|
|
7
7
|
|
|
8
|
-
# Allow testing against different Rails versions
|
|
9
|
-
|
|
8
|
+
# Allow testing against different Rails versions (e.g. RAILS_VERSION=7.1).
|
|
9
|
+
# Unset, the newest supported Rails that runs on the current Ruby is used:
|
|
10
|
+
# Rails 8.x needs Ruby 3.2+, so Ruby 3.1 falls back to 7.2.
|
|
11
|
+
rails_version = ENV['RAILS_VERSION'] || (RUBY_VERSION >= '3.2' ? '8.1' : '7.2')
|
|
12
|
+
# "~> 7.0" would allow any 7.x, so a major.minor version is pinned to that
|
|
13
|
+
# minor series ("~> 7.0.0").
|
|
14
|
+
rails_version = "#{rails_version}.0" if rails_version.count('.') == 1
|
|
10
15
|
gem 'rails', "~> #{rails_version}"
|
|
11
16
|
|
|
17
|
+
# Allow testing against different Faraday versions (e.g. FARADAY_VERSION=1.10).
|
|
18
|
+
# Unset, the gemspec constraint picks the latest release.
|
|
19
|
+
faraday_version = ENV['FARADAY_VERSION']
|
|
20
|
+
gem 'faraday', "~> #{faraday_version}" if faraday_version
|
|
21
|
+
|
|
12
22
|
gem 'byebug'
|
|
13
|
-
gem 'github_changelog_generator'
|
|
14
23
|
gem 'mysql2'
|
|
15
24
|
gem 'pry-byebug'
|
|
16
25
|
gem 'rake', '~> 13.0'
|
data/README.ja.md
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
# Resizing
|
|
2
|
+
|
|
3
|
+
[](https://rubygems.org/gems/resizing)
|
|
4
|
+
[](https://github.com/jksy/resizing-gem/actions/workflows/test.yml)
|
|
5
|
+
[](https://codecov.io/gh/jksy/resizing-gem)
|
|
6
|
+
|
|
7
|
+
[English](README.md) | 日本語
|
|
8
|
+
|
|
9
|
+
画像のホスティング・変換サービス [Resizing](https://www.resizing.net/) のクライアントとユーティリティです。
|
|
10
|
+
|
|
11
|
+
## 動作環境
|
|
12
|
+
|
|
13
|
+
- Ruby 3.1.0 以上(3.1、3.2、3.3、3.4、4.0 でテスト)
|
|
14
|
+
- CarrierWave 連携を使う場合は Rails 7.0 以上(7.0、7.1、7.2、8.0、8.1 でテスト)
|
|
15
|
+
- Faraday 1.x または 2.x(1.10 と最新の 2.x でテスト)
|
|
16
|
+
|
|
17
|
+
CI でテストしている組み合わせは `.github/workflows/test.yml` を参照してください。
|
|
18
|
+
上流のサポートが終了した Ruby・Rails のシリーズは、マイナーリリースでサポートを終了することがあります。その場合はリリースノートでお知らせします。
|
|
19
|
+
|
|
20
|
+
## インストール
|
|
21
|
+
|
|
22
|
+
アプリケーションの Gemfile に次の行を追加します。
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
gem 'resizing'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
続けて次を実行します。
|
|
29
|
+
|
|
30
|
+
$ bundle install
|
|
31
|
+
|
|
32
|
+
または、直接インストールします。
|
|
33
|
+
|
|
34
|
+
$ gem install resizing
|
|
35
|
+
|
|
36
|
+
## 設定
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
Resizing.configure = {
|
|
40
|
+
image_host: 'https://img.resizing.net',
|
|
41
|
+
project_id: 'your-project-id',
|
|
42
|
+
secret_token: 'your-secret-token'
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`video_host` は動画 API の廃止にともない非推奨です。指定しても利用されず、`Configuration#video_host` や `Configuration::DEFAULT_VIDEO_HOST` を参照すると警告が出ます。将来のバージョンで削除します。
|
|
47
|
+
|
|
48
|
+
## 使い方
|
|
49
|
+
|
|
50
|
+
### クライアントの基本的な使い方
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
# クライアントを初期化する
|
|
54
|
+
client = Resizing::Client.new
|
|
55
|
+
|
|
56
|
+
# 画像を Resizing にアップロードする
|
|
57
|
+
file = File.open('sample.jpg', 'r')
|
|
58
|
+
response = client.post(file)
|
|
59
|
+
# => {
|
|
60
|
+
# "id"=>"a4ed2bf0-a4cf-44fa-9c82-b53e581cb469",
|
|
61
|
+
# "project_id"=>"098a2a0d-0000-0000-0000-000000000000",
|
|
62
|
+
# "content_type"=>"image/jpeg",
|
|
63
|
+
# "latest_version_id"=>"LJY5bxBF7Ryxfr5kC1F.63W8bzp3pcUm",
|
|
64
|
+
# "latest_etag"=>"\"190143614e6c342637584f46f18f8c58\"",
|
|
65
|
+
# "created_at"=>"2020-05-15T15:33:10.711Z",
|
|
66
|
+
# "updated_at"=>"2020-05-15T15:33:10.711Z",
|
|
67
|
+
# "url"=>"/projects/098a2a0d-0000-0000-0000-000000000000/upload/images/a4ed2bf0-a4cf-44fa-9c82-b53e581cb469"
|
|
68
|
+
# }
|
|
69
|
+
|
|
70
|
+
# 変換用の URL を生成する
|
|
71
|
+
image_id = response['id']
|
|
72
|
+
transformation_url = Resizing.url_from_image_id(image_id, nil, ['w_200', 'h_300'])
|
|
73
|
+
# => "https://img.resizing.net/projects/.../upload/images/.../w_200,h_300"
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### 一時画像
|
|
77
|
+
|
|
78
|
+
`temporary: true` を指定してアップロードした画像は一時画像になり、期限(`expires_at`)までに確定しなければ Resizing が削除します。`expires_in` には期限までの秒数を指定します(省略すると Resizing の既定値)。
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
response = client.post(file, content_type: 'image/jpeg', temporary: true, expires_in: 3600)
|
|
82
|
+
response['temporary'] # => true
|
|
83
|
+
response['expires_at'] # => "2020-05-15T16:33:10.711Z"
|
|
84
|
+
|
|
85
|
+
# 画像を残す
|
|
86
|
+
client.confirm(response['id'])
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
一時画像も、期限までは通常の画像と同じように取得・変換できます。確定済みの画像を確定しても何も変わらずに成功します。期限切れや削除済みの画像を確定しようとすると `Resizing::APIError` が発生します。
|
|
90
|
+
|
|
91
|
+
### CarrierWave 連携
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
class ImageUploader < CarrierWave::Uploader::Base
|
|
95
|
+
include Resizing::CarrierWave
|
|
96
|
+
|
|
97
|
+
version :list_smallest do
|
|
98
|
+
process resize_to_fill: [200, 200]
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
class User
|
|
103
|
+
mount_uploader :image, ImageUploader
|
|
104
|
+
end
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
`process` は、ブラウザが画像の URL を取得するときに適用する変換を宣言するものです。アップロード時にローカルで画像処理が実行されることはありません。
|
|
108
|
+
|
|
109
|
+
CarrierWave の `:cache` コールバックに登録される uploader のバリデーションは、画像を Resizing にアップロードする前にチェックされます。
|
|
110
|
+
|
|
111
|
+
```ruby
|
|
112
|
+
class ImageUploader < CarrierWave::Uploader::Base
|
|
113
|
+
include Resizing::CarrierWave
|
|
114
|
+
|
|
115
|
+
def extension_allowlist
|
|
116
|
+
%w[jpg jpeg gif png]
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def content_type_allowlist
|
|
120
|
+
[%r{\Aimage/}]
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def size_range
|
|
124
|
+
0..(5 * 1024 * 1024)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`extension_denylist` と `content_type_denylist` も同じように働きます。拒否されたファイルはアップロードされず、通常の CarrierWave と同じく、マウントしたカラムにエラーが付いてモデルが無効になります。
|
|
130
|
+
|
|
131
|
+
`extension_allowlist` / `extension_denylist` でチェックする拡張子は、ファイルの Content-Type がわかる場合はファイル名ではなく Content-Type から決めます。Resizing の URL からダウンロードしたファイル(`remote_<column>_url=` で代入した場合など)はファイル名の末尾がバージョン文字列なので、ファイル名からは意味のある拡張子が取れません。そのため、レスポンスの `Content-Type` を使います。Content-Type が不明な場合(`application/octet-stream`)は、通常の CarrierWave と同じくファイル名の拡張子を使います。
|
|
132
|
+
|
|
133
|
+
バージョンごとに別々にアップロードすることはありません。バージョンは生成する URL の変換を変えるだけなので、1 回のアップロードですべてのバージョンをまかないます。
|
|
134
|
+
|
|
135
|
+
#### キャッシュ
|
|
136
|
+
|
|
137
|
+
マウントしたカラムにファイルを代入すると、Resizing に一時画像としてアップロードされ、モデルを保存すると確定します(ActiveRecord ではコミット後)。バリデーションの失敗やトランザクションのロールバックなどでモデルが保存されなかった場合、画像は確定されず、期限が来ると Resizing が削除します。そのため、どこからも参照されない画像が残りません。
|
|
138
|
+
|
|
139
|
+
一時画像は Resizing の既定の期間で期限切れになります。フォームを長時間開いたままにすることがある場合などは、`cache_expires_in`(秒)を上書きして変更できます。
|
|
140
|
+
|
|
141
|
+
```ruby
|
|
142
|
+
class ImageUploader < CarrierWave::Uploader::Base
|
|
143
|
+
include Resizing::CarrierWave
|
|
144
|
+
|
|
145
|
+
def cache_expires_in
|
|
146
|
+
2.days.to_i
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
##### フォームの再表示でアップロードを引き継ぐ
|
|
152
|
+
|
|
153
|
+
通常の CarrierWave と同じく、フォームで `<column>_cache` を送り返すと、バリデーションエラーでフォームを再表示したときに画像をアップロードし直す必要がありません。strong parameters ではカラムと一緒に `<column>_cache` も permit してください。permit しないと値が捨てられます。
|
|
154
|
+
|
|
155
|
+
```erb
|
|
156
|
+
<%= form_with model: @user do |form| %>
|
|
157
|
+
<%= form.file_field :image %>
|
|
158
|
+
<%= form.hidden_field :image_cache %>
|
|
159
|
+
<%= form.submit %>
|
|
160
|
+
<% end %>
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
```ruby
|
|
164
|
+
class UsersController < ApplicationController
|
|
165
|
+
def create
|
|
166
|
+
@user = User.new(user_params)
|
|
167
|
+
if @user.save
|
|
168
|
+
redirect_to @user
|
|
169
|
+
else
|
|
170
|
+
render :new, status: :unprocessable_entity
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
private
|
|
175
|
+
|
|
176
|
+
def user_params
|
|
177
|
+
params.require(:user).permit(:image, :image_cache)
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
`<column>_cache` は、確定前の一時画像の public id を返します。復元されるのは、設定したプロジェクトの、期限切れでない一時画像だけです。それ以外(確定済みの画像など)は、CarrierWave が不正なキャッシュ名を扱うのと同じく無視されます。
|
|
183
|
+
|
|
184
|
+
## 開発
|
|
185
|
+
|
|
186
|
+
### Dev Container(推奨)
|
|
187
|
+
|
|
188
|
+
全員が同じ Ruby / MySQL 環境で開発できるよう、このリポジトリには [Dev Container](https://containers.dev/) を用意しています。
|
|
189
|
+
|
|
190
|
+
必要なもの:Docker と、VS Code + *Dev Containers* 拡張機能、または [`devcontainer` CLI](https://github.com/devcontainers/cli)。
|
|
191
|
+
|
|
192
|
+
1. VS Code でリポジトリを開き、**Dev Containers: Reopen in Container** を実行します
|
|
193
|
+
(または `devcontainer up --workspace-folder .` を実行します)。
|
|
194
|
+
2. `bundle install` と MySQL の起動待ちは `.devcontainer/post-create.sh` が自動で行います。
|
|
195
|
+
3. コンテナ内で次を実行します。
|
|
196
|
+
|
|
197
|
+
```console
|
|
198
|
+
$ bundle exec rake test # テストを実行する
|
|
199
|
+
$ bundle exec rubocop # Lint を実行する
|
|
200
|
+
$ bin/console # 対話的なプロンプト
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
コンテナには次のものが用意されています。
|
|
204
|
+
|
|
205
|
+
- [rbenv](https://github.com/rbenv/rbenv) で管理する Ruby。コンテナ内でバージョンを切り替えられます(後述)
|
|
206
|
+
- CI と同じ、`resizing_gem_test` データベースを持つ MySQL 5.7
|
|
207
|
+
- 別の Rails バージョンでテストするための `RAILS_VERSION`。`Gemfile` と CI の `RAILS_VERSION` の切り替えと同じものです。指定しない場合、`Gemfile` は現在の Ruby で動く最新のサポート対象 Rails(8.1、Ruby 3.1 では 7.2)を選びます
|
|
208
|
+
|
|
209
|
+
gem は名前付きボリューム(`/usr/local/bundle`)にインストールされます。`vendor/`、`.bundle/`、`.ruby-lsp/` はそれぞれ専用のボリュームに置き、ホストで生成された Ruby の成果物がバインドマウント経由でコンテナに入り込まないようにしています。`vendor/` にはホストのライブラリに対してネイティブ拡張をビルドした gem が入り、`.bundle/config` は `BUNDLE_PATH` をそこに向け、`.ruby-lsp/` には Ruby LSP 拡張機能がインストールした gem が記録されるためです。ホスト側のファイルには手を加えません。
|
|
210
|
+
|
|
211
|
+
#### Ruby のバージョンを変える
|
|
212
|
+
|
|
213
|
+
イメージのビルド時に Ruby 3.1.7 がインストールされ、コンテナ作成時には `.ruby-version` に従います。コンテナ内で別のバージョンに切り替えるには次を実行します。
|
|
214
|
+
|
|
215
|
+
```console
|
|
216
|
+
$ rbenv install 3.3.12
|
|
217
|
+
$ rbenv local 3.3.12 # .ruby-version を書き込む(gitignore 済み)
|
|
218
|
+
$ bundle install
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
`rbenv install` に `sudo` は不要で、ビルドに必要な依存関係はイメージに入っています。イメージに入れる Ruby のバージョンを変える場合は、`.devcontainer/compose.yaml` のビルド引数 `RUBY_VERSION` を変えてリビルドします。
|
|
222
|
+
|
|
223
|
+
### Dev Container を使わない場合
|
|
224
|
+
|
|
225
|
+
リポジトリをチェックアウトしたら、`bin/setup` を実行して依存関係をインストールします。その後 `rake test` でテストを実行します。`bin/console` で、試しながら使える対話的なプロンプトも起動できます。
|
|
226
|
+
|
|
227
|
+
テストには MySQL サーバーが必要です。リポジトリ直下の `docker-compose.yml` で適切なものを起動できます(`docker compose up -d mysql`)。
|
|
228
|
+
|
|
229
|
+
テストプロセスはそれぞれ専用のデータベース(`resizing_gem_test_<host>_<pid>`)を作成し、実行が終わると削除します。そのため、複数のテストプロセスが 1 つの MySQL サーバーを共有しても、互いのテーブルを壊しません。強制終了したプロセスが残したデータベースは、次回の実行時に掃除されます。
|
|
230
|
+
|
|
231
|
+
接続先の既定値は `root:secret@127.0.0.1:3306` で、環境変数 `MYSQL_HOST`、`MYSQL_PORT`、`MYSQL_USER`、`MYSQL_PASSWORD` で上書きできます。`MYSQL_DATABASE` を指定すると、そのデータベースをそのまま使い、削除もしません。その場合、並行して実行するテストプロセスにはそれぞれ別の名前を付けてください。同じ名前だと互いのテーブルを上書きしてしまいます。
|
|
232
|
+
|
|
233
|
+
この gem をローカルマシンにインストールするには `bundle exec rake install` を実行します。新しいバージョンをリリースするには、`version.rb` のバージョン番号を更新してから `bundle exec rake release` を実行します。バージョンの git タグが作成され、git のコミットとタグが push され、`.gem` ファイルが [rubygems.org](https://rubygems.org) に公開されます。
|
|
234
|
+
|
|
235
|
+
## コントリビュート
|
|
236
|
+
|
|
237
|
+
バグ報告やプルリクエストは GitHub(https://github.com/jksy/resizing-gem )で受け付けています。
|
|
238
|
+
|
|
239
|
+
## ライセンス
|
|
240
|
+
|
|
241
|
+
この gem は [MIT License](https://opensource.org/licenses/MIT) のもとでオープンソースとして公開しています。
|
data/README.md
CHANGED
|
@@ -4,11 +4,19 @@
|
|
|
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
|
|
10
12
|
|
|
11
|
-
- Ruby 3.1.0 or later
|
|
13
|
+
- Ruby 3.1.0 or later (tested against 3.1, 3.2, 3.3, 3.4 and 4.0)
|
|
14
|
+
- Rails 7.0 or later for the CarrierWave integration (tested against 7.0, 7.1, 7.2, 8.0 and 8.1)
|
|
15
|
+
- Faraday 1.x or 2.x (tested against 1.10 and the latest 2.x)
|
|
16
|
+
|
|
17
|
+
The exact combinations exercised in CI are listed in `.github/workflows/test.yml`.
|
|
18
|
+
Support for a Ruby or Rails series that has reached upstream end-of-life may be dropped in
|
|
19
|
+
a minor release; such changes are announced in the release notes.
|
|
12
20
|
|
|
13
21
|
## Installation
|
|
14
22
|
|
|
@@ -31,12 +39,15 @@ Or install it yourself as:
|
|
|
31
39
|
```ruby
|
|
32
40
|
Resizing.configure = {
|
|
33
41
|
image_host: 'https://img.resizing.net',
|
|
34
|
-
video_host: 'https://video.resizing.net',
|
|
35
42
|
project_id: 'your-project-id',
|
|
36
43
|
secret_token: 'your-secret-token'
|
|
37
44
|
}
|
|
38
45
|
```
|
|
39
46
|
|
|
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.
|
|
50
|
+
|
|
40
51
|
## Usage
|
|
41
52
|
|
|
42
53
|
### Basic Client Usage
|
|
@@ -65,6 +76,25 @@ transformation_url = Resizing.url_from_image_id(image_id, nil, ['w_200', 'h_300'
|
|
|
65
76
|
# => "https://img.resizing.net/projects/.../upload/images/.../w_200,h_300"
|
|
66
77
|
```
|
|
67
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
|
+
|
|
68
98
|
### CarrierWave Integration
|
|
69
99
|
|
|
70
100
|
```ruby
|
|
@@ -81,10 +111,174 @@ class User
|
|
|
81
111
|
end
|
|
82
112
|
```
|
|
83
113
|
|
|
114
|
+
`process` declares the transformation applied when the browser fetches the image URL, so
|
|
115
|
+
the processors are never run as local image processing on upload.
|
|
116
|
+
|
|
117
|
+
The uploader validations hooked on CarrierWave's `:cache` callbacks are checked before the
|
|
118
|
+
image is uploaded to Resizing:
|
|
119
|
+
|
|
120
|
+
```ruby
|
|
121
|
+
class ImageUploader < CarrierWave::Uploader::Base
|
|
122
|
+
include Resizing::CarrierWave
|
|
123
|
+
|
|
124
|
+
def extension_allowlist
|
|
125
|
+
%w[jpg jpeg gif png]
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def content_type_allowlist
|
|
129
|
+
[%r{\Aimage/}]
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def size_range
|
|
133
|
+
0..(5 * 1024 * 1024)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
`extension_denylist` and `content_type_denylist` work the same way. A rejected file is not
|
|
139
|
+
uploaded, and the model becomes invalid with an error on the mounted column, as in plain
|
|
140
|
+
CarrierWave.
|
|
141
|
+
|
|
142
|
+
The extension checked by `extension_allowlist` / `extension_denylist` is derived from the
|
|
143
|
+
content type of the file whenever it is known, not from the file name. A file downloaded
|
|
144
|
+
from a Resizing URL (e.g. assigned through `remote_<column>_url=`) ends with the version
|
|
145
|
+
string, so its file name has no meaningful extension; the `Content-Type` of the response
|
|
146
|
+
is used instead. When the content type is unknown (`application/octet-stream`), the
|
|
147
|
+
extension of the file name is used as in plain CarrierWave.
|
|
148
|
+
|
|
149
|
+
Versions are not uploaded separately: a version only changes the transformation in the
|
|
150
|
+
generated URL, so a single upload backs every version.
|
|
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
|
+
|
|
84
211
|
## Development
|
|
85
212
|
|
|
213
|
+
### Dev Container (recommended)
|
|
214
|
+
|
|
215
|
+
This repository ships a [Dev Container](https://containers.dev/) so that everyone develops
|
|
216
|
+
against the same Ruby / MySQL setup.
|
|
217
|
+
|
|
218
|
+
Requirements: Docker and either VS Code + the *Dev Containers* extension, or the
|
|
219
|
+
[`devcontainer` CLI](https://github.com/devcontainers/cli).
|
|
220
|
+
|
|
221
|
+
1. Open the repository in VS Code and run **Dev Containers: Reopen in Container**
|
|
222
|
+
(or run `devcontainer up --workspace-folder .`).
|
|
223
|
+
2. `bundle install` and the MySQL wait are executed automatically by
|
|
224
|
+
`.devcontainer/post-create.sh`.
|
|
225
|
+
3. Inside the container:
|
|
226
|
+
|
|
227
|
+
```console
|
|
228
|
+
$ bundle exec rake test # run the tests
|
|
229
|
+
$ bundle exec rubocop # run the linter
|
|
230
|
+
$ bin/console # interactive prompt
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
The container provides:
|
|
234
|
+
|
|
235
|
+
- Ruby managed by [rbenv](https://github.com/rbenv/rbenv), so the version can be changed
|
|
236
|
+
from inside the container (see below)
|
|
237
|
+
- MySQL 5.7 with the `resizing_gem_test` database, matching CI
|
|
238
|
+
- `RAILS_VERSION` to test against another Rails version, matching the `RAILS_VERSION`
|
|
239
|
+
switch in the `Gemfile` and in CI. Unset, the `Gemfile` picks the newest supported Rails
|
|
240
|
+
that runs on the current Ruby (8.1, or 7.2 on Ruby 3.1)
|
|
241
|
+
|
|
242
|
+
Gems are installed into a named volume (`/usr/local/bundle`). `vendor/`, `.bundle/` and
|
|
243
|
+
`.ruby-lsp/` are kept on their own volumes so that Ruby artifacts generated on the host do
|
|
244
|
+
not leak into the container through the bind mount: `vendor/` holds gems whose native
|
|
245
|
+
extensions are built against the host's libraries, `.bundle/config` points `BUNDLE_PATH`
|
|
246
|
+
at it, and `.ruby-lsp/` records which gems the Ruby LSP extension has installed. The host
|
|
247
|
+
copies are left untouched.
|
|
248
|
+
|
|
249
|
+
#### Changing the Ruby version
|
|
250
|
+
|
|
251
|
+
Ruby 3.1.7 is installed when the image is built, and `.ruby-version` is honoured on
|
|
252
|
+
container creation. To switch to another version from inside the container:
|
|
253
|
+
|
|
254
|
+
```console
|
|
255
|
+
$ rbenv install 3.3.12
|
|
256
|
+
$ rbenv local 3.3.12 # writes .ruby-version (gitignored)
|
|
257
|
+
$ bundle install
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
`rbenv install` needs no `sudo`; the build dependencies are already in the image. To
|
|
261
|
+
change the version the image ships with, set the `RUBY_VERSION` build arg in
|
|
262
|
+
`.devcontainer/compose.yaml` and rebuild.
|
|
263
|
+
|
|
264
|
+
### Without a Dev Container
|
|
265
|
+
|
|
86
266
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
87
267
|
|
|
268
|
+
The tests need a MySQL server. `docker-compose.yml` in the repository root starts a
|
|
269
|
+
suitable one (`docker compose up -d mysql`).
|
|
270
|
+
|
|
271
|
+
Each test process creates its own database (`resizing_gem_test_<host>_<pid>`) and drops it
|
|
272
|
+
when the run finishes, so several test processes can share one MySQL server without
|
|
273
|
+
breaking each other's tables. Databases left behind by killed processes are cleaned up on
|
|
274
|
+
the next run.
|
|
275
|
+
|
|
276
|
+
The connection defaults to `root:secret@127.0.0.1:3306` and can be overridden with the
|
|
277
|
+
`MYSQL_HOST`, `MYSQL_PORT`, `MYSQL_USER` and `MYSQL_PASSWORD` environment variables.
|
|
278
|
+
Setting `MYSQL_DATABASE` uses that database as is and never drops it; in that case give
|
|
279
|
+
each concurrent test process a different name, otherwise they will overwrite each other's
|
|
280
|
+
tables.
|
|
281
|
+
|
|
88
282
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
89
283
|
|
|
90
284
|
## Contributing
|
data/Rakefile
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'bundler/gem_tasks'
|
|
4
4
|
require 'rake/testtask'
|
|
5
|
-
require '
|
|
5
|
+
require 'rubocop/rake_task'
|
|
6
6
|
|
|
7
7
|
Rake::TestTask.new(:test) do |t|
|
|
8
8
|
t.libs << 'test'
|
|
@@ -10,11 +10,9 @@ Rake::TestTask.new(:test) do |t|
|
|
|
10
10
|
t.test_files = FileList['test/**/*_test.rb']
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
config.future_release = Resizing::VERSION
|
|
18
|
-
end
|
|
13
|
+
RuboCop::RakeTask.new
|
|
14
|
+
|
|
15
|
+
desc 'Run the same checks as CI (RuboCop and the tests)'
|
|
16
|
+
task ci: %i[rubocop test]
|
|
19
17
|
|
|
20
18
|
task default: :test
|
data/bin/setup
CHANGED
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:
|