Include autenticazione, progetti isolati, mail marketing HTML con SMTP, test A/B e editor WYSIWYG. Co-authored-by: Cursor <cursoragent@cursor.com>
31 lines
1012 B
Ruby
31 lines
1012 B
Ruby
class MailImagesController < ApplicationController
|
|
before_action :require_current_project!
|
|
|
|
MAX_BYTES = 5.megabytes
|
|
ALLOWED_TYPES = %w[image/jpeg image/png image/gif image/webp].freeze
|
|
|
|
def create
|
|
file = params[:file]
|
|
unless file.respond_to?(:content_type)
|
|
return render json: { error: "Seleziona un'immagine." }, status: :unprocessable_entity
|
|
end
|
|
|
|
content_type = Marcel::MimeType.for(file, name: file.original_filename, declared_type: file.content_type)
|
|
unless ALLOWED_TYPES.include?(content_type)
|
|
return render json: { error: "Formato non valido. Usa JPG, PNG, GIF o WebP." }, status: :unprocessable_entity
|
|
end
|
|
|
|
if file.size > MAX_BYTES
|
|
return render json: { error: "L'immagine supera i 5 MB." }, status: :unprocessable_entity
|
|
end
|
|
|
|
blob = ActiveStorage::Blob.create_and_upload!(
|
|
io: file,
|
|
filename: file.original_filename.presence || "immagine",
|
|
content_type: content_type
|
|
)
|
|
|
|
render json: { url: url_for(blob) }
|
|
end
|
|
end
|