Commit Graph

701 Commits

Author SHA1 Message Date
Sean Doyle
e75b0094d0 ActiveStorage: Pass useCapture to UJS listener
Closes [@hotwired/turbo#243][].

Set the `useCapture` parameter of [addEventListener][] to `true` so that
cancelling the event occurs early enough in the process so that the
resulting upload is cancelled.

[@hotwired/turbo#243]: https://github.com/hotwired/turbo/issues/243#issuecomment-818792129
[addEventListener]: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#parameters

Co-authored-by: Javan Makhmali <javan@basecamp.com>
2021-04-13 11:56:01 -04:00
George Claghorn
b80a2bdeb9
Active Storage: upgrade to mini_mime 1.1.0
Fix validating uppercase variant formats. Closes #41796.
2021-04-04 22:44:02 -04:00
George Claghorn
e3075c17ff Add frozen_string_literal pragma 2021-03-27 08:07:47 -04:00
George Claghorn
657b97e223 Active Storage representations: respond with 404 given invalid variation key 2021-03-27 08:03:27 -04:00
George Claghorn
526d630793 Upgrade to Marcel 1.0.0 2021-03-26 14:21:28 -04:00
George Claghorn
62ac25a364 Replace mimemagic with mini_mime 2021-03-26 14:20:40 -04:00
Henrik Nyh
515108ee03 'lookup' -> 'look up' in dummy webpacker.ymls
Corresponding to this fix: https://github.com/rails/webpacker/pull/2961
2021-03-21 12:36:56 +00:00
eileencodes
226007daa1
Handle false in relation strict loading checks
Fixes: #41453
Closes: #41461

Previously when a model had strict loading set to true and then had a
relation set strict_loading to false the false wasn't considered when
deciding whether to raise/warn about strict loading.

Code example:

```ruby
class Dog < ActiveRecord::Base
  self.strict_loading_by_default = true

  has_many :treats, strict_loading: false
end
```

In the example, `dog.treats` would still raise even though
`strict_loading` was set to false. This is a bug effecting more than
Active Storage which is why I made this PR superceeding #41461. We need
to fix this for all applications since the behavior is a little
surprising. I took the test from ##41461 and the code suggestion from #41453
with some additions.

Co-authored-by: Radamés Roriz <radamesroriz@gmail.com>
2021-03-16 16:26:59 -04:00
Alex Ghiculescu
bd33ddaf71 Active Storage: Blob creation shouldn't crash if no service selected
https://github.com/rails/rails/issues/41653 noted an issue where if there's service configured (`config.active_storage.service` is commented out), Blob creation via direct upload crashes:

```
Started POST "/rails/active_storage/direct_uploads" for ::1 at 2021-03-09 13:02:57 -0800
Processing by ActiveStorage::DirectUploadsController#create as JSON
  Parameters: {"blob"=>{"filename"=>"banana.jpg", "content_type"=>"image/jpeg", "byte_size"=>577085, "checksum"=>"W/vo/JqBNmJHMCaL+PRlBQ=="}, "direct_upload"=>{"blob"=>{"filename"=>"banana.jpg", "content_type"=>"image/jpeg", "byte_size"=>577085, "checksum"=>"W/vo/JqBNmJHMCaL+PRlBQ=="}}}
Completed 500 Internal Server Error in 12ms (ActiveRecord: 3.3ms | Allocations: 5864)

NoMethodError (undefined method `name' for nil:NilClass):

activestorage (6.1.3) app/models/active_storage/blob.rb:52:in `block in <class:Blob>'
activesupport (6.1.3) lib/active_support/callbacks.rb:427:in `instance_exec'
```

This PR fixes that crash. Blob creation will still fail, but with a more informative error about a `service_name` being required.
2021-03-12 15:12:11 -05:00
Christian Schmidt
6cd184ff0f
Use image/jpeg instead of non-standard image/jpg 2021-03-05 13:14:11 -05:00
Aditya Pandit
12613accfd
Removed test for deprecated ASt combine_options transformation 2021-03-03 11:48:31 -05:00
Alex Robbin
d6197d4c86 Active Storage: raise PreviewError when a preview cannot be generated
If a preview cannot be generated, the IO stream that is captured is empty, resulting in a 0-byte preview file being generated and stored in the Active Storage service.

We came across this because Poppler was failing to generate previews of some PDFs, resulting in 0-byte files. Resizing those "previews" then resulted in a MiniMagick error. The MiniMagick error feels like the right end result if it's attempted on a 0-byte file, what doesn't feel right is `Previewer` proceeding normally if the child process that attempted to capture a preview exited unsuccessfully.

Now, if the previewer child process exits with a non-0 status code, we raise an exception.
2021-02-26 14:06:52 -05:00
David Heinemeier Hansson
ab8e3d22cb
Extract ActiveStorage::Streaming so your own controllers can use it (#41440)
Extract ActiveStorage::Streaming so your own controller can use it
2021-02-19 15:40:56 +01:00
Rafael Mendonça França
1b455e2e9d
Rails 6.2 is now Rails 7.0
We have big plans for the next version of Rails and that
require big versions.
2021-02-04 16:47:16 +00:00
Rafael Mendonça França
6487836af8
Rails 7 requires Ruby 2.7 and prefer Ruby 3+
The code cleanup is comming in later commits but this
already remove support to Ruby < 2.7.
2021-02-04 16:34:53 +00:00
Kasper Timm Hansen
a1408e72f7
Merge branch 'main' into active_storage-named-variants 2021-01-29 03:25:37 +01:00
Ali Ismayilov
a205f5110d
Respect default serving strategy in rails_{blob,representation}_{url,path} 2021-01-28 18:52:00 -05:00
Sean Doyle
48e601c7a1 Move ActiveStorage fixture hooks to on_load
In a test environment, rely on the loading of
`:active_support_test_case`.

Introduce the `:active_record_fixture_set` hook for the Active Storage
engine to listen for during the load process in a development
environment (like when running `db:fixtures:load`).

Since this commit moves the task-aware path resolution out of the block
that provided local variables, it recreates part of the fixture
directory path resolution logic.
2021-01-25 00:36:37 -05:00
Sean Doyle
948e9d74e3
Fix Flaky ActiveStorage test (#41225)
Fixes a flaky Active Storage test introduced by [rails/rails#41065][],
and improves the documentation.

It seems that the test is covering the backwards compatibility of an
older interface for retrieving records through
`ActiveStorage::Record#find_signed!`. The test itself would pass
unpredictably. To isolate the failure and reproduce it consistently, a
see value was found after some trial and error:

```
SEED=59729 bin/test test/fixture_set_test.rb test/models/attachment_test.rb
```

This _used_ to pass consistently because [rails/rails][#41065]
introduced a call to `fixtures :all`, which introduces more variation in
the database's ID generation sequence. Without that line, `id` values
start at `1`, so the fact that calls to
`ActiveStorage::Attached::One#id` and `ActiveStorage::Blob#id` **both
return `1`** is purely coincidence.

The proposed resolution changes the test slightly. Prior to this change,
the identifier used during retrieval and verification fetched from
`@user.avatar.id`, where `@user.avatar` is an instance of
`ActiveStorage::Attached::One`. The verifier/retriever combination in
that test expected a signed identifier for an `ActiveStorage::Blob`
instance. The change involved retrieving an instance through
`@user.avatar.blob`.

To better emphasize how global the `fixtures :all` declaration is, move
it from the `test/fixture_set_test.rb` file to the `test/test_helper.rb`
file.

[rails/rails#41065]: https://github.com/rails/rails/pull/41065
2021-01-24 18:29:11 +01:00
Sean Doyle
c0f33b923b
Improve Fixture support for Active Storage (#41065)
* Improve Fixture support for Active Storage

Inspired by [76b33aa][], this commit extends the Active Storage
documentation to elaborate on how to declare fixtures.

In support of that, also introduce the `ActiveStorage::FixtureSet.blob`
method for injecting in-line [ActiveStorage::Blob][] attributes directly
into fixture YAML.

[76b33aa]: 76b33aa3d1
[ActiveStorage::Blob]: https://edgeapi.rubyonrails.org/classes/ActiveStorage/Blob.html

* Extra CR for style

* Two-space indention

* Explaining variable didn't explain, inline for style

Co-authored-by: David Heinemeier Hansson <david@loudthinking.com>
2021-01-24 11:10:30 +01:00
Daniel Colson
66b861f24c
Restore ActiveStorage::Blob#find_signed
Rails 6.0 had a [public `find_signed` method][docs], but we changed it
to `find_signed!` in 31148cd6be.

This commit adds back `find_signed` alongside `find_signed!` to match
the corresponding [Active Record methods][].

[docs]: https://api.rubyonrails.org/v6.0.0/classes/ActiveStorage/Blob.html#method-c-find_signed
[Active Record methods]: https://github.com/rails/rails/blob/main/activerecord/lib/active_record/signed_id.rb#L42-L66
2021-01-23 23:14:48 -05:00
Rafael Mendonça França
077c66d5d6
Rename master to main in all code references 2021-01-19 20:46:33 +00:00
Ryuta Kamizono
2b0b5a75c0 Bump license years to 2021 [ci skip] 2021-01-01 12:21:20 +09:00
Matt Muller
4b44d4c0e9
Fix S3 multipart uploads when threshold is larger than file 2020-12-11 16:26:40 -05:00
Rafael França
45d1efab51
Merge pull request #40766 from rafaelfranca/rm-queue-defaults
Change default queue name of all the internal jobs to be the job adapter's default
2020-12-08 22:47:47 +00:00
Daniel Colson
8ef5bd9ced
Allow custom purpose for ActiveStorage signed IDs
This commit allows passing a custom `purpose:` keyword argument to both
`find_signed!` and `signed_id`. This matches the signature of the
`super` methods in ActiveRecord, and it is already possible with the
non-bang [`find_signed`][find_signed] method, since that one doesn't
have an override in ActiveStorage (although perhaps it should, to set
the default :blob_id purpose)

This is useful in cases where you want to further isolate different
types of blobs, for example if they have different levels of
authorization.

[find_signed]: be11d1b6e8/activerecord/lib/active_record/signed_id.rb (L42-L48)
2020-12-03 12:36:52 -05:00
Rafael Mendonça França
59f7f5889e
Start Rails 6.2 development 🎉 2020-12-03 01:35:29 +00:00
Alex Ghiculescu
76e432341c Always recommend config.asset_host in examples 2020-11-30 16:33:51 -06:00
Tahsin Hasan
bb148d822c
Permit hash on direct upload in active storage (#40662) 2020-11-28 10:33:44 +01:00
Jonathan Hefner
3397924e66
Use FFmpeg scene detection for video previews
Generating a video preview by capturing only the first frame of a video
is problematic for videos that begin with a fade in from black.  By
using keyframe and scene detection that is built in to FFmpeg, we can
generate a more representative preview.
2020-11-17 17:25:01 -05:00
David Angulo
1db02b3896 Add strict loading for active storage 2020-11-16 22:09:15 +08:00
Rafael Mendonça França
8389f9902c
Preparing for 6.1.0.rc1 release 2020-11-02 21:12:47 +00:00
Eugene Kenny
b7eaaf73fb Update comment about reloading in development.rb [ci skip]
This comment predates the existence of `reload_classes_only_on_change`.
2020-11-02 19:36:26 +00:00
Rafael Mendonça França
62bda903be
Remove deprecated support to pass :combine_options operations to ActiveStorage::Transformers::ImageProcessing 2020-10-30 00:25:48 +00:00
Rafael Mendonça França
bad3d0f6a3
Remove warning of undefined instance variable 2020-10-30 00:25:47 +00:00
Rafael Mendonça França
74476f0f90
Remove deprecated ActiveStorage::Transformers::MiniMagickTransformer
Now ActiveStorage only support mini_magick transformation through the
image_processing gem.
2020-10-30 00:25:47 +00:00
Rafael Mendonça França
bc28e37de2
Remove deprecated config.active_storage.queue 2020-10-30 00:25:45 +00:00
Rafael Mendonça França
92a8e1d7c5
Remove deprecated ActiveStorage::Downloading 2020-10-30 00:25:44 +00:00
Rafael França
9492339979
Merge pull request #38443 from jonathanhefner/plugin-fix-stack-traces
Fix stack traces when testing plugins
2020-10-08 19:06:40 -04:00
Eugene Kenny
e44b3419d4 Prefix keys in Active Storage service test
Otherwise this test fails when run concurrently.

https://buildkite.com/rails/rails/builds/71999#77923632-f943-4fa0-b478-0af38001c22b/1032-1098
https://buildkite.com/rails/rails/builds/71999#01fdec79-dc1f-4a6a-b730-ac0df134a2e7/970-1062
2020-10-07 21:47:59 +01:00
Jonathan Hefner
33fdae0584 Fix backtraces for generated plugin tests
`Minitest.plugin_rails_init` sets `Minitest.backtrace_filter` to
`Rails.backtrace_cleaner` right before tests are run, overwriting the
value set in test_helper.rb.

`Rails.backtrace_cleaner` silences backtrace lines that do not start
with `Rails.root` followed by e.g. "lib/" or "test/".  Thus when
`Rails.root` is a subdirectory of the project directory -- for example,
when testing a plugin that has a dummy app -- all lines of the backtrace
are silenced.

This commit adds a fallback such that when all backtrace lines are
silenced, the original `Minitest.backtrace_filter` is used instead.

Additionally, this commit refactors and expands existing test coverage.
2020-10-07 15:40:56 -05:00
Akira Matsuda
d71291cb3c Module#include is a public method since Ruby 2.1 2020-10-06 21:19:24 +09:00
Jonathan Hefner
fd3fd618f4 Add gitattributes for vendored and generated files [ci-skip]
The `linguist-vendored` attribute excludes the specified file from the
project's language stats on GitHub.  The `linguist-generated` attribute
does the same, and also suppresses that file in diffs on GitHub.

See https://github.com/github/linguist for more information.
2020-10-04 16:44:37 -05:00
George Claghorn
a50d2f1993 Extract ActiveStorage::Record
Permit applications to hack in custom DB config for ASt models until ASt has first-class multi-DB support:

    ActiveSupport.on_load(:active_storage_record) do
      connects_to reading: :active_storage_replica, writing: :active_storage_primary
    end

rebase
2020-09-29 11:59:14 -04:00
Pietro Moro
1e2cd7a75e
Support per-environment ASt service config 2020-09-29 09:59:52 -04:00
David Genord II
f4aa54d487
Add requires for mimemagic
This dependency was recently added, but not the require. This looked like it worked because sometimes marcel does the require for us.
2020-09-25 15:00:04 -04:00
George Claghorn
b6d86add73 Remove unused require
Follows up on a185ff8.
2020-09-23 14:39:52 -04:00
George Claghorn
a185ff86c0
Set default variant format from blob content type 2020-09-22 13:03:53 -04:00
Vincent Robert
3803671a81 Generate a preview without print margins
When a PDF is used for both printing and displaying. It will most likely
contain a crop box in order to hide print margins when displaying the PDF.

Use Poppler's parameter to automatically use the crop box (visible box)
rather than the media box (printable box) in order to remove those margins
when drawing the PDF.

See https://manpages.debian.org/testing/poppler-utils/pdftoppm.1.en.html
2020-09-17 11:59:45 -04:00
Santiago Bartesaghi
4a78dcb326
Clarify ActiveStorage::Service#url docs [ci skip]
* Public services don’t respect `:disposition`, `:filename`, or `:content_type`.
* `:expires_in` is optional.
2020-09-12 12:47:33 -04:00