Class: Cas::Api::FilesController

Inherits:
Cas::ApplicationController
  • Object
show all
Defined in:
app/controllers/cas/api/files_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/cas/api/files_controller.rb', line 4

def create
  if ENV.fetch("S3_BUCKET")
    service = "s3"
  end
   = resource_params[:attributes][:metadata]
  file = ::Cas::MediaFile.new(
    service: service,
    size: [:original][:metadata][:size].to_i,
    original_name: [:original][:metadata][:filename],
    mime_type: [:original][:metadata][:mime_type],
    media_type: resource_params[:attributes][:media_type],
    file: .to_json,
    attachable: attachable_record
  )
  file.save
  Cas::RemoteCallbacks.callbacks[:after_file_upload].call(file)
  render json: {
    data: {
      id: file.id.to_s,
      type: "media-files",
      attributes: {
        url: file.url(version: :original, use_cdn: false).to_s,
        "original-name": file.original_name
      }
    }
  }
end

#destroyObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/cas/api/files_controller.rb', line 32

def destroy
  files = ::Cas::MediaFile.where(id: params[:id].split(","))
  success = nil
  ActiveRecord::Base.transaction do
    success = files.each(&:destroy).all?
  end

  if success
    render json: {}, status: 204
  else
    render json: {}, status: 400
  end
end