activestorage 5.2.6 → 6.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activestorage might be problematic. Click here for more details.

Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +102 -114
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +6 -5
  5. data/app/assets/javascripts/activestorage.js +4 -1
  6. data/app/controllers/active_storage/base_controller.rb +3 -5
  7. data/app/controllers/active_storage/blobs_controller.rb +1 -1
  8. data/app/controllers/active_storage/disk_controller.rb +4 -1
  9. data/app/controllers/active_storage/representations_controller.rb +1 -1
  10. data/app/controllers/concerns/active_storage/set_current.rb +15 -0
  11. data/app/javascript/activestorage/blob_record.js +6 -1
  12. data/app/jobs/active_storage/analyze_job.rb +4 -0
  13. data/app/jobs/active_storage/base_job.rb +0 -1
  14. data/app/jobs/active_storage/purge_job.rb +3 -0
  15. data/app/models/active_storage/attachment.rb +18 -9
  16. data/app/models/active_storage/blob.rb +63 -22
  17. data/app/models/active_storage/blob/representable.rb +5 -5
  18. data/app/models/active_storage/filename.rb +0 -6
  19. data/app/models/active_storage/preview.rb +3 -3
  20. data/app/models/active_storage/variant.rb +51 -52
  21. data/app/models/active_storage/variation.rb +23 -32
  22. data/config/routes.rb +13 -12
  23. data/db/update_migrate/20180723000244_add_foreign_key_constraint_to_active_storage_attachments_for_blob_id.rb +7 -0
  24. data/lib/active_storage.rb +13 -2
  25. data/lib/active_storage/analyzer.rb +9 -4
  26. data/lib/active_storage/analyzer/video_analyzer.rb +2 -4
  27. data/lib/active_storage/attached.rb +7 -22
  28. data/lib/active_storage/attached/changes.rb +16 -0
  29. data/lib/active_storage/attached/changes/create_many.rb +46 -0
  30. data/lib/active_storage/attached/changes/create_one.rb +68 -0
  31. data/lib/active_storage/attached/changes/create_one_of_many.rb +10 -0
  32. data/lib/active_storage/attached/changes/delete_many.rb +23 -0
  33. data/lib/active_storage/attached/changes/delete_one.rb +19 -0
  34. data/lib/active_storage/attached/many.rb +16 -10
  35. data/lib/active_storage/attached/model.rb +140 -0
  36. data/lib/active_storage/attached/one.rb +16 -19
  37. data/lib/active_storage/downloader.rb +44 -0
  38. data/lib/active_storage/downloading.rb +8 -0
  39. data/lib/active_storage/engine.rb +35 -6
  40. data/lib/active_storage/errors.rb +22 -3
  41. data/lib/active_storage/gem_version.rb +4 -4
  42. data/lib/active_storage/previewer.rb +21 -11
  43. data/lib/active_storage/previewer/poppler_pdf_previewer.rb +3 -3
  44. data/lib/active_storage/previewer/video_previewer.rb +2 -3
  45. data/lib/active_storage/reflection.rb +64 -0
  46. data/lib/active_storage/service.rb +5 -6
  47. data/lib/active_storage/service/azure_storage_service.rb +30 -14
  48. data/lib/active_storage/service/configurator.rb +3 -1
  49. data/lib/active_storage/service/disk_service.rb +20 -16
  50. data/lib/active_storage/service/gcs_service.rb +48 -46
  51. data/lib/active_storage/service/mirror_service.rb +1 -1
  52. data/lib/active_storage/service/s3_service.rb +10 -9
  53. data/lib/active_storage/transformers/image_processing_transformer.rb +39 -0
  54. data/lib/active_storage/transformers/mini_magick_transformer.rb +38 -0
  55. data/lib/active_storage/transformers/transformer.rb +42 -0
  56. data/lib/tasks/activestorage.rake +7 -0
  57. metadata +28 -16
  58. data/app/models/active_storage/filename/parameters.rb +0 -36
  59. data/lib/active_storage/attached/macros.rb +0 -110
@@ -9,7 +9,7 @@ module ActiveStorage
9
9
  class Service::MirrorService < Service
10
10
  attr_reader :primary, :mirrors
11
11
 
12
- delegate :download, :download_chunk, :exist?, :url, :path_for, to: :primary
12
+ delegate :download, :download_chunk, :exist?, :url, to: :primary
13
13
 
14
14
  # Stitch together from named services.
