Class: FroalaEditorSDK::File

Inherits:
Object
  • Object
show all
Defined in:
lib/froala-editor-sdk/file.rb

Overview

File functionality.

Direct Known Subclasses

Image, Video

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.varObject (readonly)

Returns the value of attribute var.



107
108
109
# File 'lib/froala-editor-sdk/file.rb', line 107

def var
  @var
end

Class Method Details

.delete(file = , path) ⇒ Object

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.



81
82
83
84
85
86
87
88
89
90
91
92
93
# 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) ⇒ Object

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



100
101
102
103
104
# 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) ⇒ Object

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.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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 = {}) ⇒ Object

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

Instance Method Details

#varObject



110
111
112
# File 'lib/froala-editor-sdk/file.rb', line 110

def var
  self.class.var
end