diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb new file mode 100644 index 0000000..e394240 --- /dev/null +++ b/app/controllers/attachments_controller.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class AttachmentsController < ApplicationController + skip_forgery_protection + + def show + if (key = decode_verified_key) + send_data ActiveStorage::Blob.service.download(key[:key]), content_type: key[:content_type], disposition: key[:disposition] + else + head :not_found + end + rescue Errno::ENOENT + head :not_found + end + + private + + def decode_verified_key + key = ActiveStorage.verifier.verified(params[:encoded_key], purpose: :blob_key) + key&.deep_symbolize_keys + end +end diff --git a/app/views/messages/_attachment.html.erb b/app/views/messages/_attachment.html.erb new file mode 100644 index 0000000..bb47e56 --- /dev/null +++ b/app/views/messages/_attachment.html.erb @@ -0,0 +1,49 @@ +<% if attachment.content_type&.start_with?('image/') %> +
<%= attachment.download %>+ <% end %> +
<%= message.body %>