Skip to content

Commit

Permalink
Allow routing assets through CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierke committed Nov 5, 2023
1 parent 1ebbe30 commit aff621e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
12 changes: 2 additions & 10 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,15 @@ def gravatar_url(size = 256)

def avatar_url(size = :medium)
url = if avatar_image.attached?
begin
avatar_image.variant(**AVATAR_IMAGE_VARIANTS[size]).url
rescue URI::InvalidURIError, Aws::Errors::MissingRegionError
nil
end
Rails.application.routes.url_helpers.cdn_blob_url(avatar_image.variant(**AVATAR_IMAGE_VARIANTS[size]))
end

url || gravatar_url(AVATAR_IMAGE_VARIANTS.dig(size, :resize_to_fill)&.first)
end

def header_image_url(size = :medium)
if header_image.attached?
begin
header_image.variant(**HEADER_IMAGE_VARIANTS[size]).url
rescue URI::InvalidURIError, Aws::Errors::MissingRegionError
nil
end
Rails.application.routes.url_helpers.cdn_blob_url(header_image.variant(**HEADER_IMAGE_VARIANTS[size]))
end
end

Expand Down
7 changes: 4 additions & 3 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ class Application < Rails::Application
config.active_job.queue_adapter = :sidekiq
end

if Rails.env.development?
config.action_mailer.default_url_options = { host: ::ResumisConfig.canonical_host }
end
config.action_mailer.default_url_options = {
host: ::ResumisConfig.canonical_host || 'http://localhost:5000'
}
Rails.application.routes.default_url_options = config.action_mailer.default_url_options

config.generators do |g|
g.test_framework :rspec,
Expand Down
4 changes: 4 additions & 0 deletions config/initializers/resumis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def self.canonical_host
ENV['RESUMIS_CANONICAL_HOST'].presence
end

def self.cdn_host
ENV['RESUMIS_CDN_HOST'].presence
end

def self.excluded_subdomains
if ENV['RESUMIS_EXCLUDED_SUBDOMAINS'].present?
ENV['RESUMIS_EXCLUDED_SUBDOMAINS'].split(',')
Expand Down
8 changes: 8 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,13 @@
end
end

direct :cdn_blob do |blob|
if ResumisConfig.cdn_host
File.join(ResumisConfig.cdn_host, blob.key)
else
route_for(:rails_blob, blob)
end
end

root 'manage/dashboard#index'
end

0 comments on commit aff621e

Please sign in to comment.