15
15
  def self.build(primary:, mirrors:, configurator:, **options) #:nodoc:
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- gem "aws-sdk-s3", "~> 1.48"
4
-
5
3
  require "aws-sdk-s3"
6
4
  require "active_support/core_ext/numeric/bytes"
7
5
 
@@ -20,11 +18,9 @@ module ActiveStorage
20
18
 
21
19
  def upload(key, io, checksum: nil, **)
22
20
  instrument :upload, key: key, checksum: checksum do
23
- begin
24
- object_for(key).put(upload_options.merge(body: io, content_md5: checksum))
25
- rescue Aws::S3::Errors::BadDigest
26
- raise ActiveStorage::IntegrityError
27
- end
21
+ object_for(key).put(upload_options.merge(body: io, content_md5: checksum))
22
+ rescue Aws::S3::Errors::BadDigest
23
+ raise ActiveStorage::IntegrityError
28
24
  end
29
25
  end
30
26
 
@@ -36,6 +32,8 @@ module ActiveStorage
36
32
  else
37
33
  instrument :download, key: key do
38
34
  object_for(key).get.body.string.force_encoding(Encoding::BINARY)
35
+ rescue Aws::S3::Errors::NoSuchKey
36
+ raise ActiveStorage::FileNotFoundError
39
37
  end
40
38
  end
41
39
  end
@@ -43,6 +41,8 @@ module ActiveStorage
43
41
  def download_chunk(key, range)
44
42
  instrument :download_chunk, key: key, range: range do
45
43
  object_for(key).get(range: "bytes=#{range.begin}-#{range.exclude_end? ? range.end - 1 : range.end}").body.read.force_encoding(Encoding::BINARY)
44
+ rescue Aws::S3::Errors::NoSuchKey
45
+ raise ActiveStorage::FileNotFoundError
46
46
  end
47
47
  end
48
48
 
@@ -81,8 +81,7 @@ module ActiveStorage
81
81
  def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
82
82
  instrument :url, key: key do |payload|
83
83
  generated_url = object_for(key).presigned_url :put, expires_in: expires_in.to_i,
84
- content_type: content_type, content_length: content_length, content_md5: checksum,
85
- whitelist_headers: ['content-length']
84
+ content_type: content_type, content_length: content_length, content_md5: checksum
86
85
 
87
86
  payload[:url] = generated_url
88
87
 
@@ -106,6 +105,8 @@ module ActiveStorage
106
105
  chunk_size = 5.megabytes
107
106
  offset = 0
108
107
 
108
+ raise ActiveStorage::FileNotFoundError unless object.exists?
109
+
109
110
  while offset < object.content_length
110
111
  yield object.get(range: "bytes=#{offset}-#{offset + chunk_size - 1}").body.read.force_encoding(Encoding::BINARY)
