dynamic_image 2.0.23 → 2.0.24

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2a9eeb1b5378e17d2dcf9d6c0245df5386a1253dfdbefd0fff61fd5faeb2a1ee
4
- data.tar.gz: 3156acfe1931d9530dc5c23d51716904feb2f1f16f00657cd9aa52c61d97cf94
3
+ metadata.gz: 6828f7825142e16348273c97f77cda19d191849ff09e545174c8bb445372257e
4
+ data.tar.gz: 480ebd62de3ddf2a780971b1b63c1ef545dff59f7f54ee1f7ede5ebef33a485b
5
5
  SHA512:
6
- metadata.gz: d3c0d2fd1e238b181b8d7cd86ebb12c65ebbe37e606bf434c969584def13b968e0bbd66b845b553d65c7d8e9245f20a066b88ee66f523de87391adf285aa194a
7
- data.tar.gz: 51eda1715d2eb18931256018f95dfe9f714817bda16601d5c1ded50778cc4a0a31d27ec00ffe3287a88205e1c49cea09c0d159594c8552a2a94084821ff403c1
6
+ metadata.gz: 95279e2367c7f8639be599e3717fd815bcf4f482a59ab3bcafd1e51aac3ce7c86621a1f694a2dfced38473de1b73fde9e7396a085d7e0cc6a335270267ddccfb
7
+ data.tar.gz: 62e6c9b6b440a51855ae1d5a89bb2b85fab6c126603aa5e0b08f58b372bf7007ddb3e5eb7c9d910a2bb0ee59a0fb217711e706e5adbdb19008f0e97d9ec67087
@@ -12,6 +12,7 @@ require "dynamic_image/errors"
12
12
  require "dynamic_image/helper"
13
13
  require "dynamic_image/image_reader"
14
14
  require "dynamic_image/image_sizing"
15
+ require "dynamic_image/jobs"
15
16
  require "dynamic_image/metadata"
16
17
  require "dynamic_image/model"
17
18
  require "dynamic_image/processed_image"
@@ -44,13 +44,19 @@ module DynamicImage
44
44
  private
45
45
 
46
46
  def cache_expiration_header
47
- expires_in 30.days, public: true
47
+ expires_in 30.days, public: true if response.status == 200
48
48
  end
49
49
 
50
50
  def find_record
51
51
  @record = model.find(params[:id])
52
52
  end
53
53
 
54
+ def process_later?(processed_image, size)
55
+ image_size = processed_image.record.size.x * processed_image.record.size.y
56
+ image_size > 12_500_000 &&
57
+ !processed_image.find_variant(size)
58
+ end
59
+
54
60
  def render_image(options)
55
61
  return unless stale?(@record)
56
62
 
@@ -60,7 +66,7 @@ module DynamicImage
60
66
  layout: false, locals: { options: options })
61
67
  end
62
68
  format.any(:gif, :jpeg, :jpg, :png, :tiff, :webp) do
63
- send_image(DynamicImage::ProcessedImage.new(@record, options))
69
+ send_image(@record, options)
64
70
  end
65
71
  end
66
72
  end
@@ -82,12 +88,17 @@ module DynamicImage
82
88
  params[:format]
83
89
  end
84
90
 
85
- def send_image(processed_image)
86
- send_data(
87
- processed_image.cropped_and_resized(requested_size),
88
- content_type: processed_image.content_type,
89
- disposition: "inline"
90
- )
91
+ def send_image(image, options)
92
+ processed_image = DynamicImage::ProcessedImage.new(image, options)
93
+ if process_later?(processed_image, requested_size)
94
+ DynamicImage::Jobs::CreateVariant
95
+ .perform_later(image, options, requested_size.to_s)
96
+ head 503, retry_after: 10
97
+ else
98
+ send_data(processed_image.cropped_and_resized(requested_size),
99
+ content_type: processed_image.content_type,
100
+ disposition: "inline")
101
+ end
91
102
  end
92
103
 
