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.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.devcontainer/Dockerfile +73 -0
  3. data/.devcontainer/compose.yaml +61 -0
  4. data/.devcontainer/devcontainer-lock.json +9 -0
  5. data/.devcontainer/devcontainer.json +32 -0
  6. data/.devcontainer/post-create.sh +41 -0
  7. data/.github/copilot-instructions.md +1 -0
  8. data/.github/dependabot.yml +23 -0
  9. data/.github/labeler.yml +28 -0
  10. data/.github/release.yml +35 -0
  11. data/.github/tools/actionlint/Dockerfile +3 -0
  12. data/.github/tools/gitleaks/Dockerfile +2 -0
  13. data/.github/workflows/actionlint.yml +27 -0
  14. data/.github/workflows/actions.lock +54 -0
  15. data/.github/workflows/labeler.yml +22 -0
  16. data/.github/workflows/lint.yml +37 -0
  17. data/.github/workflows/secret-scan.yml +29 -0
  18. data/.github/workflows/test.yml +51 -13
  19. data/.github/workflows/verify-lockfile.yml +28 -0
  20. data/AGENTS.md +1 -0
  21. data/CHANGELOG.md +3 -0
  22. data/CLAUDE.md +68 -0
  23. data/Gemfile +12 -3
  24. data/README.ja.md +241 -0
  25. data/README.md +196 -2
  26. data/Rakefile +5 -7
  27. data/bin/setup +3 -0
  28. data/docker-compose.yml +7 -0
  29. data/git-hooks/pre-push +6 -0
  30. data/git-hooks/pre-push-ruby.sh +53 -0
  31. data/lib/resizing/carrier_wave/callback_file.rb +88 -0
  32. data/lib/resizing/carrier_wave/storage/file.rb +105 -38
  33. data/lib/resizing/carrier_wave/storage/remote.rb +25 -16
  34. data/lib/resizing/carrier_wave.rb +109 -6
  35. data/lib/resizing/client.rb +47 -4
  36. data/lib/resizing/configurable.rb +1 -1
  37. data/lib/resizing/configuration.rb +40 -10
  38. data/lib/resizing/mock_client.rb +48 -9
  39. data/lib/resizing/public_id.rb +3 -2
  40. data/lib/resizing/version.rb +1 -1
  41. data/lib/resizing.rb +4 -1
  42. data/resizing.gemspec +3 -3
  43. data/test/rails_app/app/views/users/new.html.erb +6 -0
  44. data/test/rails_app/rails_app.rb +86 -0
  45. data/test/resizing/carrier_wave/storage/file_test.rb +495 -9
  46. data/test/resizing/carrier_wave/storage/remote_test.rb +151 -12
  47. data/test/resizing/carrier_wave_cache_callbacks_test.rb +224 -0
  48. data/test/resizing/carrier_wave_callback_file_test.rb +77 -0
  49. data/test/resizing/carrier_wave_rails_integration_test.rb +94 -0
  50. data/test/resizing/carrier_wave_temporary_image_test.rb +280 -0
  51. data/test/resizing/carrier_wave_test.rb +214 -30
  52. data/test/resizing/carrier_wave_uploader_test.rb +225 -0
  53. data/test/resizing/client_test.rb +419 -30
  54. data/test/resizing/configuration_test.rb +235 -10
  55. data/test/resizing/http_clientable_test.rb +27 -0
  56. data/test/resizing/mock_client_test.rb +145 -1
  57. data/test/resizing/public_id_test.rb +96 -0
  58. data/test/resizing_module_test.rb +126 -0
  59. data/test/resizing_test.rb +8 -0
  60. data/test/test_helper.rb +318 -9
  61. data/test/vcr/carrier_wave_test/confirm.yml +23 -0
  62. data/test/vcr/carrier_wave_test/retrieve_from_cache.yml +23 -0
  63. data/test/vcr/carrier_wave_test/save.yml +1 -1
  64. data/test/vcr/carrier_wave_test/update_image.yml +42 -2
  65. metadata +47 -9
@@ -6,7 +6,7 @@ module Resizing
6
6
  mod.send(:attr_reader, :config)
7
7
  end
8
8
 
9
- def initialize_config *attrs
9
+ def initialize_config(*attrs)
10
10
  config = if attrs.first.is_a? Configuration
11
11
  attrs.first
12
12
  elsif attrs.first.nil?
@@ -15,10 +15,17 @@ module Resizing
15
15
  # Resizing::Client.new(configuration)
16
16
  #++
17
17
  class Configuration
18
- attr_reader :image_host, :video_host, :project_id, :secret_token, :open_timeout, :response_timeout, :enable_mock
18
+ attr_reader :image_host, :project_id, :secret_token, :open_timeout, :response_timeout, :enable_mock
19
19
  DEFAULT_HOST = 'https://img.resizing.net'
20
20
  DEFAULT_IMAGE_HOST = 'https://img.resizing.net'
