Skip to content

Commit 4a87c5a

Browse files
committed
Initial commit
1 parent cd13ef9 commit 4a87c5a

20 files changed

+1288
-0
lines changed

.idea/.rakeTasks

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+706
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/wysiwyg-editor-ruby-sdk.iml

+26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec

Gemfile.lock

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
PATH
2+
remote: .
3+
specs:
4+
wysiwyg-editor-ruby-sdk (0.0.1)
5+
mime_types (~> 0.0.1)
6+
mini_magick (~> 4.5.0)
7+
wysiwyg-rails (~> 2.6.0)
8+
9+
GEM
10+
remote: https://rubygems.org/
11+
specs:
12+
actionpack (5.1.1)
13+
actionview (= 5.1.1)
14+
activesupport (= 5.1.1)
15+
rack (~> 2.0)
16+
rack-test (~> 0.6.3)
17+
rails-dom-testing (~> 2.0)
18+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
19+
actionview (5.1.1)
20+
activesupport (= 5.1.1)
21+
builder (~> 3.1)
22+
erubi (~> 1.4)
23+
rails-dom-testing (~> 2.0)
24+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
25+
activesupport (5.1.1)
26+
concurrent-ruby (~> 1.0, >= 1.0.2)
27+
i18n (~> 0.7)
28+
minitest (~> 5.1)
29+
tzinfo (~> 1.1)
30+
builder (3.2.3)
31+
concurrent-ruby (1.0.5)
32+
erubi (1.6.0)
33+
font-awesome-sass (4.7.0)
34+
sass (>= 3.2)
35+
i18n (0.8.1)
36+
loofah (2.0.3)
37+
nokogiri (>= 1.5.9)
38+
method_source (0.8.2)
39+
mime_types (0.0.1)
40+
mini_magick (4.5.1)
41+
mini_portile2 (2.1.0)
42+
minitest (5.10.2)
43+
nokogiri (1.7.2)
44+
mini_portile2 (~> 2.1.0)
45+
rack (2.0.3)
46+
rack-test (0.6.3)
47+
rack (>= 1.0)
48+
rails-dom-testing (2.0.3)
49+
activesupport (>= 4.2.0)
50+
nokogiri (>= 1.6)
51+
rails-html-sanitizer (1.0.3)
52+
loofah (~> 2.0)
53+
railties (5.1.1)
54+
actionpack (= 5.1.1)
55+
activesupport (= 5.1.1)
56+
method_source
57+
rake (>= 0.8.7)
58+
thor (>= 0.18.1, < 2.0)
59+
rake (12.0.0)
60+
sass (3.4.24)
61+
thor (0.19.4)
62+
thread_safe (0.3.6)
63+
tzinfo (1.2.3)
64+
thread_safe (~> 0.1)
65+
wysiwyg-rails (2.6.0)
66+
font-awesome-sass (~> 4.4, >= 4.4.0)
67+
railties (>= 3.2, < 6.0)
68+
69+
PLATFORMS
70+
ruby
71+
72+
DEPENDENCIES
73+
wysiwyg-editor-ruby-sdk!
74+
75+
BUNDLED WITH
76+
1.15.0

Rakefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "bundler/gem_tasks"
2+
require "rake/testtask"
3+
4+
Rake::TestTask.new(:test)
5+
6+
task :default => :test

lib/froala_editor.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'mini_magick'
2+
require 'froala_editor/utils/file_validation'
3+
require 'froala_editor/utils/image_validation'
4+
require 'froala_editor/utils/video_validation'
5+
require 'froala_editor/utils/utils'
6+
7+
8+
require 'froala_editor/file'
9+
require 'froala_editor/image'
10+
require 'froala_editor/s3'
11+
require 'froala_editor/version'
12+
require 'froala_editor/video'

