diff --git a/.gitignore b/.gitignore
index abe6efc..505a927 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,5 @@
.bundle
Gemfile.lock
pkg/*
-*.DS_Store
\ No newline at end of file
+*.DS_Store
+.idea/*
\ No newline at end of file
diff --git a/app/controllers/kindeditor/assets_controller.rb b/app/controllers/kindeditor/assets_controller.rb
index 4f682f1..b78ec71 100644
--- a/app/controllers/kindeditor/assets_controller.rb
+++ b/app/controllers/kindeditor/assets_controller.rb
@@ -1,127 +1,89 @@
#coding: utf-8
-require "find"
+
class Kindeditor::AssetsController < ApplicationController
skip_before_action :verify_authenticity_token
def create
- @imgFile, @dir = params[:imgFile], params[:dir]
- unless @imgFile.nil?
- if Kindeditor::AssetUploader.save_upload_info? # save upload info into database
- begin
- @asset = "Kindeditor::#{@dir.camelize}".constantize.new(:asset => @imgFile)
- @asset.owner_id = params[:owner_id] ? params[:owner_id] : 0
- @asset.owner_type = params[:owner_type] ? params[:owner_type] : ''
- logger.warn '========= Warning: the owner have not been created, "delete uploaded files automatically" will not work. =========' if defined?(logger) && @asset.owner_id == 0
- @asset.asset_type = @dir
- if @asset.save
- render :plain => ({:error => 0, :url => @asset.asset.url}.to_json)
- else
- show_error(@asset.errors.full_messages)
- end
- rescue Exception => e
- show_error(e.to_s)
- end
- else # do not touch database
- begin
- uploader = "Kindeditor::#{@dir.camelize}Uploader".constantize.new
- uploader.store!(@imgFile)
- render :plain => ({:error => 0, :url => uploader.url}.to_json)
- rescue CarrierWave::UploadError => e
- show_error(e.message)
- rescue Exception => e
- show_error(e.to_s)
- end
+ cls = Kindeditor::AssetUploader.create_asset_model(params[:dir])
+ if cls
+ options = {
+ asset: params[:imgFile],
+ owner_id: params[:owner_id],
+ owner_type: params[:owner_type],
+ asset_type: params[:dir]
+ }.delete_if {|k,v| v.blank?}
+ asset = cls.new(options)
+ if asset.save
+ render json: {error: 0, url: asset.asset.url}
+ else
+ render json: {error: 1, message: asset.errors.full_messages}
end
else
- show_error("No File Selected!")
+ cls = Kindeditor::AssetUploader.create_uploader(params[:dir])
+ if cls
+ uploader = cls.new()
+ uploader.store!(params[:imgFile])
+ render json: {:error => 0, :url => uploader.url}
+ else
+ render json: {:error => 1, :message => "can't upload the file for #{params[:dir]} type!"}
+ end
end
end
def list
- @root_path = "#{Rails.public_path}/#{RailsKindeditor.upload_store_dir}/"
- @root_url = "/#{RailsKindeditor.upload_store_dir}/"
- @img_ext = Kindeditor::AssetUploader::EXT_NAMES[:image]
- @dir = params[:dir].strip || ""
- unless Kindeditor::AssetUploader::EXT_NAMES.keys.map(&:to_s).push("").include?(@dir)
- render :plain => "Invalid Directory name."
- return
+ root_url = File.join('/', RailsKindeditor.upload_store_dir)
+ root_url = File.join(root_url, params[:dir]) unless params[:dir].blank?
+ root_url.split('/').each do |dir|
+ path ||= Rails.public_path
+ path = File.join(path, dir)
+ Dir.mkdir(path) unless Dir.exist?(path)
end
-
- Dir.chdir(Rails.public_path)
- RailsKindeditor.upload_store_dir.split('/').each do |dir|
- Dir.mkdir(dir) unless Dir.exist?(dir)
- Dir.chdir(dir)
- end
-
- Dir.mkdir(@dir) unless Dir.exist?(@dir)
-
- @root_path += @dir + "/"
- @root_url += @dir + "/"
-
- @path = params[:path].strip || ""
- if @path.empty?
- @current_path = @root_path
- @current_url = @root_url
- @current_dir_path = ""
- @moveup_dir_path = ""
- else
- @current_path = @root_path + @path + "/"
- @current_url = @root_url + @path + "/"
- @current_dir_path = @path
- @moveup_dir_path = @current_dir_path.gsub(/(.*?)[^\/]+\/$/, "")
- end
- @order = %w(name size type).include?(params[:order].downcase) ? params[:order].downcase : "name"
- if !@current_path.match(/\.\./).nil?
- render :plain => "Access is not allowed."
- return
- end
- if @current_path.match(/\/$/).nil?
- render :plain => "Parameter is not valid."
- return
+ root_url = File.join(root_url, params[:path]) unless params[:path].blank?
+
+ root_path = File.join( Rails.public_path, root_url)
+ file_list = []
+ Dir.foreach(root_path) do |entry|
+ next if (entry == '..' || entry == '.')
+ full_path = File.join(root_path, entry)
+ file_ext = File.extname(full_path).gsub(/\./,"")
+ info = {
+ filename: entry,
+ datetime: File.mtime(full_path).to_s(:db)
+ }
+ if File.directory?(full_path)
+ info[:is_dir] = true
+ info[:has_file] = Dir.entries(full_path).size > 0
+ info[:filesize] = 0
+ info[:is_photo] = false
+ info[:filetype] = ""
+ else
+ info[:is_dir] = false
+ info[:has_file] = false
+ info[:filesize] = File.size(full_path)
+ info[:dir_path] = ""
+ info[:is_photo] = Kindeditor::AssetUploader.is_image?(file_ext)
+ info[:filetype] = file_ext
+ end
+ file_list << info
end
- if !File.exist?(@current_path) || !File.directory?(@current_path)
- render :plain => "Directory does not exist."
- return
+ order = "filename"
+ unless params[:order].blank?
+ order = params[:order].downcase if %w(filename filesize filetype datetime).include?(params[:order].downcase)
end
- @file_list = []
- Dir.foreach(@current_path) do |filename|
- hash = {}
- if filename != "." and filename != ".." and filename != ".DS_Store"
- file = @current_path + filename
- if File.directory?(file)
- hash[:is_dir] = true
- hash[:has_file] = (Dir.foreach(file).count > 2)
- hash[:filesize] = 0
- hash[:is_photo] = false
- hash[:filetype] = ""
- else
- hash[:is_dir] = false
- hash[:has_file] = false
- hash[:filesize] = File.size(file)
- hash[:dir_path] = ""
- file_ext = file.gsub(/.*\./,"")
- hash[:is_photo] = @img_ext.include?(file_ext)
- hash[:filetype] = file_ext
- end
- hash[:filename] = filename
- hash[:datetime] = File.mtime(file).to_s(:db)
- @file_list << hash
- end
+
+ if order=='datetime'
+ file_list.sort! {|a, b| b["#{order}".to_sym] <=> a["#{order}".to_sym]}
+ else
+ file_list.sort! {|a, b| a["#{order}".to_sym] <=> b["#{order}".to_sym]}
end
- @file_list.sort! {|a, b| a["file#{@order}".to_sym] <=> b["file#{@order}".to_sym]}
-
- @result = {}
- @result[:moveup_dir_path] = @moveup_dir_path
- @result[:current_dir_path] = @current_dir_path
- @result[:current_url] = @current_url
- @result[:total_count] = @file_list.count
- @result[:file_list] = @file_list
- render :plain => @result.to_json
- end
-
- private
- def show_error(msg)
- render :plain => ({:error => 1, :message => msg}.to_json)
+ result = {
+ moveup_dir_path: params[:path].gsub(/(.*?)[^\/]+\/$/, ""),
+ current_dir_path: params[:path],
+ current_url: root_url,
+ total_count: file_list.count,
+ file_list: file_list
+ }
+ render json: result
end
-
-end
\ No newline at end of file
+
+end
diff --git a/app/uploaders/kindeditor/asset_uploader.rb b/app/uploaders/kindeditor/asset_uploader.rb
index 8cb0a0f..580908b 100644
--- a/app/uploaders/kindeditor/asset_uploader.rb
+++ b/app/uploaders/kindeditor/asset_uploader.rb
@@ -19,7 +19,7 @@ class Kindeditor::AssetUploader < CarrierWave::Uploader::Base
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
- if Kindeditor::AssetUploader.save_upload_info?
+ if model
"#{RailsKindeditor.upload_store_dir}/#{model.asset_type.to_s.underscore.gsub(/(kindeditor\/)|(_uploader)/, '')}/#{model.created_at.strftime("%Y%m")}"
else
"#{RailsKindeditor.upload_store_dir}/#{self.class.to_s.underscore.gsub(/(kindeditor\/)|(_uploader)/, '')}/#{Time.now.strftime("%Y%m")}"
@@ -69,20 +69,40 @@ def delete_tmp_dir(new_file)
def filename
if original_filename
- @name ||= Digest::MD5.hexdigest(File.dirname(current_path)).slice(0, 12)
+ @name ||= Digest::MD5.hexdigest(current_path)
"#{@name}.#{file.extension}"
end
end
- def self.save_upload_info?
- begin
- %w(asset file flash image media).each do |s|
- "Kindeditor::#{s.camelize}".constantize
- end
- return true
- rescue
- return false
- end
+ def move_to_cache
+ false
+ end
+
+ def move_to_store
+ true
+ end
+
+ def self.has_asset?(name)
+ EXT_NAMES.keys.include?(name.to_sym)
+ end
+
+ def self.is_image?(extname)
+ EXT_NAMES[:image].include?(extname)
+ end
+
+
+ def self.create_asset_model(name)
+ return nil unless %w(asset file flash image media).include?(name.to_s)
+ class_name = "Kindeditor::#{name.camelize}"
+ return Object.const_get(class_name) if Object.const_defined?(class_name)
+ nil
+ end
+
+ def self.create_uploader(name)
+ return nil unless %w(asset file flash image media).include?(name.to_s)
+ class_name = "Kindeditor::#{name.camelize}Uploader"
+ return Object.const_get(class_name) if Object.const_defined?(class_name)
+ nil
end
end
diff --git a/lib/rails_kindeditor.rb b/lib/rails_kindeditor.rb
index 876e2f1..d9e996b 100644
--- a/lib/rails_kindeditor.rb
+++ b/lib/rails_kindeditor.rb
@@ -16,7 +16,7 @@ module RailsKindeditor
@@upload_flash_ext = %w[swf flv]
mattr_accessor :upload_media_ext
- @@upload_media_ext = %w[swf flv mp3 wav wma wmv mid avi mpg asf rm rmvb]
+ @@upload_media_ext = %w[f4v flv mp3 mp4 ogg webm]
mattr_accessor :upload_file_ext
@@upload_file_ext = %w[doc docx xls xlsx ppt htm html txt zip rar gz bz2]
@@ -57,5 +57,7 @@ def self.resize_to_limit
def self.setup
yield self
end
+
+
end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/helper.rb b/lib/rails_kindeditor/helper.rb
index 6bd0598..f7cf2ec 100644
--- a/lib/rails_kindeditor/helper.rb
+++ b/lib/rails_kindeditor/helper.rb
@@ -20,7 +20,7 @@ def kindeditor(name, method, options = {})
def merge_assets_info(options)
owner = options.delete(:owner)
options[:class] = "#{options[:class]} rails_kindeditor"
- if Kindeditor::AssetUploader.save_upload_info? && (!owner.nil?) && (!owner.id.nil?)
+ if (!owner.nil?) && (!owner.id.nil?)
begin
owner_id = owner.id
owner_type = owner.class.name
diff --git a/lib/rails_kindeditor/version.rb b/lib/rails_kindeditor/version.rb
index e21f221..c64b38d 100644
--- a/lib/rails_kindeditor/version.rb
+++ b/lib/rails_kindeditor/version.rb
@@ -1,4 +1,4 @@
module RailsKindeditor
- VERSION = "0.5.0"
+ VERSION = "0.6.0"
end
diff --git a/vendor/assets/javascripts/kindeditor/kindeditor.js b/vendor/assets/javascripts/kindeditor/kindeditor.js
index 51dd03a..a3e143a 100755
--- a/vendor/assets/javascripts/kindeditor/kindeditor.js
+++ b/vendor/assets/javascripts/kindeditor/kindeditor.js
@@ -210,7 +210,7 @@ var K = {
};
var _INLINE_TAG_MAP = _toMap('a,abbr,acronym,b,basefont,bdo,big,br,button,cite,code,del,dfn,em,font,i,img,input,ins,kbd,label,map,q,s,samp,select,small,span,strike,strong,sub,sup,textarea,tt,u,var'),
_BLOCK_TAG_MAP = _toMap('address,applet,blockquote,body,center,dd,dir,div,dl,dt,fieldset,form,frameset,h1,h2,h3,h4,h5,h6,head,hr,html,iframe,ins,isindex,li,map,menu,meta,noframes,noscript,object,ol,p,pre,script,style,table,tbody,td,tfoot,th,thead,title,tr,ul'),
- _SINGLE_TAG_MAP = _toMap('area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed'),
+ _SINGLE_TAG_MAP = _toMap('area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed','video'),
_STYLE_TAG_MAP = _toMap('b,basefont,big,del,em,font,i,s,small,span,strike,strong,sub,sup,u'),
_CONTROL_TAG_MAP = _toMap('img,table,input,textarea,button'),
_PRE_TAG_MAP = _toMap('pre,style,script'),
@@ -236,7 +236,7 @@ K.basePath = _getBasePath();
K.options = {
designMode : true,
fullscreenMode : false,
- filterMode : true,
+ filterMode : false,
wellFormatMode : true,
shadowMode : true,
loadStyleMode : true,
@@ -304,6 +304,7 @@ K.options = {
],
a : ['id', 'class', 'href', 'target', 'name'],
embed : ['id', 'class', 'src', 'width', 'height', 'type', 'loop', 'autostart', 'quality', '.width', '.height', 'align', 'allowscriptaccess', 'wmode'],
+ video : ['src', 'width', 'height','controls','class','autoplay'],
img : ['id', 'class', 'src', 'width', 'height', 'border', 'alt', 'title', 'align', '.width', '.height', '.border'],
'p,ol,ul,li,blockquote,h1,h2,h3,h4,h5,h6' : [
'id', 'class', 'align', '.text-align', '.color', '.background-color', '.font-size', '.font-family', '.background',
@@ -930,7 +931,7 @@ function _mediaType(src) {
if (/\.(rm|rmvb)(\?|$)/i.test(src)) {
return 'audio/x-pn-realaudio-plugin';
}
- if (/\.(swf|flv)(\?|$)/i.test(src)) {
+ if (/\.(swf|flv|mp4|ogg|mp3|webm|f4v)(\?|$)/i.test(src)) {
return 'application/x-shockwave-flash';
}
return 'video/x-ms-asf-plugin';
@@ -949,12 +950,29 @@ function _mediaAttrs(srcTag) {
return _getAttrList(unescape(srcTag));
}
function _mediaEmbed(attrs) {
- var html = '';
- return html;
+ var autostart = "";
+ if (attrs.autostart){
+ autostart = 'autoplay="autoplay"';
+ }
+ var video_html = '';
+ var html = "";
+ if (/\.(mp4|ogg|mp3|webm|f4v)(\?|$)/i.test(attrs.flashvars)) {
+ html = "';
+ }else if(/\.(mp4|ogg|mp3|webm|f4v)(\?|$)/i.test(attrs.src)) {
+ html = "';
+
+ }else{
+ html = video_html;
+ }
+ return html;
}
function _mediaImg(blankPath, attrs) {
var width = attrs.width,
@@ -5292,7 +5310,7 @@ KEditor.prototype = {
text : function(val) {
var self = this;
if (val === undefined) {
- return _trim(self.html().replace(/<(?!img|embed).*?>/ig, '').replace(/ /ig, ' '));
+ return _trim(self.html().replace(/<(?!img|embed|video).*?>/ig, '').replace(/ /ig, ' '));
} else {
return self.html(_escape(val));
}
@@ -5315,7 +5333,7 @@ KEditor.prototype = {
return self.html().length;
}
if (mode === 'text') {
- return self.text().replace(/<(?:img|embed).*?>/ig, 'K').replace(/\r\n|\n|\r/g, '').length;
+ return self.text().replace(/<(?:img|embed|video).*?>/ig, 'K').replace(/\r\n|\n|\r/g, '').length;
}
return 0;
},
@@ -6043,6 +6061,7 @@ _plugin('core', function(K) {
return full;
});
}
+ html = html.replace(/(<\/?video.*?>)/g, '');
return html.replace(/