make active storage work only when set up to locally store files

This commit is contained in:
riggraz
2025-02-06 22:15:33 +01:00
parent bf69381e1a
commit 71cf5b2e1c
4 changed files with 63 additions and 27 deletions

View File

@@ -0,0 +1,16 @@
class LocalFilesController < ApplicationController
skip_forgery_protection
def show
blob = ActiveStorage::Blob.find_by(key: params[:key])
if blob.present? && blob.service.is_a?(ActiveStorage::Service::DiskService)
send_file blob.service.path_for(blob.key),
type: blob.content_type, # Set correct MIME type
disposition: :inline, # Show in browser
filename: blob.filename.to_s # Ensure correct filename
else
head :not_found
end
end
end

View File

@@ -0,0 +1,44 @@
# ensure logger is available
logger = Rails.logger
if ENV["SKIP_ACTIVE_STORAGE_PATCH"] == "true"
logger.info "SKIP_ACTIVE_STORAGE_PATCH is set. Skip loading Active Storage Patch ..."
else
Rails.application.config.to_prepare do
logger.info "Loading Active Storage Blob Patch ..."
class ActiveStorage::Blob < ActiveStorage::Record
# Ensure variants are deleted only if variants tracking is disabled
# This is needed because ActiveStorage::PurgeJob fails if variants are tracked
# but also fails if variants are not tracked. The latter also keeps files in
# the variants folder which are basically stale.
# see https://github.com/rails/rails/issues/50583
def delete
service.delete(key)
# Never delete variants
# I had to edit the github snippet to make it work
# service.delete_prefixed("variants/#{key}/") if image? && !ActiveStorage.track_variants
end
# Override url method
# This is needed because ActiveStorage::Blob#url method does not work with local disk service
# Use LocalFilesController to serve local files if active storage service is local disk
# Otherwise use the default ActiveStorage::Blob#url method
def url(**options)
if service.is_a?(ActiveStorage::Service::DiskService)
Rails.application.routes.url_helpers.local_file_url(key: key, host: Rails.application.base_url)
else
service.url(
key,
expires_in: 5.minutes,
disposition: :inline,
filename: self.filename,
content_type: self.content_type,
**options
)
end
end
end
end
end

View File

@@ -1,27 +0,0 @@
# https://github.com/rails/rails/issues/50583#issuecomment-1920061327
# ensure logger is available
logger = Rails.logger
if ENV["SKIP_ACTIVE_STORAGE_PATCH"] == "true"
logger.info "SKIP_ACTIVE_STORAGE_PATCH is set. Skip loading Active Storage Patch ..."
else
# Ensure variants are deleted only if variants tracking is disabled
# This is needed because ActiveStorage::PurgeJob fails if variants are tracked
# but also fails if variants are not tracked. The latter also keeps files in
# the variants folder which are basically stale.
# see https://github.com/rails/rails/issues/50583
Rails.application.config.to_prepare do
logger.info "Loading Active Storage Blob Purge Patch ..."
class ActiveStorage::Blob < ActiveStorage::Record
def delete
service.delete(key)
# Never delete variants
# I had to edit the github snippet to make it work
# service.delete_prefixed("variants/#{key}/") if image? && !ActiveStorage.track_variants
end
end
end
end

View File

@@ -1,6 +1,9 @@
Rails.application.routes.draw do
if !Rails.application.multi_tenancy?
mount Rswag::Api::Engine => '/api-docs'
# in case active storage backend is local disk
get "/local_files/:key", to: "local_files#show", as: :local_file
end
if Rails.application.multi_tenancy?