lib/froala_editor/file.rb

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
module FroalaEditor
2+
3+
# File functionality.
4+
class File
5+
6+
# Default options that are used if no options are passed to the upload function
7+
@@default_options = {
8+
fieldname: 'file',
9+
validation: {
10+
allowedExts: [".txt", ".pdf", ".doc", ".json", ".html"],
11+
allowedMimeTypes: [ "text/plain", "application/msword", "application/x-pdf", "application/pdf", "application/json","text/html" ]
12+
},
13+
resize: nil
14+
}
15+
16+
# Uploads a file to the server.
17+
# Params:
18+
# +params+:: File upload parameter mostly is "file".
19+
# +upload_path+:: Server upload path, a storage path where the file will be stored.
20+
# +options+:: Hash object that contains configuration parameters for uploading a file.
21+
# Returns json object
22+
def self.upload(params, upload_path = "public/uploads/files", options = nil)
23+
24+
if options == nil
25+
options = @@default_options
26+
else
27+
options = @@default_options.merge(options)
28+
end
29+
file = params[options[:fieldname]]
30+
31+
if file
32+
# Validates the file extension and mime type.
33+
validation = FileValidation.check(file, options)
34+
# Uses the Utlis name function to generate a random name for the file.
35+
file_name = Utils.name(file)
36+
path = Rails.root.join(upload_path, file_name)
37+
# Saves the file on the server and returns the path.
38+
serve_url = save(file, path)
39+
40+
return {:link => serve_url}.to_json
41+
else
42+
return nil
43+
end
44+
end
45+
46+
# Saves a file on the server.
47+
# Params:
48+
# +file+:: The uploaded file that will be saved on the server.
49+
# +path+:: The path where the file will be saved.
50+
def self.save (file, path)
51+
if ::File.open(path, "wb") {|f| f.write(file.read)}
52+
# Returns a public accessible server path.
53+
return "#{"/uploads/"}#{Utils.get_file_name(path)}"
54+
else
55+
return "error"
56+
end
57+
end
58+
59+
# Deletes a file found on the server.
60+
# Params:
61+
# +file+:: The file that will be deleted from the server.
62+
# +path+:: The server path where the file resides.
63+
# Returns true or false.
64+
def self.delete(file = params[:file], path)
65+
66+
file_path = Rails.root.join(path, ::File.basename(file))
67+
if ::File.delete(file_path)
68+
return true
69+
else
70+
return false
71+
end
72+
end
73+
end
74+
end

lib/froala_editor/image.rb

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
module FroalaEditor
2+
# Image functionality.
3+
class Image
4+
5+
# Default options that are used if no options are passed to the upload function
6+
@@default_options = {
7+
fieldname: 'file',
8+
validation: {
9+
allowedExts: [".gif", ".jpeg", ".jpg", ".png", ".svg", ".blob"],
10+
allowedMimeTypes: [ "image/gif", "image/jpeg", "image/pjpeg", "image/x-png", "image/png", "image/svg+xml" ]
11+
},
12+
resize: nil
13+
}
14+
15+
# Uploads an image to the server.
16+
# Params:
17+
# +params+:: File upload parameter mostly is "file".
18+
# +upload_path+:: Server upload path, a storage path where the image will be stored.
19+
# +options+:: Hash object that contains configuration parameters for uploading a image.
20+
# Returns json object
21+
def self.upload(params, upload_path = "public/uploads/images", options = nil)
22+
23+
if options == nil
24+
options = @@default_options
25+
else
26+
options = @@default_options.merge(options)
27+
end
28+
29+
file = params[options[:fieldname]]
30+
31+
32+
if file
33+
# Validates the image extension and mime type.
34+
validation = ImageValidation.check(file, options)
35+
# Uses the Utlis name function to generate a random name for the image.
36+
file_name = Utils.name(file)
37+
path = Rails.root.join(upload_path, file_name)
38+
# Saves the image on the server and returns the path.
39+
serve_url = save(file, path)
40+
# Check the option param, if resize is not needed it will use default options constant.
41+
if !options[:resize].nil?
42+
resize = image_resize(options, path)
43+
44+
return {:link => serve_url}.to_json
45+
else
46+
return {:link => serve_url}.to_json
47+
end
48+
49+
end
50+
end
51+
52+
# Saves an image on the server.
53+
# Params:
54+
# +file+:: The uploaded image that will be saved on the server.
55+
# +path+:: The path where the image will be saved.
56+
def self.save(file, path)
57+
if ::File.open(path, "wb") {|f| f.write(file.read)}
58+
# Returns a public accessible server path.
59+
return "#{"/uploads/"}#{Utils.get_file_name(path)}"
60+
else
61+
return "error"
62+
end
63+
end
64+
65+
# Resizes an image based on the options provided.
66+
# The function resizes the original file,
67+
# Params:
68+
# +options+:: The options that contain the resize hash
69+
# +path+:: The path where the image is stored
70+
def self.image_resize(options, path)
71+
72+
image = MiniMagick::Image.new(path)
73+
image.path
74+
image.resize("#{options[:resize][:height]}x#{options[:resize][:width]}")
75+
end
76+
77+
# Deletes an image found on the server.
78+
# Params:
79+
# +file+:: The image that will be deleted from the server.
80+
# +path+:: The server path where the image resides.
81+
# Returns true or false.
82+
def self.delete(file = params[:file], path)
83+
84+
file_path = Rails.root.join(path, ::File.basename(file))
85+
86+
if ::File.delete(file_path)
87+
return true
88+
else
89+
return false
90+
end
91+
end
92+
93+
# Loads the images from a specific path
94+
# Params:
95+
# +path+:: The server path where the images are saved
96+
# Returns Json object
97+
def self.load_images(path)
98+
99+
images = Dir["#{path}*"]
100+
all_images = []
101+
102+
images.each do |img|
103+
all_images.push({url: "#{"/uploads/"}#{Utils.get_file_name(img)}"})
104+
end
105+
106+
return all_images.to_json
107+
end
108+
end
109+
end

0 commit comments

Comments
 (0)