Class: FileManager
- Inherits:
-
Object
- Object
- FileManager
- Defined in:
- app/models/file_manager.rb
Class Method Summary collapse
- .allowed_content_type?(type) ⇒ Boolean
-
.create_dir(parent_dir, dir_name) ⇒ Object
create dir.
- .create_file(parent_dir, filename) ⇒ Object
-
.remove_dir(fullpath) ⇒ Object
remove dir.
-
.remove_file(fullpath) ⇒ Object
remove file.
-
.rename_dir(fullpath, new_name) ⇒ Object
rename dir.
-
.rename_file(fullpath, new_name) ⇒ Object
rename file.
- .save_file(file_name, content) ⇒ Object
- .unzip_file(file) ⇒ Object
Instance Method Summary collapse
- #dirs(path = ".") ⇒ Object
- #files(path = ".") ⇒ Object
-
#initialize(root, parent = nil) ⇒ FileManager
constructor
A new instance of FileManager.
Constructor Details
#initialize(root, parent = nil) ⇒ FileManager
Returns a new instance of FileManager.
3 4 5 6 |
# File 'app/models/file_manager.rb', line 3 def initialize(root, parent=nil) @root = root @parent = parent end |
Class Method Details
.allowed_content_type?(type) ⇒ Boolean
190 191 192 193 |
# File 'app/models/file_manager.rb', line 190 def self.allowed_content_type?(type) return true unless Editable.mime_for(".#{type}").eql?('unknown_type') return false if Editable.mime_for(".#{type}").eql?('unknown_type') end |
.create_dir(parent_dir, dir_name) ⇒ Object
create dir
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'app/models/file_manager.rb', line 159 def self.create_dir(parent_dir, dir_name) slugged_name = slugify(dir_name) path = File.join(Rails.root, 'themes', Refinery::Themes::Theme.current_theme_key, parent_dir) create_path = File.join(path, slugged_name) if self.secured_path?(path) && !File.exist?(create_path) && FileUtils.mkdir(create_path) result = {:status => 1, :fullpath => File.join(parent_dir, slugged_name), :notice => ('Directory "%s" was successfully created' % slugged_name), :node_name => slugged_name } else result = {:notice => 'Directory "%s" could not be created' % slugged_name} end result end |
.create_file(parent_dir, filename) ⇒ Object
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 |
# File 'app/models/file_manager.rb', line 35 def self.create_file(parent_dir, filename) content_type = Editable.mime_for filename target_name = self.slugify(filename) if filename_ext = File.extname(filename) target_name = self.slugify(filename[0..-(filename_ext.length+1)]) + filename_ext end return {:notice => "Files of this type (#{filename_ext}) are not allowed!"} unless self.allowed_content_type?(filename_ext) target = File.join(Rails.root, 'themes', Refinery::Themes::Theme.current_theme_key, parent_dir, target_name) begin File.open(target, 'w+') do |file| file.write('') end rescue SystemCallError => boom return {:notice => "Error: #{boom}"} end if File.exist?(target) result = {:status => 1, :fullpath => File.join(parent_dir, target_name), :notice => ('File "%s" was successfully added' % File.basename(target_name)), :node_name => target_name } else result = {:notice => 'File "%s" could not be uploaded' % File.basename(target_name)} end result end |
.remove_dir(fullpath) ⇒ Object
remove dir
83 84 85 86 87 88 89 90 |
# File 'app/models/file_manager.rb', line 83 def self.remove_dir(fullpath) begin Dir.rmdir(File.join(Rails.root, 'themes', Refinery::Themes::Theme.current_theme_key, fullpath)) return {:status => 1, :notice => ('Directory "%s" was successfully removed' % fullpath)} rescue SystemCallError => boom return {:notice => "Error: #{boom}"} end end |
.remove_file(fullpath) ⇒ Object
remove file
93 94 95 96 97 98 99 100 101 |
# File 'app/models/file_manager.rb', line 93 def self.remove_file(fullpath) begin File.delete(File.join(Rails.root, 'themes', Refinery::Themes::Theme.current_theme_key, fullpath)) notice = 'File "%s" was successfully removed' % fullpath return {:status => 1, :notice => notice} rescue SystemCallError => boom return {:notice => "Error: #{boom}"} end end |
.rename_dir(fullpath, new_name) ⇒ Object
rename dir
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/models/file_manager.rb', line 105 def self.rename_dir(fullpath, new_name) target_name = slugify(new_name) segments = fullpath.split("/") segments.pop dir_path = segments.join('/') begin File.rename(File.join(Rails.root, 'themes', Refinery::Themes::Theme.current_theme_key, fullpath), File.join(Rails.root, 'themes', Refinery::Themes::Theme.current_theme_key, dir_path, target_name)) rescue SystemCallError => boom return {:notice => "Error: #{boom}"} end { :status => 1, :fullpath => File.join(dir_path, target_name), :notice => ('Directory "%s" was successfully renamed' % target_name), :node_name => target_name } end |
.rename_file(fullpath, new_name) ⇒ Object
rename file
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'app/models/file_manager.rb', line 129 def self.rename_file(fullpath, new_name) content_type = Editable.mime_for new_name target_name = self.slugify(new_name) if filename_ext = File.extname(new_name) target_name = self.slugify(new_name[0..-(filename_ext.length+1)]) + filename_ext end unless FileManager.allowed_content_type?(filename_ext) return {:notice => "Files of this type (#{filename_ext}) are not allowed!"} end dir_path = File.dirname(fullpath) begin File.rename(File.join(Rails.root, 'themes', Refinery::Themes::Theme.current_theme_key, fullpath), File.join(Rails.root, 'themes',Refinery::Themes::Theme.current_theme_key, dir_path, target_name)) rescue SystemCallError => boom return {:notice => "Error: #{boom}"} end { :status => 1, :fullpath => File.join(dir_path, target_name), :notice => ('File "%s" was successfully renamed' % File.basename(target_name)), :node_name => target_name } end |
.save_file(file_name, content) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/models/file_manager.rb', line 67 def self.save_file(file_name, content) file = File.join(Rails.root, "themes", Refinery::Themes::Theme.current_theme_key, file_name) begin File.open(file, 'w+b') do |f| f.write(content) end rescue SystemCallError => boom return {:notice => "Error: #{boom}"}, :layout => false end File.read(file) end |
.unzip_file(file) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 |
# File 'app/models/file_manager.rb', line 178 def self.unzip_file (file) require 'zip/zip' Zip::ZipFile.open(file) { |zip_file| zip_file.each { |f| f_path=File.join(Rails.root.join('themes'), f.name) FileUtils.mkdir_p(File.dirname(f_path)) zip_file.extract(f, f_path) unless File.exist?(f_path) } } end |
Instance Method Details
#dirs(path = ".") ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'app/models/file_manager.rb', line 8 def dirs(path=".") path = "" if path.nil? @path = File.join(File.(@root), path) if File.exists?(@path) Dir.entries(@path).sort {|a,b| a <=> b}.inject([]) do |ary, folder| ary << { :attr => { :fullpath => File.join(@parent, folder), :rel => 'folder'}, :data => folder, :state => Dir[Rails.root.join("themes/#{Refinery::Themes::Theme.current_theme_key}/#{@parent}/#{folder}/*")].empty? ? 'leaf' : 'closed' } if File.directory?(File.join(@path, folder)) && folder[0,1] != "." ary end end end |
#files(path = ".") ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'app/models/file_manager.rb', line 23 def files(path=".") path = "" if path.nil? @path = File.join(File.(@root), path) if File.exists?(@path) Dir.entries(@path).sort {|a,b| a <=> b}.inject([]) do |ary, file| ary << {:attr => {:fullpath => "#{@parent}/#{file}", :rel => 'default'}, :data => file, :state => 'leaf'} if File.file?(File.join(@path, file)) ary end end end |