Files
fizzy/app/controllers/uploads_controller.rb
T
Kevin McConnell cc1051c968 Expire upload links after 5 minutes
By default the Active Storage URLs are valid for 5 minutes. We should
avoid caching the responses for longer than that.
2025-01-28 16:13:26 +00:00

29 lines
710 B
Ruby

class UploadsController < ApplicationController
include ActiveStorage::SetCurrent
before_action :set_file, only: :create
before_action :set_attachment, only: :show
def create
@upload = Current.account.uploads_attachments.create! blob: create_blob!
end
def show
expires_in 5.minutes, public: true
redirect_to @attachment.url
end
private
def set_file
@file = params[:file]
end
def set_attachment
@attachment = ActiveStorage::Attachment.find_by! slug: "#{params[:slug]}.#{params[:format]}"
end
def create_blob!
ActiveStorage::Blob.create_and_upload! io: @file, filename: @file.original_filename, content_type: @file.content_type
end
end