83 lines
2.9 KiB
Ruby
83 lines
2.9 KiB
Ruby
class FileManagerTrash
|
|
include Mongoid::Document
|
|
include Mongoid::Timestamps
|
|
Restrict_dirs = ["/", "/home/", "/etc/", "/var/*/", "/root/", "/usr/*/"]
|
|
field :is_file, type: Boolean, default: true
|
|
field :module, type: String, default: ""
|
|
field :path, type: String, default: ""
|
|
field :trash_path, type: String, default: ""
|
|
field :user_id
|
|
field :is_slave, type: Boolean, default: false
|
|
field :record_only, type: Boolean, default: false
|
|
belongs_to :file_manager_upload, index: true
|
|
belongs_to :file_manager_setting
|
|
index({module: 1}, { unique: false, background: true })
|
|
index({created_at: -1}, { unique: false, background: true })
|
|
before_create do
|
|
if self.trash_path.blank?
|
|
self.trash_path = Pathname.new("#{FileManagerRoot::RootPath}").join(self.file_manager_setting && self.file_manager_setting.class == FileManagerSetting ? self.file_manager_setting.root_path : '').join(".trash/#{self.file_manager_setting_id}/#{self.id}/#{File.basename(self.path)}")
|
|
trash_dir = File.dirname(self.trash_path)
|
|
FileUtils.mkdir_p(trash_dir)
|
|
upload = self.file_manager_upload
|
|
if File.exist?(upload.get_real_path)
|
|
FileUtils.mv(upload.get_real_path, self.trash_path)
|
|
end
|
|
end
|
|
end
|
|
before_destroy do
|
|
upload = self.file_manager_upload
|
|
if upload
|
|
if upload.is_default
|
|
upload.remove_file_callback
|
|
end
|
|
if self.record_only
|
|
upload.destroy
|
|
else
|
|
FileManagerUpload.where(:path=> upload.path).destroy
|
|
end
|
|
end
|
|
true
|
|
end
|
|
def get_version
|
|
return (self.file_manager_upload ? self.file_manager_upload.version : 1)
|
|
end
|
|
def delete_file
|
|
self.class.delete_file_permanent(self.path, File.dirname(self.trash_path), self.record_only)
|
|
unless self.is_file
|
|
self.class.where(:trash_path=>/^#{::Regexp.escape(self.trash_path)}/,:is_slave=>true).destroy
|
|
end
|
|
self.destroy
|
|
end
|
|
def self.check_restricted(path)
|
|
return Restrict_dirs.map{|d| path.match("^#{d.gsub('/*/','/.*')}[^/]+(/|)$")}.compact.length > 0
|
|
end
|
|
def self.delete_file_permanent(path, trash_path=nil, only_self=false)
|
|
unless self.check_restricted(path)
|
|
dir = File.dirname(path)
|
|
basename = File.basename(path)
|
|
if trash_path
|
|
FileUtils.rm_rf(trash_path)
|
|
else
|
|
FileUtils.rm_rf(path)
|
|
end
|
|
unless only_self
|
|
Dir.glob("#{dir}/.versions/*/#{basename}").each{|f| FileUtils.rm_rf(f)}
|
|
end
|
|
else
|
|
puts "Path: #{path}"
|
|
puts "File or Directory restricted!"
|
|
end
|
|
end
|
|
def recover_file
|
|
if File.exist?(self.trash_path)
|
|
dir = File.dirname(self.path)
|
|
FileUtils.mkdir_p(dir)
|
|
FileUtils.mv(self.trash_path, self.path)
|
|
end
|
|
self.file_manager_upload.update(:is_trash=>false) if self.file_manager_upload
|
|
unless self.is_file
|
|
self.class.where(:trash_path=>/^#{::Regexp.escape(self.trash_path)}/,:is_slave=>true).delete
|
|
end
|
|
self.delete
|
|
end
|
|
end |