Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify code #94

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e07c38d
fixed project root path is changed!
songgz Jun 1, 2017
226b5b7
modify video
zhaoxia1204 Jun 7, 2017
183ba9f
modify video
zhaoxia1204 Jun 8, 2017
b13c044
modify video
zhaoxia1204 Jun 8, 2017
400aed0
modify video
zhaoxia1204 Jun 8, 2017
86661ea
modify video
zhaoxia1204 Jun 8, 2017
2d10e80
modify video
zhaoxia1204 Jun 8, 2017
47fc76f
modify video
zhaoxia1204 Jun 8, 2017
ee5cb9b
modify video
zhaoxia1204 Jun 8, 2017
2a77428
modify video
zhaoxia1204 Jun 8, 2017
67d8fd8
modify video
zhaoxia1204 Jul 20, 2017
0c49cd5
modify video
zhaoxia1204 Jul 20, 2017
b384d25
Update asset_uploader.rb
songgz Jul 24, 2017
e8393f8
Update asset_uploader.rb
songgz Jul 24, 2017
4d63536
Update asset_uploader.rb
songgz Nov 9, 2017
3c9d833
modify assets_controller.rb
sgzhe Nov 10, 2017
921bdc7
modify for git
sgzhe Nov 10, 2017
c5c98f1
modify asset_upload.rb for filename method.
sgzhe Nov 10, 2017
6ecf1b6
modify asset_upload.rb and asset_controller.rb
sgzhe Nov 10, 2017
9cc7086
modify help.rb
sgzhe Nov 10, 2017
5114d4a
modify help.rb
sgzhe Nov 10, 2017
0966cc9
Update assets_controller.rb
songgz Nov 10, 2017
ac5dfef
modify controller
sgzhe Nov 10, 2017
587d67d
Merge remote-tracking branch 'origin/master'
sgzhe Nov 10, 2017
944ec1d
Update kindeditor.js
songgz Mar 6, 2018
cf55e54
modify controller
sgzhe Mar 6, 2018
9f3916c
modify kindeditor.js
sgzhe Mar 6, 2018
42e7d59
modify assets_controller.rb
sgzhe Mar 22, 2018
39bbd9e
modify assets_controller.rb
sgzhe Mar 22, 2018
ef0b201
modify kindeditor.js for video
sgzhe Apr 9, 2018
b8a8517
modify kindeditor.js for video
sgzhe Apr 9, 2018
090ad7c
remove video modify
sgzhe Apr 9, 2018
9b91d7a
modify video
sgzhe Apr 12, 2018
cfd360f
modify video
sgzhe Apr 12, 2018
964a512
modify video
sgzhe Apr 12, 2018
50d12a1
modify video
sgzhe Apr 12, 2018
5371e57
modify video
sgzhe Apr 12, 2018
6842854
modify video
sgzhe Apr 12, 2018
d71e7d4
modify video width height
sgzhe May 4, 2018
fe08f23
modify video
sgzhe May 8, 2018
b3933ed
modify video
sgzhe May 9, 2018
654e1a3
modify video
sgzhe May 11, 2018
8e859cb
modify video
sgzhe May 11, 2018
15ece17
modify video
sgzhe May 11, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.bundle
Gemfile.lock
pkg/*
*.DS_Store
*.DS_Store
.idea/*
184 changes: 73 additions & 111 deletions app/controllers/kindeditor/assets_controller.rb
Original file line number Diff line number Diff line change
@@ -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

end
42 changes: 31 additions & 11 deletions app/uploaders/kindeditor/asset_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")}"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/rails_kindeditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -57,5 +57,7 @@ def self.resize_to_limit
def self.setup
yield self
end



end
2 changes: 1 addition & 1 deletion lib/rails_kindeditor/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_kindeditor/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module RailsKindeditor
VERSION = "0.5.0"
VERSION = "0.6.0"
end

Loading