mirror of
https://github.com/System-End/cdn.git
synced 2026-04-19 18:35:12 +00:00
58 lines
1.6 KiB
Ruby
58 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module API
|
|
module V4
|
|
class UploadsController < ApplicationController
|
|
# POST /api/v4/upload
|
|
def create
|
|
file = params[:file]
|
|
|
|
unless file.present?
|
|
render json: { error: "Missing file parameter" }, status: :bad_request
|
|
return
|
|
end
|
|
|
|
blob = ActiveStorage::Blob.create_and_upload!(
|
|
io: file.tempfile,
|
|
filename: file.original_filename,
|
|
content_type: file.content_type
|
|
)
|
|
|
|
upload = current_user.uploads.create!(blob: blob, provenance: :api)
|
|
|
|
render json: upload_json(upload), status: :created
|
|
rescue => e
|
|
render json: { error: "Upload failed: #{e.message}" }, status: :unprocessable_entity
|
|
end
|
|
|
|
# POST /api/v4/upload_from_url
|
|
def create_from_url
|
|
url = params[:url]
|
|
|
|
unless url.present?
|
|
render json: { error: "Missing url parameter" }, status: :bad_request
|
|
return
|
|
end
|
|
|
|
upload = Upload.create_from_url(url, user: current_user, provenance: :api, original_url: url)
|
|
|
|
render json: upload_json(upload), status: :created
|
|
rescue => e
|
|
render json: { error: "Upload failed: #{e.message}" }, status: :unprocessable_entity
|
|
end
|
|
|
|
private
|
|
|
|
def upload_json(upload)
|
|
{
|
|
id: upload.id,
|
|
filename: upload.filename.to_s,
|
|
size: upload.byte_size,
|
|
content_type: upload.content_type,
|
|
url: upload.cdn_url,
|
|
created_at: upload.created_at.iso8601
|
|
}
|
|
end
|
|
end
|
|
end
|
|
end
|