From 8550dba86b7e63ff435d3a81252a503906f06388 Mon Sep 17 00:00:00 2001 From: Justin Craig-Kuhn Date: Sun, 3 Nov 2024 13:24:40 -0800 Subject: [PATCH] Convert Shrine files to ActiveStorage (#410) --- README.md | 2 +- app/controllers/downloads_controller.rb | 29 +- app/javascript/components/CoverArt.jsx | 6 +- app/javascript/components/Show.jsx | 3 +- app/javascript/components/Shows.jsx | 2 +- .../stylesheets/application.css.scss | 21 +- app/javascript/stylesheets/content.css.scss | 439 +++++++++++++++--- app/javascript/stylesheets/errors.css.scss | 2 +- app/javascript/stylesheets/layout.css.scss | 310 ------------- app/javascript/stylesheets/mobile.css.scss | 2 + app/javascript/stylesheets/player.css.scss | 15 +- app/models/application_record.rb | 8 + app/models/concerns/has_cover_art.rb | 117 ++--- app/models/track.rb | 25 +- .../cloudflare_cache_purge_service.rb | 40 -- app/services/id3_tag_service.rb | 22 +- app/services/mp3_duration_query.rb | 23 - app/services/show_importer/orchestrator.rb | 7 +- app/services/track_attachment_service.rb | 28 ++ app/services/waveform_image_service.rb | 29 +- config/storage.yml | 4 +- lib/tasks/shows.rake | 9 +- spec/factories/track.rb | 21 +- spec/models/playlist_spec.rb | 11 +- spec/models/track_spec.rb | 6 +- spec/services/id3_tag_service_spec.rb | 10 +- spec/services/mp3_duration_query_spec.rb | 34 -- spec/support/shrine_test_data.rb | 24 - 28 files changed, 616 insertions(+), 633 deletions(-) delete mode 100644 app/javascript/stylesheets/layout.css.scss delete mode 100644 app/services/cloudflare_cache_purge_service.rb delete mode 100644 app/services/mp3_duration_query.rb create mode 100644 app/services/track_attachment_service.rb delete mode 100644 spec/services/mp3_duration_query_spec.rb delete mode 100644 spec/support/shrine_test_data.rb diff --git a/README.md b/README.md index de4f249e6..efddd41b6 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Join the [Discord](https://discord.gg/KZWFsNN) to discuss content and developmen 2. Clone the repo to your local machine -4. Download the [Fixtures Pack](https://www.dropbox.com/scl/fi/gacrptl8htt331py9ysrh/PhishinDevFixtures.zip?rlkey=ys61k2qrer7exv2zej95b275l&st=xi1navpb&dl=0) and unzip it. This file contains a full database export (updated May 2024) minus users and API keys. It also includes MP3 audio and PNG waveform attachments for the last Baker's Dozen show, which should be browsable and playable via `localhost:3000/2017-08-06` once the local server is running. Additionally it includes MP3s/notes for 2018-12-28 for testing the `rails shows:import` task. +4. Download the [Fixtures Pack](https://www.dropbox.com/scl/fi/ezmwysxnj6z1kgpfy04nb/PhishinDevFixtures.zip?rlkey=qvpvezehcudnr0u7c54337gv0&st=c8nchmf2&dl=0) and unzip it. This file contains a full database export (updated May 2024) minus users and API keys. It also includes MP3 audio and PNG waveform attachments for the last Baker's Dozen show, which should be browsable and playable via `localhost:3000/2017-08-06` once the local server is running. Additionally it includes MP3s/notes for 2018-12-28 for testing the `rails shows:import` task. ```bash # Copy SQL dump into PG container and run it diff --git a/app/controllers/downloads_controller.rb b/app/controllers/downloads_controller.rb index 614d47a4e..959a7a372 100644 --- a/app/controllers/downloads_controller.rb +++ b/app/controllers/downloads_controller.rb @@ -1,7 +1,14 @@ class DownloadsController < ApplicationController def download_track - raise ActiveRecord::RecordNotFound if track&.audio_file.blank? - send_audio_file_as_attachment + raise ActiveRecord::RecordNotFound if track.blank? + + if track.mp3_audio.attached? + send_active_storage_audio_as_attachment + elsif track.audio_file.exists? + send_shrine_audio_as_attachment + else + head :not_found + end end def download_blob @@ -11,7 +18,19 @@ def download_blob private - def send_audio_file_as_attachment + def send_active_storage_audio_as_attachment + add_cache_header + send_file \ + ActiveStorage::Blob.service.send(:path_for, track.mp3_audio.blob.key), + type: "audio/mpeg", + disposition: "attachment", + filename: "Phish #{track.show.date} #{track.title}.mp3", + length: track.mp3_audio.blob.byte_size + rescue ActionController::MissingFile + head :not_found + end + + def send_shrine_audio_as_attachment add_cache_header send_file \ track.audio_file.to_io.path, @@ -36,9 +55,7 @@ def send_blob_file_inline end def track - @track ||= - Track.includes(show: :venue) - .find_by(id: params[:id]) + @track ||= Track.includes(show: :venue).find_by(id: params[:id]) end def blob diff --git a/app/javascript/components/CoverArt.jsx b/app/javascript/components/CoverArt.jsx index d236a6bfb..2fd87ead0 100644 --- a/app/javascript/components/CoverArt.jsx +++ b/app/javascript/components/CoverArt.jsx @@ -37,17 +37,17 @@ const CoverArt = ({ coverArtUrls, albumCoverUrl, openAppModal, size = "small", c setIsLoaded(true); }; - const selectedImage = selectedOption === "albumCover" ? albumCoverUrl : coverArtUrls?.[size]; + const selectedImage = selectedOption === "albumCover" ? albumCoverUrl : coverArtUrls?.[size]; return (
{selectedOption diff --git a/app/javascript/components/Show.jsx b/app/javascript/components/Show.jsx index c4731117c..ca1e6c0cc 100644 --- a/app/javascript/components/Show.jsx +++ b/app/javascript/components/Show.jsx @@ -84,7 +84,7 @@ const Show = ({ trackSlug }) => {