Class: ElFinder2::Command::Upload

Inherits:
Base
  • Object
show all
Defined in:
lib/el_finder2/command/upload.rb

Overview

Process file upload requests. Client may request the upload of multiple files at once.

Constant Summary collapse

IMAGE_MIME_REGEX =
/^(image|(x-)?application)\/(bmp|gif|p?jpeg|jpg|(x-)?png)$/

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from HashUtils

#from_base64url, #to_base64url, #to_path

Constructor Details

This class inherits a constructor from ElFinder2::Command::Base

Instance Method Details

#executeObject

Response: An array of successfully uploaded files if success, an error otherwise.

added : (Array) of files that were successfully uploaded. Information
        about File/Directory

If the files could not be loaded, return the following:

error: "Unable to upload files"

If at least one file has been uploaded, the response is Client-Server-API-2.1 # open and * select *. If not all files are uploaded, failures will be put in * error * and * errorData *: {

// open
// (Array)  An array of hashes of the loaded paths
select: [ "8d331825ebfbe1ddae14d314bf81a712" ],
// (String) If not all files have been uploaded
error: "Some files were not uploaded",
// (Object) Info about the files that could not be uploaded
errorData{
  // (String) "filename": "error"
  "some-file.exe": "Not allowed file type"
}

}



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/el_finder2/command/upload.rb', line 31

def execute
  uploads = @uploads.map do |upload|
    type = upload_type(upload)
    attributes = type.upload_attributes(upload).merge(content: upload)

    type.all.merge(@folder.children).
      where(name: upload.original_filename).
      first_or_initialize.
      tap { |instance| instance.update_attributes(attributes) }
  end

  added, errored = uploads.partition(&:valid?)

  response = { added: ActiveModel::ArraySerializer.new(added).as_json }

  unless errored.empty?
    response[:error] = 'Some files were not uploaded'
    response[:errorData] = errored.map do |file|
      { file.name => file.errors.full_messages.to_sentence }
    end.reduce(&:merge)
  end

  render json: response
end