111
112
  offset += chunk_size
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "image_processing"
4
+
5
+ module ActiveStorage
6
+ module Transformers
7
+ class ImageProcessingTransformer < Transformer
8
+ private
9
+ def process(file, format:)
10
+ processor.
11
+ source(file).
12
+ loader(page: 0).
13
+ convert(format).
14
+ apply(operations).
15
+ call
16
+ end
17
+
18
+ def processor
19
+ ImageProcessing.const_get(ActiveStorage.variant_processor.to_s.camelize)
20
+ end
21
+
22
+ def operations
23
+ transformations.each_with_object([]) do |(name, argument), list|
24
+ if name.to_s == "combine_options"
25
+ ActiveSupport::Deprecation.warn <<~WARNING
26
+ Active Storage's ImageProcessing transformer doesn't support :combine_options,
27
+ as it always generates a single ImageMagick command. Passing :combine_options will
28
+ not be supported in Rails 6.1.
29
+ WARNING
30
+
31
+ list.concat argument.keep_if { |key, value| value.present? }.to_a
32
+ elsif argument.present?
33
+ list << [ name, argument ]
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mini_magick"
4
+
5
+ module ActiveStorage
6
+ module Transformers
7
+ class MiniMagickTransformer < Transformer
8
+ private
9
+ def process(file, format:)
10
+ image = MiniMagick::Image.new(file.path, file)
11
+
12
+ transformations.each do |name, argument_or_subtransformations|
13
+ image.mogrify do |command|
14
+ if name.to_s == "combine_options"
15
+ argument_or_subtransformations.each do |subtransformation_name, subtransformation_argument|
16
+ pass_transform_argument(command, subtransformation_name, subtransformation_argument)
17
+ end
18
+ else
19
+ pass_transform_argument(command, name, argument_or_subtransformations)
20
+ end
21
+ end
22
+ end
23
+
24
+ image.format(format) if format
25
+
26
+ image.tempfile.tap(&:open)
27
+ end
28
+
29
+ def pass_transform_argument(command, method, argument)
30
+ if argument == true
31
+ command.public_send(method)
32
+ elsif argument.present?
33
+ command.public_send(method, argument)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveStorage
4
+ module Transformers
5
+ # A Transformer applies a set of transformations to an image.
6
+ #
7
+ # The following concrete subclasses are included in Active Storage:
8
+ #
9
+ # * ActiveStorage::Transformers::ImageProcessingTransformer:
10
+ # backed by ImageProcessing, a common interface for MiniMagick and ruby-vips
11
+ #
12
+ # * ActiveStorage::Transformers::MiniMagickTransformer:
13
+ # backed by MiniMagick, a wrapper around the ImageMagick CLI
14
+ class Transformer
15
+ attr_reader :transformations
16
+
17
+ def initialize(transformations)
18
+ @transformations = transformations
19
+ end
20
+
21
+ # Applies the transformations to the source image in +file+, producing a target image in the
22
+ # specified +format+. Yields an open Tempfile containing the target image. Closes and unlinks
23
+ # the output tempfile after yielding to the given block. Returns the result of the block.
24
+ def transform(file, format:)
25
+ output = process(file, format: format)
26
+
27
+ begin
28
+ yield output
29
+ ensure
30
+ output.close!
31
+ end
32
+ end
33
+
34
+ private
35
+ # Returns an open Tempfile containing a transformed image in the given +format+.
36
+ # All subclasses implement this method.
37
+ def process(file, format:) #:doc:
38
+ raise NotImplementedError
39
+ end
40
+ end
41
+ end
42
+ end
@@ -12,4 +12,11 @@ namespace :active_storage do
12
12
  Rake::Task["app:active_storage:install:migrations"].invoke
13
13
  end
14
14
  end
15
+
16
+ # desc "Copy over the migrations needed to the application upgrading"
17
+ task update: :environment do
18
+ ENV["MIGRATIONS_PATH"] = "db/update_migrate"
19
+
20
+ Rake::Task["active_storage:install"].invoke
21
+ end
15
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activestorage
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.6
4
+ version: 6.0.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-05 00:00:00.000000000 Z
11
+ date: 2019-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -16,42 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 5.2.6
19
+ version: 6.0.0.beta1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 5.2.6
26
+ version: 6.0.0.beta1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 5.2.6
33
+ version: 6.0.0.beta1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 5.2.6
40
+ version: 6.0.0.beta1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: marcel
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 1.0.0
47
+ version: 0.3.1
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 1.0.0
54
+ version: 0.3.1
55
55
  description: Attach cloud and local files in Rails applications.
56
56
  email: david@loudthinking.com
57
57
  executables: []
@@ -68,6 +68,7 @@ files:
68
68
  - app/controllers/active_storage/disk_controller.rb
69
69
  - app/controllers/active_storage/representations_controller.rb
70
70
  - app/controllers/concerns/active_storage/set_blob.rb
71
+ - app/controllers/concerns/active_storage/set_current.rb
71
72
  - app/javascript/activestorage/blob_record.js
72
73
  - app/javascript/activestorage/blob_upload.js
73
74
  - app/javascript/activestorage/direct_upload.js
@@ -87,21 +88,28 @@ files:
87
88
  - app/models/active_storage/blob/representable.rb
88
89
  - app/models/active_storage/current.rb
89
90
  - app/models/active_storage/filename.rb
90
- - app/models/active_storage/filename/parameters.rb
91
91
  - app/models/active_storage/preview.rb
92
92
  - app/models/active_storage/variant.rb
93
93
  - app/models/active_storage/variation.rb
94
94
  - config/routes.rb
95
95
  - db/migrate/20170806125915_create_active_storage_tables.rb
96
+ - db/update_migrate/20180723000244_add_foreign_key_constraint_to_active_storage_attachments_for_blob_id.rb
96
97
  - lib/active_storage.rb
97
98
  - lib/active_storage/analyzer.rb
98
99
  - lib/active_storage/analyzer/image_analyzer.rb
99
100
  - lib/active_storage/analyzer/null_analyzer.rb
100
101
  - lib/active_storage/analyzer/video_analyzer.rb
