Module: SMEditor::Rails::Uploads

Included in:
UploadsController
Defined in:
lib/smeditor/rails/uploads.rb

Overview

Controller concern backing the image uploader.

class SMEditor::UploadsController < ApplicationController
include SMEditor::Rails::Uploads
end

The JS side posts a file param; the controller stores it via ActiveStorage and responds with image attributes for the editor.

Active only when config.uploads == :active_storage — otherwise the endpoint responds with 404 and the host wires its own upload.

The endpoint is unauthenticated by default. It enforces a content type allow-list and a size ceiling, but an app with real users should subclass SMEditor::UploadsController and add authorization.

Instance Method Summary collapse

Instance Method Details

#create ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/smeditor/rails/uploads.rb', line 21

def create
  error = upload_error
  if error
    render json: { error: error[:message] }, status: error[:status]
  else
    blob = ActiveStorage::Blob.create_and_upload!(
      io: upload_file.to_io,
      filename: upload_file.original_filename,
      content_type: upload_file.content_type,
    )

    render json: image_payload(blob, upload_file)
  end
end