Class: Collavre::Creatives::AttachmentsController

Inherits:
ApplicationController show all
Includes:
PublicAssetsHelper
Defined in:
app/controllers/collavre/creatives/attachments_controller.rb

Overview

Bearer-only multipart upload endpoint for agents/CLI. Creates an ActiveStorage blob from the uploaded bytes, embeds the matching node into the Creative’s description, and lets after_save reconcile attach it to creative.files. No server-local paths, no session/CSRF.

Instance Method Summary collapse

Methods included from PublicAssetsHelper

#public_asset_url

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/collavre/creatives/attachments_controller.rb', line 14

def create
  creative = Collavre::Creative.find_by(id: params[:creative_id])
  return render(json: { error: "Creative not found" }, status: :not_found) unless creative

  unless creative.has_permission?(Current.user, :write)
    return render(json: { error: "No write permission" }, status: :forbidden)
  end

  unless creative.attachments_embeddable?
    return render(json: { error: "Cannot attach files to GitHub-synced content" }, status: :unprocessable_entity)
  end

  file = params[:file]
  return render(json: { error: "No file provided" }, status: :unprocessable_entity) unless file.respond_to?(:read)

  io = file.to_io
  content_type = resolved_content_type(file, io)
  io.rewind
  blob = ActiveStorage::Blob.create_and_upload!(
    io: io,
    filename: file.original_filename,
    content_type: content_type
  )

  creative.embed_attachment_blob!(blob)

  render json: {
    signed_id: blob.signed_id,
    filename: blob.filename.to_s,
    content_type: blob.content_type,
    byte_size: blob.byte_size,
    url: public_asset_url(blob)
  }
end