93
104
  def verify_signed_params
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dynamic_image/jobs/create_variant"
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DynamicImage
4
+ module Jobs
5
+ # = Create variant
6
+ #
7
+ # Creates an image variant.
8
+ class CreateVariant < ActiveJob::Base
9
+ queue_as :dis
10
+
11
+ def perform(record, options, size)
12
+ size_v = Vector2d.parse(size)
13
+ DynamicImage::ProcessedImage.new(record, options)
14
+ .find_or_create_variant(size_v)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -6,6 +6,8 @@ module DynamicImage
6
6
  # Handles all processing of images. Takes an instance of
7
7
  # +DynamicImage::Model+ as argument.
8
8
  class ProcessedImage
9
+ attr_reader :record
10
+
9
11
  def initialize(record, options = {})
10
12
  @record = record
11
13
  @uncropped = options[:uncropped] ? true : false
@@ -40,6 +42,20 @@ module DynamicImage
40
42
  find_or_create_variant(size).data
41
43
  end
42
44
 
45
+ # Find or create a variant with the given size.
46
+ def find_or_create_variant(size)
47
+ find_variant(size) || create_variant(size)
48
+ rescue ActiveRecord::RecordNotUnique
49
+ find_variant(size)
50
+ end
51
+
52
+ # Find a variant with the given size.
53
+ def find_variant(size)
54
+ return nil unless record.persisted?
55
+
56
+ record.variants.find_by(variant_params(size))
57
+ end
58
+
43
59
  # Normalizes the image.
44
60
  #
45
61
  # * Applies EXIF rotation
@@ -95,12 +111,6 @@ module DynamicImage
95
111
  end
96
112
  end
97
113
 
98
- def find_or_create_variant(size)
99
- record.variants.find_by(variant_params(size)) || create_variant(size)
100
- rescue ActiveRecord::RecordNotUnique
101
- record.variants.find_by(variant_params(size))
102
- end
103
-
104
114
  def format
105
115
  @format ||= record_format
106
116
  end
@@ -110,7 +120,7 @@ module DynamicImage
110
120
  end
111
121
 
112
122
  def jpeg?
113
- content_type == "image/jpeg" || content_type == "image/jpeg"
123
+ content_type == "image/jpeg"
114
124
  end
115
125
 
116
126
  def image_sizing
@@ -137,8 +147,6 @@ module DynamicImage
137
147
  result
138
148
  end
139
149
 
140
- attr_reader :record
141
-
142
150
  def record_format
143
151
  { "image/bmp" => "BMP", "image/png" => "PNG", "image/gif" => "GIF",
144
152
  "image/jpeg" => "JPEG", "image/pjpeg" => "JPEG", "image/tiff" => "TIFF",
@@ -13,8 +13,9 @@ module DynamicImage
13
13
  options = {
14
14
  path: "#{resource_name}/:digest(/:size)",
15
15
  constraints: { size: /\d+x\d+/ },
16
- only: [:show]
16
+ only: %i[show]
17
17
  }.merge(options)
18
+
18
19
  resources resource_name, options do
19
20
  get :uncropped, on: :member
20
21
  get :original, on: :member
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DynamicImage
4
- VERSION = "2.0.23"
4
+ VERSION = "2.0.24"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic_image
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.23
4
+ version: 2.0.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Inge Jørgensen
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-23 00:00:00.000000000 Z
11
+ date: 2020-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dis
@@ -155,6 +155,8 @@ files:
155
155
  - lib/dynamic_image/helper.rb
156
156
  - lib/dynamic_image/image_reader.rb
157
157
  - lib/dynamic_image/image_sizing.rb
158
+ - lib/dynamic_image/jobs.rb
159
+ - lib/dynamic_image/jobs/create_variant.rb
158
160
  - lib/dynamic_image/metadata.rb
159
161
  - lib/dynamic_image/model.rb
160
162
  - lib/dynamic_image/model/dimensions.rb
@@ -171,7 +173,7 @@ homepage: https://github.com/elektronaut/dynamic_image
171
173
  licenses:
172
174
  - MIT
173
175
  metadata: {}
174
- post_install_message:
176
+ post_install_message:
175
177
  rdoc_options: []
176
178
  require_paths:
177
179
  - lib
@@ -187,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
187
189
  version: '0'
188
190
  requirements: []
189
191
  rubygems_version: 3.1.2
190
- signing_key:
192
+ signing_key:
191
193
  specification_version: 4
192
194
  summary: Rails plugin that simplifies image uploading and processing
193
195
  test_files: []