class FroalaEditorSDK::File

File functionality.

Attributes

var[R]

Public Class Methods

delete(file = params[:file], path) click to toggle source

Deletes a file found on the server. Params:

file

The file that will be deleted from the server.

path

The server path where the file resides.

Returns true or false.

# File lib/froala-editor-sdk/file.rb, line 81
def self.delete(file = params[:file], path)

  file_path = Rails.root.join(path, ::File.basename(file))
  begin
    if ::File.delete(file_path)
      return true
    else
      return false
    end
  rescue => exception
    return false
  end
end
resize(options, path) click to toggle source

Resizes an image based on the options provided. The function resizes the original file, Params:

options

The options that contain the resize hash

path

The path where the image is stored

# File lib/froala-editor-sdk/file.rb, line 100
def self.resize (options, path)
  image = MiniMagick::Image.new(path)
  image.path
  image.resize("#{options[:resize][:width]}x#{options[:resize][:height]}")
end
save(file, path, file_access_path) click to toggle source

Saves a file on the server. Params:

file

The uploaded file that will be saved on the server.

path

The path where the file will be saved.

# File lib/froala-editor-sdk/file.rb, line 59
def self.save (file, path, file_access_path)

  # Create directory if it doesn't exist.
  dirname = ::File.dirname(path)
  unless ::File.directory?(dirname)
    ::FileUtils.mkdir_p(dirname)
  end

  if ::File.open(path, "wb") {|f| f.write(file.read)}

    # Returns a public accessible server path.
    return "#{file_access_path}#{Utils.get_file_name(path)}"
  else
    return "error"
  end
end
upload(params, upload_path = @default_upload_path, options = {}) click to toggle source

Uploads a file to the server. Params:

params

File upload parameter mostly is “file”.

upload_path

Server upload path, a storage path where the file will be stored.

options

Hash object that contains configuration parameters for uploading a file.

Returns json object

# File lib/froala-editor-sdk/file.rb, line 28
def self.upload(params, upload_path = @default_upload_path, options = {})

  # Merge options.
  options = @default_options.merge(options)

  file = params[options[:fieldname]]

  if file

    # Validates the file extension and mime type.
    validation = Validation.check(file, options)

    # Uses the Utlis name function to generate a random name for the file.
    file_name = Utils.name(file)
    path = Rails.root.join(upload_path, file_name)

    # Saves the file on the server and returns the path.
    serve_url = save(file, path, options[:file_access_path])

    resize(options, path) if !options[:resize].nil?

    return {:link => serve_url}.to_json
  else
    return nil
  end
end

Public Instance Methods

var() click to toggle source
# File lib/froala-editor-sdk/file.rb, line 110
def var
  self.class.var
end