Skip to content

Commit

Permalink
Add blob extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jcraigk committed Nov 9, 2024
1 parent 45cfc74 commit 82925fc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/downloads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def track
end

def blob
@blob ||= ActiveStorage::Blob.find_by(key: params[:key])
@blob ||= ActiveStorage::Blob.find_by(key: params[:key].split(".").first)
end

def add_cache_header
Expand Down
4 changes: 2 additions & 2 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

def blob_url(attachment, variant: nil, placeholder: nil)
def blob_url(attachment, variant: nil, placeholder: nil, ext: nil)
if attachment.attached?
key = variant ? attachment.variant(variant).processed.key : attachment.blob.key
"#{App.content_base_url}/blob/#{key}"
"#{App.content_base_url}/blob/#{key}#{".#{ext}" if ext}"
elsif placeholder
"/placeholders/#{placeholder}"
end
Expand Down
16 changes: 9 additions & 7 deletions app/models/concerns/has_cover_art.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ module HasCoverArt

included do # rubocop:disable Metrics/BlockLength
def cover_art_urls
{
large: blob_url(cover_art, placeholder: "cover-art-large.jpg"),
medium: blob_url(cover_art, variant: :medium, placeholder: "cover-art-medium.jpg"),
small: blob_url(cover_art, variant: :small, placeholder: "cover-art-small.jpg")
}
%i[large medium small].index_with do |size|
blob_url \
cover_art,
**(size == :large ? {} : { variant: size }),
placeholder: "cover-art-#{size}.jpg",
ext: :jpg
end
end

def album_cover_url
blob_url(album_cover, placeholder: "cover-art-large.jpg")
blob_url(album_cover, placeholder: "cover-art-large.jpg", ext: :jpg)
end

def album_zip_url
blob_url(album_zip)
blob_url(album_zip, ext: :zip)
end

def cover_art_path
Expand Down
4 changes: 2 additions & 2 deletions app/models/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def generate_slug(force: false)
end

def mp3_url
blob_url(mp3_audio, placeholder: "audio.mp3")
blob_url(mp3_audio, placeholder: "audio.mp3", ext: :mp3)
end

def waveform_image_url
blob_url(png_waveform, placeholder: "waveform.png")
blob_url(png_waveform, placeholder: "waveform.png", ext: :png)
end

def urls
Expand Down

0 comments on commit 82925fc

Please sign in to comment.