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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.devcontainer/compose.yaml +7 -4
  3. data/.github/dependabot.yml +23 -0
  4. data/.github/tools/actionlint/Dockerfile +3 -0
  5. data/.github/tools/gitleaks/Dockerfile +2 -0
  6. data/.github/workflows/actionlint.yml +27 -0
  7. data/.github/workflows/actions.lock +54 -0
  8. data/.github/workflows/labeler.yml +3 -1
  9. data/.github/workflows/lint.yml +2 -2
  10. data/.github/workflows/secret-scan.yml +29 -0
  11. data/.github/workflows/test.yml +37 -6
  12. data/.github/workflows/verify-lockfile.yml +28 -0
  13. data/CLAUDE.md +2 -2
  14. data/Gemfile +3 -0
  15. data/README.ja.md +241 -0
  16. data/README.md +83 -1
  17. data/docker-compose.yml +7 -0
  18. data/git-hooks/pre-push-ruby.sh +16 -11
  19. data/lib/resizing/carrier_wave/storage/file.rb +21 -2
  20. data/lib/resizing/carrier_wave/storage/remote.rb +18 -13
  21. data/lib/resizing/carrier_wave.rb +75 -7
  22. data/lib/resizing/client.rb +47 -3
  23. data/lib/resizing/mock_client.rb +25 -2
  24. data/lib/resizing/version.rb +1 -1
  25. data/lib/resizing.rb +4 -0
  26. data/test/rails_app/app/views/users/new.html.erb +6 -0
  27. data/test/rails_app/rails_app.rb +86 -0
  28. data/test/resizing/carrier_wave/storage/file_test.rb +40 -0
  29. data/test/resizing/carrier_wave/storage/remote_test.rb +66 -9
  30. data/test/resizing/carrier_wave_rails_integration_test.rb +94 -0
  31. data/test/resizing/carrier_wave_temporary_image_test.rb +280 -0
  32. data/test/resizing/carrier_wave_test.rb +15 -6
  33. data/test/resizing/carrier_wave_uploader_test.rb +14 -0
  34. data/test/resizing/client_test.rb +99 -9
  35. data/test/resizing/mock_client_test.rb +34 -0
  36. data/test/resizing_module_test.rb +16 -0
  37. data/test/test_helper.rb +23 -2
  38. data/test/vcr/carrier_wave_test/confirm.yml +23 -0
  39. data/test/vcr/carrier_wave_test/retrieve_from_cache.yml +23 -0
  40. data/test/vcr/carrier_wave_test/save.yml +1 -1
  41. data/test/vcr/carrier_wave_test/update_image.yml +42 -2
  42. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ff23b326ca457e2ba530fa2b299b7cb88c814fe08dc6469c3355506cdbb8f65
4
- data.tar.gz: d0425ae8a78d155163a813a164b6f7b1e98f2a9b10032832d50c917878fcfbce
3
+ metadata.gz: a3e94426f43cec8a3f2e0ff353fbe80de1f826599c2f5c1347152b70f67355dc
4
+ data.tar.gz: 27be738a572a08c1c059cf312a7f86487f5b3f4c2a693450f4aec38b856aa6b2
5
5
  SHA512:
6
- metadata.gz: 3e8a8b33fda6f8b6209c7b85fb7b2e89346b5059443b5de19f2334fb70019b1df4b92b0bf815e7f091e930a3e726e30f601d344e1330e2bb2ce83d6adbad3c9a
7
- data.tar.gz: ab6ea786bce933942de4e10b8826beb243a2285789ec48c9a33644aae954bfc7fa26817ab73c95516844661d60d5e6f2b5d36da2c6b0509eb6b4126602ca6874
6
+ metadata.gz: 2842a8b98e345387267078eb4094975248ede7453f0b20028831be6599f96614c7119e0ff8fe986295238e304bf5af73ce8d38a7ee7eee4bfc611e2fe2f08ed4
7
+ data.tar.gz: 8af5ab4079f72664958e345ef1ac0ff6a39bd028751a5591e45833171d9e838249c821943c79cb39f24b139a00e35f286a5f56981c4a40432e0d7f6854ef5afe
@@ -1,7 +1,3 @@
1
- # A distinct project name and service name so this does not collide with the
2
- # docker-compose.yml in the repository root (project resizing-gem, service mysql)
3
- name: resizing-gem-devcontainer
4
-
5
1
  services:
