Class: FroalaEditorSDK::File
- Inherits:
-
Object
- Object
- FroalaEditorSDK::File
- Defined in:
- lib/froala-editor-sdk/file.rb
Overview
File functionality.
Constant Summary collapse
- @@default_options =
Default options that are used if no options are passed to the upload function
{ fieldname: 'file', validation: { allowedExts: [".txt", ".pdf", ".doc", ".json", ".html"], allowedMimeTypes: [ "text/plain", "application/msword", "application/x-pdf", "application/pdf", "application/json","text/html" ] }, resize: nil }
- @@default_upload_path =
Default upload path.
"public/uploads/files"
Class Method Summary collapse
-
.delete(file = , path) ⇒ Object
Deletes a file found on the server.
-
.resize(options, path) ⇒ Object
Resizes an image based on the options provided.
-
.save(file, path) ⇒ Object
Saves a file on the server.
-
.upload(params, upload_path = @@default_upload_path, options = nil) ⇒ Object
Uploads a file to the server.
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.
80 81 82 83 84 85 86 87 88 |
# File 'lib/froala-editor-sdk/file.rb', line 80 def self.delete(file = params[:file], path) file_path = Rails.root.join(path, ::File.basename(file)) if ::File.delete(file_path) return true else 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
95 96 97 98 99 |
# File 'lib/froala-editor-sdk/file.rb', line 95 def self.resize (, path) image = MiniMagick::Image.new(path) image.path image.resize("#{[:resize][:height]}x#{[:resize][:width]}") end |
.save(file, 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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/froala-editor-sdk/file.rb', line 58 def self.save (file, 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 "#{"/uploads/"}#{Utils.get_file_name(path)}" else return "error" end end |
.upload(params, upload_path = @@default_upload_path, options = nil) ⇒ 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
27 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 |
# File 'lib/froala-editor-sdk/file.rb', line 27 def self.upload(params, upload_path = @@default_upload_path, = nil) # Merge options. = ( || @@default_options).merge() file = params[[:fieldname]] if file # Validates the file extension and mime type. validation = Validation.check(file, ) # 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) resize(, path) if ![:resize].nil? return {:link => serve_url}.to_json else return nil end end |