21
- DEFAULT_VIDEO_HOST = 'https://video.resizing.net'
21
+ VIDEO_HOST_FALLBACK = 'https://video.resizing.net'
22
+ private_constant :VIDEO_HOST_FALLBACK
23
+ # 動画 API はサービス側で廃止されたため非推奨。将来のバージョンで削除する
24
+ DEFAULT_VIDEO_HOST = VIDEO_HOST_FALLBACK
25
+ deprecate_constant :DEFAULT_VIDEO_HOST
26
+ DEPRECATED_VIDEO_HOST_MESSAGE = '[resizing] video_host is deprecated: ' \
27
+ 'the video API has been removed from Resizing and this value is no longer used. ' \
28
+ 'It will be deleted in a future version.'
22
29
  DEFAULT_OPEN_TIMEOUT = 2
23
30
  DEFAULT_RESPONSE_TIMEOUT = 10
24
31
 
@@ -28,7 +35,7 @@ module Resizing
28
35
  case attr = attrs.first
29
36
  when Hash
30
37
  raise_configiration_error if attr[:project_id].nil? || attr[:secret_token].nil?
31
- raise_configiration_error if attr[:host].present?
38
+ raise_deprecated_host_error unless blank_value?(attr[:host])
32
39
 
33
40
  initialize_by_hash attr
34
41
  return
@@ -79,32 +86,55 @@ module Resizing
79
86
  ::SecureRandom.uuid
80
87
  end
81
88
 
89
+ # 動画 API の廃止により非推奨。値は保持するが利用されない
90
+ def video_host
91
+ warn_video_host_deprecation
92
+ @video_host
93
+ end
94
+
82
95
  def ==(other)
83
96
  return false unless self.class == other.class
84
97
 
85
- %i[image_host video_host project_id secret_token open_timeout response_timeout].all? do |name|
98
+ return false unless @video_host == other.instance_variable_get(:@video_host)
99
+
100
+ %i[image_host project_id secret_token open_timeout response_timeout].all? do |name|
86
101
  send(name) == other.send(name)
87
102
  end
88
103
  end
89
104
 
90
105
  private
91
106
 
107
+ # ActiveSupport の Object#present? / #blank? に依存しないための素の Ruby 実装。
108
+ # このgemはRails外からも利用されるため、activesupportがロードされていない前提で動作する必要がある。
109
+ def blank_value?(value)
110
+ value.nil? || value.to_s.strip.empty?
111
+ end
112
+
113
+ def raise_deprecated_host_error
114
+ raise ConfigurationError, 'The host on configuration is deprecated. Use image_host'
115
+ end
116
+
117
+ def deprecated_video_host(value)
118
+ warn_video_host_deprecation unless value.nil?
119
+ value.dup.freeze || VIDEO_HOST_FALLBACK
120
+ end
121
+
122
+ def warn_video_host_deprecation
123
+ warn DEPRECATED_VIDEO_HOST_MESSAGE
124
+ end
125
+
92
126
  def raise_configiration_error
93
- raise ConfigurationError, 'need hash and some keys like :image_host, video_host, :project_id, :secret_token'
127
+ raise ConfigurationError, 'need hash and some keys like :image_host, :project_id, :secret_token'
94
128
  end
95
129
 
96
- # rubocop:disable Metrics/AbcSize
97
130
  def initialize_by_hash(attr)
98
- raise 'The host on configuration is deprecated. Use image_host, video_host' if attr[:host].present?
99
-
100
131
  @image_host = attr[:image_host].dup.freeze || DEFAULT_IMAGE_HOST
101
- @video_host = attr[:video_host].dup.freeze || DEFAULT_VIDEO_HOST
132
+ @video_host = deprecated_video_host(attr[:video_host])
102
133
  @project_id = attr[:project_id].dup.freeze
103
134
  @secret_token = attr[:secret_token].dup.freeze
104
135
  @open_timeout = attr[:open_timeout] || DEFAULT_OPEN_TIMEOUT
105
136
  @response_timeout = attr[:response_timeout] || DEFAULT_RESPONSE_TIMEOUT
106
137
  @enable_mock = attr[:enable_mock] || false
107
138
  end
108
- # rubocop:enable Metrics/AbcSize
109
139
  end
110
140
  end
@@ -1,42 +1,81 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'securerandom'
4
+
3
5
  module Resizing
4
6
  class MockClient
5
- def post(_file_or_binary, _options = {})
7
+ # Resizing の既定の一時画像の有効期限 (秒)
8
+ DEFAULT_TEMPORARY_EXPIRES_IN = 24 * 60 * 60
9
+
10
+ def post(_file_or_binary, options = {})
6
11
  r = load_yaml('test/vcr/client/post.yml')
7
- 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
8
28
  end
9
29
 
10
30
  def put(name, _file_or_binary, _options)
11
31
  r = load_yaml('test/vcr/client/put.yml')
12
32
  result = JSON.parse(r['string'])
13
- # replace name, public_id and version by name argument
33
+ # カセット内の値に依存せず、name と新しい version で id / public_id を組み立て直す
34
+ version = generate_version
14
35
  result['id'] = name
