-
Notifications
You must be signed in to change notification settings - Fork 10
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
centralize abstraction for thumbnail generation #1681
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,8 @@ | ||
<% begin %> | ||
<%# NOTE: This is superficially similar to the logic in DraftItem#thumbnail, but this file is run against | ||
every file attached to an item via a JS callback rendering files/update_files_list.js.erb calling _files_list.html.erb | ||
whereas DraftItem#thumbnail specifically only deals with rendering the designated file for representing it as a | ||
thumbnail and is used on various other pages, like profiles, which use the application/_thumbnail partial.%> | ||
<% thumbnail = rails_representation_path(file.variant(resize: '100x100', auto_orient: true).processed) %> | ||
<%= safe_thumbnail_tag(thumbnail, alt: '', title: file.filename, size: '100x100') %> | ||
<% rescue ActiveStorage::InvariableError %> | ||
<% begin %> | ||
<% thumbnail = rails_representation_path(file.preview(resize: '100x100', auto_orient: true).processed) %> | ||
<%= safe_thumbnail_tag(thumbnail, alt: '', title: file.filename, size: '100x100') %> | ||
<% rescue ActiveStorage::UnpreviewableError %> | ||
<div class="text-muted text-center img-thumbnail p-3"> | ||
<%= icon('far', file_icon(file.content_type), class: 'fa-5x') %> | ||
<span class="sr-only"><%= file.filename %></span> | ||
</div> | ||
<% end %> | ||
<% if thumbnail_path(file).present? %> | ||
<%= safe_thumbnail_tag(thumbnail_path(file), alt: '', title: file.filename, size: '100x100') %> | ||
<% else %> | ||
<div class="text-muted text-center img-thumbnail p-3"> | ||
<%= icon('far', file_icon(file.content_type), class: 'fa-5x') %> | ||
<span class="sr-only"><%= file.filename %></span> | ||
</div> | ||
<% end %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,8 @@ | ||
<% begin %> | ||
<%# NOTE: This is superficially similar to the logic in DraftItem#thumbnail, but this file is run against | ||
every file attached to an item via a JS callback rendering files/update_files_list.js.erb calling _files_list.html.erb | ||
whereas DraftItem#thumbnail specifically only deals with rendering the designated file for representing it as a | ||
thumbnail and is used on various other pages, like profiles, which use the application/_thumbnail partial.%> | ||
<% thumbnail = rails_representation_path(file.variant(resize: '100x100', auto_orient: true).processed) %> | ||
<%= safe_thumbnail_tag(thumbnail, alt: '', title: file.filename, size: '100x100') %> | ||
<% rescue ActiveStorage::InvariableError %> | ||
<% begin %> | ||
<% thumbnail = rails_representation_path(file.preview(resize: '100x100', auto_orient: true).processed) %> | ||
<%= safe_thumbnail_tag(thumbnail, alt: '', title: file.filename, size: '100x100') %> | ||
<% rescue ActiveStorage::UnpreviewableError %> | ||
<div class="text-muted text-center img-thumbnail p-3"> | ||
<%= icon('far', file_icon(file.content_type), class: 'fa-5x') %> | ||
<span class="sr-only"><%= file.filename %></span> | ||
</div> | ||
<% end %> | ||
<% if thumbnail_path(file).present? %> | ||
<%= safe_thumbnail_tag(thumbnail_path(file), alt: '', title: file.filename, size: '100x100') %> | ||
<% else %> | ||
<div class="text-muted text-center img-thumbnail p-3"> | ||
<%= icon('far', file_icon(file.content_type), class: 'fa-5x') %> | ||
<span class="sr-only"><%= file.filename %></span> | ||
</div> | ||
<% end %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
logo: | ||
name: 'files' | ||
record_type: 'Item' | ||
record_id: <%= ActiveRecord::FixtureSet.identify(:fancy) %> | ||
blob_id: <%= ActiveRecord::FixtureSet.identify(:jpeg) %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
jpeg: | ||
key: '12345' | ||
filename: 'image-sample.jpeg' | ||
content_type: 'image/jpeg' | ||
byte_size: 12086 | ||
checksum: "GxpIjJsC4KnRoBKNjWnkJA==" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ def base_url | |
|
||
# page_title | ||
|
||
# page_title | ||
test 'should return the page title when given one' do | ||
assert_equal t('site_name'), page_title(t('site_name')) | ||
end | ||
|
@@ -93,9 +92,94 @@ def base_url | |
@community.logo.attach io: File.open(file_fixture('image-sample.jpeg')), | ||
filename: 'image-sample.jpeg', content_type: 'image/jpeg' | ||
|
||
assert_equal page_image_url, request.base_url + @community.thumbnail_path | ||
assert_equal page_image_url, request.base_url + thumbnail_path(@community.thumbnail_file) | ||
end | ||
|
||
# thumbnail_path | ||
|
||
test 'thumbnail_path should return preview for pdf (Invariable but Previewable)' do | ||
community = communities(:books) | ||
collection = collections(:fantasy_books) | ||
@item = Item.new.tap do |uo| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could probably clean up some of this by using an Item fixture. E.g: @item = items(:fancy)
File.open(file_fixture('pdf-sample.pdf'), 'r') do |file|
@item.add_and_ingest_files([file])
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I was hoping that would work too. Maybe I'm using it wrong
gives me
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, my guess is you might have to Take a peak at downloads controller test. Heres the relevant snippet: item = items(:fancy)
File.open(file_fixture('text-sample.txt'), 'r') do |file|
item.add_and_ingest_files([file])
end
@file = item.reload.files.first There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was exactly what I was missing. Thanks! |
||
uo.title = 'Fantastic item' | ||
uo.owner_id = users(:admin).id | ||
uo.creators = ['Joe Blow', 'Smokey Chantilly-Tiffany', 'Céline Marie Claudette Dion'] | ||
uo.visibility = JupiterCore::VISIBILITY_PUBLIC | ||
uo.created = '1999-09-09' | ||
uo.languages = [CONTROLLED_VOCABULARIES[:language].english] | ||
uo.license = CONTROLLED_VOCABULARIES[:license].attribution_4_0_international | ||
uo.item_type = CONTROLLED_VOCABULARIES[:item_type].article | ||
uo.publication_status = [CONTROLLED_VOCABULARIES[:publication_status].draft, | ||
CONTROLLED_VOCABULARIES[:publication_status].submitted] | ||
uo.subject = ['Items'] | ||
uo.add_to_path(community.id, collection.id) | ||
uo.save! | ||
end | ||
Sidekiq::Testing.inline! do | ||
# Attach a file to the item so that it provide preview | ||
File.open(file_fixture('pdf-sample.pdf'), 'r') do |file| | ||
@item.add_and_ingest_files([file]) | ||
end | ||
end | ||
|
||
logo = @item.files.first | ||
expected = Rails.application.routes.url_helpers.rails_representation_path( | ||
logo.preview(resize: '100x100', auto_orient: true).processed | ||
) | ||
assert_equal expected, thumbnail_path(logo) | ||
end | ||
|
||
test 'thumbnail_path should return preview for image (Variable)' do | ||
community = communities(:books) | ||
collection = collections(:fantasy_books) | ||
@item = Item.new.tap do |uo| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, just use an item fixture and add an attachment to it (dont even think you need the @item = items(:fancy)
File.open(file_fixture('text-sample.txt'), 'r') do |file|
@item.add_and_ingest_files([file])
end |
||
uo.title = 'Fantastic item' | ||
uo.owner_id = users(:admin).id | ||
uo.creators = ['Joe Blow', 'Smokey Chantilly-Tiffany', 'Céline Marie Claudette Dion'] | ||
uo.visibility = JupiterCore::VISIBILITY_PUBLIC | ||
uo.created = '1999-09-09' | ||
uo.languages = [CONTROLLED_VOCABULARIES[:language].english] | ||
uo.license = CONTROLLED_VOCABULARIES[:license].attribution_4_0_international | ||
uo.item_type = CONTROLLED_VOCABULARIES[:item_type].article | ||
uo.publication_status = [CONTROLLED_VOCABULARIES[:publication_status].draft, | ||
CONTROLLED_VOCABULARIES[:publication_status].submitted] | ||
uo.subject = ['Items'] | ||
uo.add_to_path(community.id, collection.id) | ||
uo.save! | ||
end | ||
Sidekiq::Testing.inline! do | ||
# Attach a file to the item so that it can provide variant | ||
File.open(file_fixture('image-sample.jpeg'), 'r') do |file| | ||
@item.add_and_ingest_files([file]) | ||
end | ||
end | ||
|
||
logo = @item.files.first | ||
expected = Rails.application.routes.url_helpers.rails_representation_path( | ||
logo.variant(resize: '100x100', auto_orient: true).processed | ||
) | ||
assert_equal expected, thumbnail_path(logo) | ||
end | ||
|
||
test 'thumbnail_path should provide nil if no thumbnail is possible (StandardError on variable)' do | ||
logo = active_storage_attachments(:logo) | ||
logo.define_singleton_method(:variant) { |_| throw StandardError } | ||
|
||
assert_nil thumbnail_path(logo) | ||
# TODO: assert that the logger.warn was written | ||
end | ||
|
||
test 'thumbnail_path should return nil if both the variant and preview fail' do | ||
logo = active_storage_attachments(:logo) | ||
logo.define_singleton_method(:variant) { |_| throw ActiveStorage::InvariableError } | ||
logo.define_singleton_method(:preview) { |_| throw StandardError } | ||
|
||
assert_nil thumbnail_path(logo) | ||
# TODO: assert that the logger.warn was written | ||
end | ||
|
||
# canonical_href | ||
|
||
test 'canonical_href is returning the correct canoncial url' do | ||
assert_equal Jupiter::PRODUCTION_URL, canonical_href(nil) | ||
assert_equal Jupiter::PRODUCTION_URL, canonical_href('/') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is installing poppler easy on CI/Dev machines? If not then maybe we should keep it as an "optional" dependency (if not installed we just fallback to file icon image which already happens as it returns nil)? Maybe this test only runs if Poppler dependency is defined 🤔 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's pretty easy in Ubuntu
sudo apt-get install poppler-utils
I'll see if I can get this to work like Clamby.jupiter/app/models/concerns/draft_properties.rb
Line 153 in be07bda
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jupiter/.github/workflows/push.yml
Line 6 in b667a36
https://github.com/actions/virtual-environments/blob/master/images/linux/Ubuntu2004-README.md
Has ImageMagick installed but not the PDF dependencies.