Class: Kindeditor::AssetsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/kindeditor/assets_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/kindeditor/assets_controller.rb', line 5

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)
        if @asset.save
          render :text => ({: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 :text => ({:error => 0, :url => uploader.url}.to_json)
      rescue CarrierWave::UploadError => e
        show_error(e.message)
      rescue Exception => e
        show_error(e.to_s)
      end
    end
  else
    show_error("No File Selected!")
  end
end

#listObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/kindeditor/assets_controller.rb', line 35

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 :text => "Invalid Directory name."
    return
  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 :text => "Access is not allowed."
    return
  end
  if @current_path.match(/\/$/).nil?
    render :text => "Parameter is not valid."
    return
  end
  if !File.exist?(@current_path) || !File.directory?(@current_path)
    render :text => "Directory does not exist."
    return
  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
  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 :text => @result.to_json
end