2013-10-22 09:02:48 +08:00
|
|
|
# encoding: utf-8
|
|
|
|
module FilesHelper
|
|
|
|
|
|
|
|
def downloadAll containers
|
|
|
|
paths = []
|
|
|
|
files = []
|
|
|
|
tmpfile = "tmp.zip"
|
|
|
|
|
|
|
|
containers.each do |container|
|
|
|
|
next if container.attachments.empty?
|
|
|
|
if container.is_a?(Version);end
|
|
|
|
container.attachments.each do |attachment|
|
|
|
|
paths << attachment.diskfile
|
|
|
|
file = attachment.diskfile
|
|
|
|
# logger.error "[FilesHelper] downloadAll: #{e}"
|
|
|
|
begin
|
|
|
|
File.new(file, "r")
|
|
|
|
rescue Exception => e
|
|
|
|
logger.error e
|
|
|
|
next
|
|
|
|
end
|
|
|
|
files << file
|
|
|
|
# zip.add(file.path.dup.sub(directory, ''), file.path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
zipfile_name = "archive.zip"
|
|
|
|
if File.exists? File.open(zipfile_name, "w+")
|
|
|
|
ff = File.open(zipfile_name, "w+")
|
|
|
|
ff.close
|
|
|
|
File.delete ff
|
|
|
|
end
|
|
|
|
Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile|
|
|
|
|
files.each do |filename|
|
|
|
|
directory = File.dirname filename
|
|
|
|
# Two arguments:
|
|
|
|
# - The name of the file as it will appear in the archive
|
|
|
|
# - The original file, including the path to find it
|
|
|
|
dir = filename.sub(directory+"/", '')
|
|
|
|
zipfile.add(dir, filename)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
File.new(zipfile_name,'w+')
|
|
|
|
end
|
|
|
|
|
2014-05-13 16:34:53 +08:00
|
|
|
# 判断指定的资源时候符合类型
|
|
|
|
def isTypeOk(attachment, type, contentType)
|
|
|
|
result = false
|
|
|
|
if type != 0
|
|
|
|
if attachment.attachtype == type
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
if result
|
2014-05-14 17:12:04 +08:00
|
|
|
if contentType != '0' && contentType != attachment.suffix_type
|
2014-05-13 16:34:53 +08:00
|
|
|
result = false
|
2014-05-14 17:12:04 +08:00
|
|
|
end
|
2014-05-13 16:34:53 +08:00
|
|
|
end
|
|
|
|
result
|
2014-05-12 18:46:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-10-22 09:02:48 +08:00
|
|
|
end
|