101
102
  - lib/active_storage/attached.rb
102
- - lib/active_storage/attached/macros.rb
103
+ - lib/active_storage/attached/changes.rb
104
+ - lib/active_storage/attached/changes/create_many.rb
105
+ - lib/active_storage/attached/changes/create_one.rb
106
+ - lib/active_storage/attached/changes/create_one_of_many.rb
107
+ - lib/active_storage/attached/changes/delete_many.rb
108
+ - lib/active_storage/attached/changes/delete_one.rb
103
109
  - lib/active_storage/attached/many.rb
110
+ - lib/active_storage/attached/model.rb
104
111
  - lib/active_storage/attached/one.rb
112
+ - lib/active_storage/downloader.rb
105
113
  - lib/active_storage/downloading.rb
106
114
  - lib/active_storage/engine.rb
107
115
  - lib/active_storage/errors.rb
@@ -111,6 +119,7 @@ files:
111
119
  - lib/active_storage/previewer/mupdf_previewer.rb
112
120
  - lib/active_storage/previewer/poppler_pdf_previewer.rb
113
121
  - lib/active_storage/previewer/video_previewer.rb
122
+ - lib/active_storage/reflection.rb
114
123
  - lib/active_storage/service.rb
115
124
  - lib/active_storage/service/azure_storage_service.rb
116
125
  - lib/active_storage/service/configurator.rb
@@ -118,14 +127,17 @@ files:
118
127
  - lib/active_storage/service/gcs_service.rb
119
128
  - lib/active_storage/service/mirror_service.rb
120
129
  - lib/active_storage/service/s3_service.rb
130
+ - lib/active_storage/transformers/image_processing_transformer.rb
131
+ - lib/active_storage/transformers/mini_magick_transformer.rb
132
+ - lib/active_storage/transformers/transformer.rb
121
133
  - lib/active_storage/version.rb
122
134
  - lib/tasks/activestorage.rake
123
135
  homepage: http://rubyonrails.org
124
136
  licenses:
125
137
  - MIT
126
138
  metadata:
127
- source_code_uri: https://github.com/rails/rails/tree/v5.2.6/activestorage
128
- changelog_uri: https://github.com/rails/rails/blob/v5.2.6/activestorage/CHANGELOG.md
139
+ source_code_uri: https://github.com/rails/rails/tree/v6.0.0.beta1/activestorage
140
+ changelog_uri: https://github.com/rails/rails/blob/v6.0.0.beta1/activestorage/CHANGELOG.md
129
141
  post_install_message:
130
142
  rdoc_options: []
131
143
  require_paths:
@@ -134,14 +146,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
134
146
  requirements:
135
147
  - - ">="
136
148
  - !ruby/object:Gem::Version
137
- version: 2.2.2
149
+ version: 2.5.0
138
150
  required_rubygems_version: !ruby/object:Gem::Requirement
139
151
  requirements:
140
- - - ">="
152
+ - - ">"
141
153
  - !ruby/object:Gem::Version
142
- version: '0'
154
+ version: 1.3.1
143
155
  requirements: []
144
- rubygems_version: 3.1.6
156
+ rubygems_version: 3.0.1
145
157
  signing_key:
146
158
  specification_version: 4