15
- result['public_id'].gsub!(/AWEaewfAreaweFAFASfwe/, name)
16
- result['public_id'].gsub!(/v6Ew3HmDAYfb3NMRdLxR45i_gXMbLlGyi/, "v#{Time.now.to_f}")
36
+ result['version'] = version
37
+ result['latest_version_id'] = version
38
+ result['public_id'] = build_public_id(result['public_id'], image_id: name, version: version)
17
39
  result
18
40
  end
19
41
 
20
42
  def delete(name)
21
43
  r = load_yaml('test/vcr/client/delete.yml')
22
44
  result = JSON.parse(r['string'])
23
- # replace name and public_id by name argument
45
+ # カセット内の値に依存せず、name で id / public_id を組み立て直す
24
46
  result['id'] = name
25
- result['public_id'].gsub!(/28c49144-c00d-4cb5-8619-98ce95977b9c/, name)
47
+ result['public_id'] = build_public_id(result['public_id'], image_id: name)
26
48
  result
27
49
  end
28
50
 
29
51
  def metadata(name, _options = {})
30
52
  r = load_yaml('test/vcr/client/metadata.yml')
31
53
  result = JSON.parse(r['string'])
32
- # replace name and public_id by name argument
54
+ # カセット内の値に依存せず、name で id / public_id を組み立て直す
33
55
  result['id'] = name
34
- result['public_id'].gsub!(/bfdaf2b3-7ec5-41f4-9caa-d53247dd9666/, name)
56
+ result['public_id'] = build_public_id(result['public_id'], image_id: name)
35
57
  result
36
58
  end
37
59
 
38
60
  private
39
61
 
62
+ # カセットの public_id を PublicId で分解し、image_id / version を差し替えて組み立て直す
63
+ def build_public_id(public_id, image_id:, version: nil)
64
+ parsed = Resizing::PublicId.new(public_id)
65
+ version ||= parsed.version
66
+ identifier = "/projects/#{parsed.project_id}/upload/images/#{image_id}"
67
+ version.nil? ? identifier : "#{identifier}/v#{version}"
68
+ end
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
+
75
+ def generate_version
76
+ SecureRandom.alphanumeric(32)
77
+ end
78
+
40
79
  def load_yaml(filename)
41
80
  path = "#{library_root}/#{filename}"
42
81
  YAML.load_file(path)['http_interactions'].first['response']['body']
@@ -7,8 +7,9 @@ module Resizing
7
7
  parsed
8
8
  end
9
9
 
10
+ # nil / 空文字 / 空白のみの文字列はいずれも「値なし」として扱う
10
11
  def empty?
11
- @public_id.to_s.empty?
12
+ @public_id.to_s.strip.empty?
12
13
  end
13
14
 
14
15
  def image_id
@@ -39,7 +40,7 @@ module Resizing
39
40
  private
40
41
 
41
42
  def parsed
42
- return nil if @public_id.nil?
43
+ return nil if empty?
43
44
 
44
45
  unless defined? @parsed
45
46
  @parsed = Resizing.separate_public_id(@public_id)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Resizing
4
- VERSION = '1.2.2'
4
+ VERSION = '1.4.0'
5
5
  end
data/lib/resizing.rb CHANGED
@@ -14,7 +14,6 @@ module Resizing
14
14
  autoload :Configuration, 'resizing/configuration'
15
15
  autoload :CarrierWave, 'resizing/carrier_wave'
16
16
  autoload :PublicId, 'resizing/public_id'
17
- autoload :Video, 'resizing/video'
18
17
  autoload :ActiveStorage, 'resizing/active_storage'
19
18
 
20
19
  class Error < StandardError; end
@@ -59,6 +58,10 @@ module Resizing
59
58
  client.put name, file_or_binary, options
60
59
  end
61
60
 
61
+ def self.confirm(name)
62
+ client.confirm name
63
+ end
64
+
62
65
  def self.delete(name)
63
66
  client.delete name
64
67
  end
data/resizing.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.description = 'Client and utilities for Resizing '
13
13
  spec.homepage = 'https://github.com/jksy/resizing-gem'
14
14
  spec.license = 'MIT'
15
- spec.required_ruby_version = Gem::Requirement.new('>= 3.0.0')
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 3.1.0')
16
16
 
17
17
  spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
18
 
@@ -26,8 +26,8 @@ Gem::Specification.new do |spec|
26
26
  `git ls-files -z`.split("\x0")
27
27
  end
28
28
  spec.require_paths = ['lib']
29
- spec.add_runtime_dependency 'faraday', '~> 2.3'
30
- spec.add_runtime_dependency 'faraday-multipart'
29
+ spec.add_runtime_dependency 'faraday', '>= 1.0', '< 3'
30
+ spec.add_runtime_dependency 'faraday-multipart', '>= 1.0'
31
31
  spec.add_development_dependency 'carrierwave', '~> 2.2.5'
32
32
  spec.add_development_dependency 'fog-aws'
33
33
  spec.add_development_dependency 'minitest', '~> 5.16'
@@ -0,0 +1,6 @@
1
+ <%= form_with model: @user do |form| %>
2
+ <%= form.text_field :name %>
3
+ <%= form.file_field :image %>
4
+ <%= form.hidden_field :image_cache %>
5
+ <%= form.submit %>
6
+ <% end %>
@@ -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