文件的删除限制加大

如果文件有了历史版本 数据记录不能被删除
如果文件被发送过,或者有了历史版本,硬盘上的文件和数据记录就不能被删除
This commit is contained in:
lizanle 2015-12-30 18:04:04 +08:00
parent 0ef0045941
commit c34127f21f
2 changed files with 7 additions and 2 deletions

View File

@ -349,11 +349,16 @@ class Attachment < ActiveRecord::Base
# Deletes the file from the file system if it's not referenced by other attachments
def delete_from_disk
if Attachment.where("disk_filename = ? AND id <> ?", disk_filename, id).empty?
#资源存在且历史记录为0 且 该资源没有存在任何拷贝才能删除资源
if Attachment.where("disk_filename = ? AND id <> ?", disk_filename, id).empty? && self.attachment_histories.count == 0 && Attachment.where('copy_from = ?',self.id) .count == 0
delete_from_disk!
end
end
def destroyable
Attachment.where("disk_filename = ? AND id <> ?", disk_filename, id).empty? && self.attachment_histories.count == 0 && Attachment.where('copy_from = ?',self.id) .count == 0
end
# Returns file's location on disk
def diskfile
File.join(self.class.storage_path, disk_directory.to_s, disk_filename.to_s)

View File

@ -52,7 +52,7 @@
<%end%>
<li>
<%= link_to( '删除资源', attachment_path(file),
:data => {:confirm => l(:text_are_you_sure)}, :method => :delete,:class => "postOptionLink") if (delete_allowed || User.current.id == file.author_id) && file.container_id == @course.id && file.container_type == "Course"%>
:data => {:confirm => l(:text_are_you_sure)}, :method => :delete,:class => "postOptionLink") if (delete_allowed || User.current.id == file.author_id) && file.container_id == @course.id && file.container_type == "Course" && file.destroyable %>
</li>
<% end %>