scinote-web/app/controllers/active_storage/direct_uploads_controller.rb

34 lines
1,007 B
Ruby
Raw Normal View History

# frozen_string_literal: true
module ActiveStorage
class DirectUploadsController < CustomBaseController
2020-03-03 22:20:29 +08:00
before_action :check_file_size, only: :create
def create
blob = ActiveStorage::Blob.create_before_direct_upload!(blob_args)
render json: direct_upload_json(blob)
end
private
def blob_args
args = params.require(:blob)
.permit(:filename, :byte_size, :checksum, :content_type, :metadata)
.to_h
.symbolize_keys
args[:content_type] = 'application/octet-stream' if args[:content_type].blank?
args
end
def direct_upload_json(blob)
blob.as_json(root: false, methods: :signed_id)
.merge(direct_upload: { url: blob.service_url_for_direct_upload,
headers: blob.service_headers_for_direct_upload })
end
2020-03-03 22:20:29 +08:00
def check_file_size
render_403 if blob_args[:byte_size] > Rails.configuration.x.file_max_size_mb.megabytes
end
end
end