Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Active Storage for Picture and File attachments #2968

Draft
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

tvdeyen
Copy link
Member

@tvdeyen tvdeyen commented Jul 9, 2024

What is this pull request for?

Use ActiveStorage instead of Dragonfly for picture and file attachments.

Important changes

  • Cropping animated images (gif and webp) does not work with vips. It works with mini_magick though
  • Upfront processing of image variants after upload is not supported in Rails 7.0 (Need Rails 7.1 to re-implement support)
  • Passing additional params to the picture url is not supported anymore

TODO

  • Use ActiveStorage for Alchemy::Attachment as well
  • Remove Dragonfly dependency and related code
  • Remove our custom picture variants and accompany files
  • Make displaying animated GIFs work again
  • Write upgrade task to create ActiveStorage variants from our variants
  • Figure out how to configure alchemy active storage service for different envs
  • Fix image cropping with mini_magick (?? still a thing ??)

Checklist

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have added tests to cover this change

@tvdeyen tvdeyen changed the title Active storage Use Active Storage for Picture and File attachments Jul 10, 2024
@tvdeyen tvdeyen force-pushed the active-storage branch 2 times, most recently from 0b43fa0 to 2b7123c Compare July 10, 2024 20:13
Copy link

codecov bot commented Jul 10, 2024

Codecov Report

Attention: Patch coverage is 95.86777% with 10 lines in your changes missing coverage. Please review.

Project coverage is 96.40%. Comparing base (b4b2471) to head (38b4972).

Files with missing lines Patch % Lines
app/models/alchemy/picture.rb 88.88% 5 Missing ⚠️
app/models/alchemy/picture/url.rb 88.88% 2 Missing ⚠️
app/controllers/alchemy/attachments_controller.rb 92.85% 1 Missing ⚠️
app/models/alchemy/attachment.rb 97.14% 1 Missing ⚠️
app/models/alchemy/picture/preprocessor.rb 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2968      +/-   ##
==========================================
- Coverage   96.58%   96.40%   -0.19%     
==========================================
  Files         236      225      -11     
  Lines        6334     6261      -73     
==========================================
- Hits         6118     6036      -82     
- Misses        216      225       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tvdeyen tvdeyen self-assigned this Jul 22, 2024
@tvdeyen tvdeyen force-pushed the active-storage branch 3 times, most recently from b4a85f8 to 9bedb48 Compare August 10, 2024 09:45
@HatsuMora
Copy link

It would be great to support uploads on the Resources, to use it when Alchemy is being used as admin for other models.

@tvdeyen
Copy link
Member Author

tvdeyen commented Sep 11, 2024

This change is not related to allow attachments in Rails models. You can already do this with established Rails ways. There is nothing special about that in Alchemy. The model is yours. Just use it as normal Rails model.

lib/alchemy_cms.rb Outdated Show resolved Hide resolved
@tvdeyen tvdeyen force-pushed the active-storage branch 2 times, most recently from 59d4563 to 77a78ef Compare September 18, 2024 10:14
@tvdeyen tvdeyen force-pushed the active-storage branch 2 times, most recently from b4a03f9 to 9587e59 Compare October 4, 2024 14:40
@tvdeyen tvdeyen force-pushed the active-storage branch 2 times, most recently from 9b2e8f7 to fd3afe9 Compare December 3, 2024 19:20
Necessary for active storage image transforms
ActiveStorage uses the image_processing gem to process images. This uses a hash to describe the conversion instead of a string
Instead of using the Dragonfly Model validations we write our own. That way we can use another image attachment library more easily
The vips image_processing processor sharpens images by default. This disables this behavior.
Active storage already knows if a file is convertible
This avoids N+1 in the image library
Using activestorage methods to read the values.
Ideally we would cache these values, but active
storage asynchronously sets these values and provides
no callback we could use.
Now that we use ActiveStorage we can remove our custom picture variant handling
This method is the only one left after the PictureVariant and PictureThumb removal.
Active storage has the mime type stored not the extension.
ruby-vips and mini_magick need to be told to ignore
the page in order to resize animated gifs.
Before we where not telling active storage that we want
a certain format explicitely.
We need that for displaying animated PNGs properly.
Proxy downloads the file.
Only works in Rails 7.1
ActiveStorage expects an extension and not an mime type.
It is only configured by default in unreleased Rails 7.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants