Class: FroalaEditorSDK::File
- Inherits:
-
Object
- Object
- FroalaEditorSDK::File
- Defined in:
- lib/froala-editor-sdk/file.rb
Overview
File functionality.
Class Attribute Summary collapse
-
.var ⇒ Object
readonly
Returns the value of attribute var.
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, file_access_path) ⇒ Object
Saves a file on the server.
-
.upload(params, upload_path = @default_upload_path, options = {}) ⇒ Object
Uploads a file to the server.
Instance Method Summary collapse
Class Attribute Details
.var ⇒ Object (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:
fileThe file that will be deleted from the server.
pathThe 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:
optionsThe options that contain the resize hash
pathThe path where the image is stored
100 101 102 103 104 |
# File 'lib/froala-editor-sdk/file.rb', line 100 def self.resize (, path) image = MiniMagick::Image.new(path) image.path image.resize("#{[:resize][:width]}x#{[:resize][:height]}") end |
.save(file, path, file_access_path) ⇒ Object
Saves a file on the server. Params:
fileThe uploaded file that will be saved on the server.
pathThe 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:
paramsFile upload parameter mostly is "file".
upload_pathServer upload path, a storage path where the file will be stored.
optionsHash 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, = {}) # 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, [:file_access_path]) resize(, path) if ![:resize].nil? return {:link => serve_url}.to_json else return nil end end |
Instance Method Details
#var ⇒ Object
110 111 112 |
# File 'lib/froala-editor-sdk/file.rb', line 110 def var self.class.var end |