147
159
  summary: Local and cloud file storage framework.
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class ActiveStorage::Filename::Parameters #:nodoc:
4
- attr_reader :filename
5
-
6
- def initialize(filename)
7
- @filename = filename
8
- end
9
-
10
- def combined
11
- "#{ascii}; #{utf8}"
12
- end
13
-
14
- TRADITIONAL_ESCAPED_CHAR = /[^ A-Za-z0-9!#$+.^_`|~-]/
15
-
16
- def ascii
17
- 'filename="' + percent_escape(I18n.transliterate(filename.sanitized), TRADITIONAL_ESCAPED_CHAR) + '"'
18
- end
19
-
20
- RFC_5987_ESCAPED_CHAR = /[^A-Za-z0-9!#$&+.^_`|~-]/
21
-
22
- def utf8
23
- "filename*=UTF-8''" + percent_escape(filename.sanitized, RFC_5987_ESCAPED_CHAR)
24
- end
25
-
26
- def to_s
27
- combined
28
- end
29
-
30
- private
31
- def percent_escape(string, pattern)
32
- string.gsub(pattern) do |char|
33
- char.bytes.map { |byte| "%%%02X" % byte }.join
34
- end
35
- end
36
- end
@@ -1,110 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveStorage
4
- # Provides the class-level DSL for declaring that an Active Record model has attached blobs.
5
- module Attached::Macros
6
- # Specifies the relation between a single attachment and the model.
7
- #
8
- # class User < ActiveRecord::Base
9
- # has_one_attached :avatar
10
- # end
11
- #
12
- # There is no column defined on the model side, Active Storage takes
13
- # care of the mapping between your records and the attachment.
14
- #
15
- # To avoid N+1 queries, you can include the attached blobs in your query like so:
16
- #
17
- # User.with_attached_avatar
18
- #
19
- # Under the covers, this relationship is implemented as a +has_one+ association to a
20
- # ActiveStorage::Attachment record and a +has_one-through+ association to a
21
- # ActiveStorage::Blob record. These associations are available as +avatar_attachment+
22
- # and +avatar_blob+. But you shouldn't need to work with these associations directly in
23
- # most circumstances.
24
- #
25
- # The system has been designed to having you go through the ActiveStorage::Attached::One
26
- # proxy that provides the dynamic proxy to the associations and factory methods, like +attach+.
27
- #
28
- # If the +:dependent+ option isn't set, the attachment will be purged
29
- # (i.e. destroyed) whenever the record is destroyed.
30
- def has_one_attached(name, dependent: :purge_later)
31
- class_eval <<-CODE, __FILE__, __LINE__ + 1
32
- def #{name}
33
- @active_storage_attached_#{name} ||= ActiveStorage::Attached::One.new("#{name}", self, dependent: #{dependent == :purge_later ? ":purge_later" : "false"})
34
- end
35
-
36
- def #{name}=(attachable)
37
- #{name}.attach(attachable)
38
- end
39
- CODE
40
-
41
- has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record, inverse_of: :record, dependent: false
42
- has_one :"#{name}_blob", through: :"#{name}_attachment", class_name: "ActiveStorage::Blob", source: :blob
43
-
44
- scope :"with_attached_#{name}", -> { includes("#{name}_attachment": :blob) }
45
-
46
- if dependent == :purge_later
47
- after_destroy_commit { public_send(name).purge_later }
48
- else
49
- before_destroy { public_send(name).detach }
50
- end
51
- end
52
-
53
- # Specifies the relation between multiple attachments and the model.
54
- #
55
- # class Gallery < ActiveRecord::Base
56
- # has_many_attached :photos
57
- # end
58
- #
59
- # There are no columns defined on the model side, Active Storage takes
60
- # care of the mapping between your records and the attachments.
61
- #
62
- # To avoid N+1 queries, you can include the attached blobs in your query like so:
63
- #
64
- # Gallery.where(user: Current.user).with_attached_photos
65
- #
66
- # Under the covers, this relationship is implemented as a +has_many+ association to a
67
- # ActiveStorage::Attachment record and a +has_many-through+ association to a
68
- # ActiveStorage::Blob record. These associations are available as +photos_attachments+
69
- # and +photos_blobs+. But you shouldn't need to work with these associations directly in
70
- # most circumstances.
71
- #
72
- # The system has been designed to having you go through the ActiveStorage::Attached::Many
73
- # proxy that provides the dynamic proxy to the associations and factory methods, like +#attach+.
74
- #
75
- # If the +:dependent+ option isn't set, all the attachments will be purged
76
- # (i.e. destroyed) whenever the record is destroyed.
77
- def has_many_attached(name, dependent: :purge_later)
78
- class_eval <<-CODE, __FILE__, __LINE__ + 1
79
- def #{name}
80
- @active_storage_attached_#{name} ||= ActiveStorage::Attached::Many.new("#{name}", self, dependent: #{dependent == :purge_later ? ":purge_later" : "false"})
81
- end
82
-
83
- def #{name}=(attachables)
84
- #{name}.attach(attachables)
85
- end
86
- CODE
87
-
88
- has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment", inverse_of: :record, dependent: false do
89
- def purge
90
- each(&:purge)
91
- reset
92
- end
93
-
94
- def purge_later
95
- each(&:purge_later)
96
- reset
97
- end
98
- end
99
- has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob
100
-
101
- scope :"with_attached_#{name}", -> { includes("#{name}_attachments": :blob) }
102
-
103
- if dependent == :purge_later
104
- after_destroy_commit { public_send(name).purge_later }
105
- else
106
- before_destroy { public_send(name).detach }
107
- end
108
- end
109
- end
110
- end