class ElFinder2::Command::Upload

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

Constants

IMAGE_MIME_REGEX

Public Instance Methods

execute() click to toggle source

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"
}

}

# 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

Private Instance Methods

parse_params!(params) click to toggle source

Arguments (HTTP POST):

target : hash of the directory to upload to
upload[] : array of multipart files to upload
# File lib/el_finder2/command/upload.rb, line 59
        def parse_params!(params)
  target, @uploads = params.values_at(:target, :upload)

  fail ElFinder2::Error.new(%w(errCmdParams upload)) unless @uploads.present? && target

  path = to_path(target)

  @folder = ElFinder2::Folder.find_by_path(path)

  fail ElFinder2::Error.new('errFolderNotFound') unless @folder
end
upload_type(upload) click to toggle source
# File lib/el_finder2/command/upload.rb, line 73
        def upload_type(upload)
  mime_type = Paperclip::ContentTypeDetector.new(upload.path).detect

  if mime_type =~ ElFinder2::IMAGE_MIME_REGEX
    ElFinder2::Image
  else
    ElFinder2::Document
  end
end