Skip to content

Commit

Permalink
allow to create attachments with key
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Jun 15, 2023
1 parent 14b4db6 commit 24f3ae1
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/controllers/motor/active_storage_attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ class ActiveStorageAttachmentsController < ApiBaseController
load_and_authorize_resource :attachment, class: 'ActiveStorage::Attachment', parent: false

def create
@attachment.assign_attributes(blob: ActiveStorage::Blob.create_and_upload!(file_params), record: record)
blob = if file_params[:key]
ActiveStorage::Blob.create!(file_params)
else
ActiveStorage::Blob.create_and_upload!(file_params)
end

@attachment.assign_attributes(blob: blob, record: record)
@attachment.assign_attributes(record_type: '', record_id: 0) unless record

if @attachment.save(validate: false)
Expand All @@ -36,7 +42,9 @@ def record
end

def file_params
attrs = params.require(:data).require(:file).permit(:io, :filename).to_h.symbolize_keys
attrs = params.require(:data).require(:file).permit(:io, :filename, :key,
:checksum, :byte_size,
:content_type).to_h.symbolize_keys

return attrs if params.dig(:data, :file, :base64).blank?

Expand Down

0 comments on commit 24f3ae1

Please sign in to comment.