6
2
  app:
7
3
  build:
@@ -38,6 +34,13 @@ services:
38
34
  # mysql:5.7 is published for amd64 only, so run it emulated on Apple Silicon
39
35
  platform: linux/amd64
40
36
  command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_bin
37
+ # MySQL 5.7 sizes its file descriptor tables from the nofile limit. On hosts
38
+ # where containerd runs with LimitNOFILE=infinity, containers inherit about
39
+ # 2^31, and mysqld then allocates tens of GB of memory and never gets ready.
40
+ ulimits:
41
+ nofile:
42
+ soft: 65536
43
+ hard: 65536
41
44
  environment:
42
45
  MYSQL_ROOT_PASSWORD: secret
43
46
  MYSQL_DATABASE: resizing_gem_test
@@ -0,0 +1,23 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: /
5
+ schedule:
6
+ interval: weekly
7
+
8
+ # The official tool images that CI runs, pinned by tag and digest in
9
+ # the Dockerfiles under .github/tools/.
10
+ - package-ecosystem: docker
11
+ directories:
12
+ - /.github/tools/actionlint
13
+ - /.github/tools/gitleaks
14
+ schedule:
15
+ interval: weekly
16
+
17
+ - package-ecosystem: github-actions
18
+ directory: /
19
+ schedule:
20
+ interval: weekly
21
+ groups:
22
+ github-actions:
23
+ patterns: ['*']
@@ -0,0 +1,3 @@
1
+ # The official actionlint image, pinned by digest and updated by Dependabot.
2
+ # It bundles shellcheck, which actionlint uses to check `run:` scripts.
3
+ FROM rhysd/actionlint:1.7.12@sha256:b1934ee5f1c509618f2508e6eb47ee0d3520686341fec936f3b79331f9315667
@@ -0,0 +1,2 @@
1
+ # The official gitleaks image, pinned by digest and updated by Dependabot.
2
+ FROM zricethezav/gitleaks:v8.30.1@sha256:c00b6bd0aeb3071cbcb79009cb16a60dd9e0a7c60e2be9ab65d25e6bc8abbb7f
@@ -0,0 +1,27 @@
1
+ name: actionlint
2
+
3
+ on:
4
+ pull_request:
5
+ paths:
6
+ - .github/workflows/**
7
+ - .github/actionlint.yaml
8
+ - .github/tools/actionlint/**
9
+
10
+ permissions:
11
+ contents: read
12
+
13
+ jobs:
14
+ actionlint:
15
+ runs-on: ubuntu-24.04
16
+ timeout-minutes: 10
17
+ steps:
18
+ - uses: actions/checkout@v7.0.1
19
+ with:
20
+ persist-credentials: false
21
+ # The official image, pinned by digest in the Dockerfile.
22
+ - name: Build actionlint image
23
+ run: docker build --quiet --tag actionlint .github/tools/actionlint
24
+ - name: Run actionlint
25
+ run: |
26
+ docker run --rm -v "$PWD:/repo" -w /repo actionlint -version
27
+ docker run --rm -v "$PWD:/repo" -w /repo actionlint -color
@@ -0,0 +1,54 @@
1
+ # This file is machine-generated by `gh actions-lock`.
2
+ # Do not edit by hand; run `gh actions-lock` to update.
3
+ # Docs: https://gh.io/actions-lockfile
4
+ version: 'v0.0.3'
5
+ workflows:
6
+ '.github/workflows/actionlint.yml':
7
+ - 'actions/checkout@v7.0.1'
8
+ '.github/workflows/labeler.yml':
9
+ - 'actions/labeler@v7.0.0'
10
+ '.github/workflows/lint.yml':
11
+ - 'actions/checkout@v7.0.1'
12
+ - 'ruby/setup-ruby@v1.338.0'
13
+ '.github/workflows/secret-scan.yml':
14
+ - 'actions/checkout@v7.0.1'
15
+ '.github/workflows/test.yml':
16
+ - 'actions/checkout@v7.0.1'
17
+ - 'codecov/codecov-action@v7.1.1'
18
+ - 'mirromutth/mysql-action@v1.1'
19
+ - 'ruby/setup-ruby@v1.338.0'
20
+ '.github/workflows/verify-lockfile.yml':
21
+ - 'actions/checkout@v7.0.1'
22
+ dependencies:
23
+ 'actions/checkout@v7.0.1':
24
+ ref: 'v7.0.1'
25
+ commit: 'sha1-3d3c42e5aac5ba805825da76410c181273ba90b1'
26
+ owner_id: 44036562
27
+ repo_id: 197814629
28
+ 'actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd':
29
+ ref: 'v8.0.0'
30
+ commit: 'sha1-ed597411d8f924073f98dfc5c65a23a2325f34cd'
31
+ owner_id: 44036562
32
+ repo_id: 205262760
33
+ 'actions/labeler@v7.0.0':
34
+ ref: 'v7.0.0'
35
+ commit: 'sha1-bf12e9b00b37c5c0ca2b87b79b2daf7891dbda13'
36
+ owner_id: 44036562
37
+ repo_id: 177139928
38
+ 'codecov/codecov-action@v7.1.1':
39
+ ref: 'v7.1.1'
40
+ commit: 'sha1-303a32d7a59b442fa8d48b6a1cc6825c09c847a5'
41
+ owner_id: 8226205
42
+ repo_id: 200299178
43
+ uses:
44
+ - 'actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd'
45
+ 'mirromutth/mysql-action@v1.1':
46
+ ref: 'v1.1'
47
+ commit: 'sha1-de1fba8b3f90ce8db80f663a7043be3cf3231248'
48
+ owner_id: 29607994
49
+ repo_id: 206556246
50
+ 'ruby/setup-ruby@v1.338.0':
51
+ ref: 'v1.338.0'
52
+ commit: 'sha1-b3aaceaef7e0ee231b499e7b5f49c03fc1d32199'
53
+ owner_id: 210414
54
+ repo_id: 231208785
@@ -7,6 +7,8 @@ on:
7
7
  - synchronize
8
8
  - reopened
9
9
 
10
+ permissions: {}
11
+
10
12
  jobs:
11
13
  labeler:
12
14
  runs-on: ubuntu-latest
@@ -14,7 +16,7 @@ jobs:
14
16
  contents: read
15
17
  pull-requests: write
16
18
  steps:
17
- - uses: actions/labeler@b8dd2d9be0f68b860e7dae5dae7d772984eacd6d # v6
19
+ - uses: actions/labeler@v7.0.0
18
20
  with:
19
21
  repo-token: ${{ secrets.GITHUB_TOKEN }}
20
22
  sync-labels: false
@@ -15,14 +15,14 @@ jobs:
15
15
  runs-on: ubuntu-22.04
16
16
 
17
17
  steps:
18
- - uses: actions/checkout@v4
18
+ - uses: actions/checkout@v7.0.1
19
19
 
20
20
  - name: "Install ruby dependencies"
21
21
  run: |
22
22
  sudo apt update
23
23
  sudo apt install -y libyaml-dev curl libmysqlclient-dev
24
24
 
25
- - uses: ruby/setup-ruby@v1
25
+ - uses: ruby/setup-ruby@v1.338.0
26
26
  with:
27
27
  # Matches the devcontainer, so local and CI report the same offenses
28
28
  ruby-version: '3.1'
@@ -0,0 +1,29 @@
1
+ name: Secret scan
2
+
3
+ on:
4
+ pull_request:
5
+
6
+ permissions:
7
+ contents: read
8
+
9
+ jobs:
10
+ gitleaks:
11
+ runs-on: ubuntu-24.04
12
+ timeout-minutes: 10
13
+ steps:
14
+ # Check out the PR head rather than the merge commit, so the scanned
15
+ # range is exactly the commits the PR adds.
16
+ - uses: actions/checkout@v7.0.1
17
+ with:
18
+ ref: ${{ github.event.pull_request.head.sha }}
19
+ fetch-depth: 0
20
+ persist-credentials: false
21
+ # The official image, pinned by digest in the Dockerfile.
22
+ - name: Build gitleaks image
23
+ run: docker build --quiet --tag gitleaks .github/tools/gitleaks
24
+ - name: Scan the PR commits
25
+ env:
26
+ BASE_REF: ${{ github.base_ref }}
27
+ run: |
28
+ docker run --rm -v "$PWD:/repo" -w /repo gitleaks version
29
+ docker run --rm -v "$PWD:/repo" -w /repo gitleaks git --log-opts="origin/$BASE_REF..HEAD" --redact --verbose .
@@ -7,7 +7,6 @@ on:
7
7
  - master
8
8
 
9
9
  permissions:
10
- id-token: write # This is required for codecov/codecov-action
11
10
  contents: read # This is required for actions/checkout
12
11
 
13
12
  jobs:
@@ -38,7 +37,7 @@ jobs:
38
37
  FARADAY_VERSION: ${{ matrix.faraday }}
39
38
 
40
39
  steps:
41
- - uses: actions/checkout@v4
40
+ - uses: actions/checkout@v7.0.1
42
41
 
43
42
  - uses: mirromutth/mysql-action@v1.1
44
43
  with:
@@ -55,7 +54,7 @@ jobs:
55
54
  sudo apt update
56
55
  sudo apt install -y libyaml-dev curl libmysqlclient-dev
57
56
 
58
- - uses: ruby/setup-ruby@v1
57
+ - uses: ruby/setup-ruby@v1.338.0
59
58
  with:
60
59
  ruby-version: ${{ matrix.ruby }}
61
60
  bundler-cache: false
@@ -64,15 +63,47 @@ jobs:
64
63
  run: |
65
64
  bundle install
66
65
 
67
- - name: Show Faraday version
66
+ # Fail when the resolved Rails is not the one the matrix asks for, so that a
67
+ # loose Gemfile requirement cannot silently test another version
68
+ - name: Check Rails and Faraday versions
68
69
  run: |
69
- bundle list | grep faraday
70
+ bundle exec ruby -e '
71
+ require "rails"
72
+ require "faraday"
73
+ puts "Rails #{Rails::VERSION::STRING} / Faraday #{Faraday::VERSION}"
74
+ expected = ENV.fetch("RAILS_VERSION")
75
+ unless Rails::VERSION::STRING.start_with?("#{expected}.")
76
+ abort "Rails #{Rails::VERSION::STRING} is installed, but RAILS_VERSION is #{expected}"
77
+ end
78
+ '
70
79
 
71
80
  - name: Run test
72
81
  run: |
73
82
  bundle exec rake test
74
83
 
75
84
  - name: Upload coverage reports to Codecov
76
- uses: codecov/codecov-action@v3
85
+ uses: codecov/codecov-action@v7.1.1
77
86
  env:
78
87
  CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
88
+
89
+ # Single status for the required checks: fails unless every test job
90
+ # succeeded, including when one is skipped or cancelled.
91
+ ci-passed:
92
+ if: always()
93
+ needs: [test]
94
+ runs-on: ubuntu-24.04
95
+ timeout-minutes: 5
96
+
97
+ steps:
98
+ - name: Check test results
99
+ shell: bash
100
+ env:
101
+ NEEDS: ${{ toJSON(needs) }}
102
+ run: |
103
+ set -euo pipefail
104
+ jq -r 'to_entries[] | "\(.key): \(.value.result)"' <<<"$NEEDS"
105
+ failed="$(jq -r '[to_entries[] | select(.value.result != "success") | .key] | join(", ")' <<<"$NEEDS")"
106
+ if [[ -n "$failed" ]]; then
107
+ echo "::error::Test jobs did not succeed: $failed"
108
+ exit 1
109
+ fi
@@ -0,0 +1,28 @@
1
+ # Fails when .github/workflows/actions.lock is out of sync with the
2
+ # workflows or a locked pin no longer verifies upstream.
3
+
4
+ name: verify-lockfile
5
+
6
+ on:
7
+ pull_request:
8
+ push:
9
+ branches:
10
+ - master
11
+
12
+ permissions:
13
+ contents: read
14
+
15
+ jobs:
16
+ verify:
17
+ runs-on: ubuntu-24.04
18
+ timeout-minutes: 10
19
+ env:
20
+ GH_TOKEN: ${{ github.token }}
21
+ steps:
22
+ - uses: actions/checkout@v7.0.1
23
+ with:
24
+ persist-credentials: false
25
+ - name: Install gh-actions-lock
26
+ run: gh extension install github/gh-actions-lock --pin v0.4.0
27
+ - name: Verify the lockfile
28
+ run: gh actions-lock --verify
data/CLAUDE.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  ## 開発環境
6
6
 
7
- devcontainer(`.devcontainer/`)を用意してある。VS Code の "Reopen in Container"、または `devcontainer up --workspace-folder .` で、Ruby・MySQL・`bundle install` まで揃った状態になる。
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
8
 
9
9
  Ruby は rbenv 管理で `.ruby-version` に従う。別バージョンを試す場合はコンテナ内で `rbenv install <version>` → `rbenv local <version>` → `bundle install`(sudo 不要)。イメージに焼くバージョンを変える場合は `.devcontainer/compose.yaml` の `RUBY_VERSION` を変更してリビルドする。
10
10
 
@@ -37,7 +37,7 @@ bin/setup # bundle install + git hooks の有効化
37
37
  PRE_PUSH_SKIP_TESTS=1 git push # RuboCop だけにしてテストを省く
38
38
  ```
39
39
 
40
- チェックは gem が入っている環境で走る。`bundle check` が通る環境(devcontainer の中、または `bundle install` 済みのホスト)ならそのまま、通らなければ devcontainer CLI か docker compose 経由で devcontainer 内で実行する。
40
+ チェックは gem が入っている環境で走る。`bundle check` が通る環境(devcontainer の中、または `bundle install` 済みのホスト)ならそのまま、通らなければそのチェックアウト用に起動した devcontainer の中で実行する(コンテナの `devcontainer.local_folder` ラベルで探すので、別の worktree のコンテナは使わない)。見つからなければ `devcontainer up --workspace-folder .` を促して中断する。
41
41
 
42
42
  ## PR 運用
43
43
 
data/Gemfile CHANGED
@@ -9,6 +9,9 @@ gemspec
9
9
  # Unset, the newest supported Rails that runs on the current Ruby is used:
10
10
  # Rails 8.x needs Ruby 3.2+, so Ruby 3.1 falls back to 7.2.
11
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
12
15
  gem 'rails', "~> #{rails_version}"
13
16
 
14
17
  # Allow testing against different Faraday versions (e.g. FARADAY_VERSION=1.10).
data/README.ja.md ADDED
@@ -0,0 +1,241 @@
1
+ # Resizing
2
+
3
+ [![Gem Version](https://img.shields.io/gem/v/resizing.svg)](https://rubygems.org/gems/resizing)
4
+ [![test](https://github.com/jksy/resizing-gem/actions/workflows/test.yml/badge.svg)](https://github.com/jksy/resizing-gem/actions/workflows/test.yml)
5
+ [![codecov](https://codecov.io/gh/jksy/resizing-gem/graph/badge.svg)](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) のもとでオープンソースとして公開しています。