Feature #7056 » download_all_fix.patch
app/controllers/attachments_controller.rb (working copy) | ||
---|---|---|
137 | 137 |
end |
138 | 138 | |
139 | 139 |
def download_all |
140 |
Tempfile.create('attachments_zip-', Rails.root.join('tmp')) do |tempfile| |
|
141 |
zip_file = Attachment.archive_attachments(tempfile, @attachments) |
|
142 |
if zip_file |
|
143 |
send_data( |
|
144 |
File.read(zip_file.path), |
|
145 |
:type => 'application/zip', |
|
146 |
:filename => "#{@container.class.to_s.downcase}-#{@container.id}-attachments.zip") |
|
147 |
else |
|
148 |
render_404 |
|
149 |
end |
|
140 |
tempfile_path = Rails.root.join('tmp', "attachments_zip-#{SecureRandom.hex(8)}.zip") |
|
141 |
zip_file = Attachment.archive_attachments(tempfile_path, @attachments) |
|
142 |
if zip_file |
|
143 |
send_data( |
|
144 |
File.read(zip_file), |
|
145 |
:type => 'application/zip', |
|
146 |
:filename => "#{@container.class.to_s.downcase}-#{@container.id}-attachments.zip") |
|
147 |
else |
|
148 |
render_404 |
|
150 | 149 |
end |
150 |
ensure |
|
151 |
FileUtils.rm_rf(tempfile_path) |
|
151 | 152 |
end |
152 | 153 | |
153 | 154 |
def update |
app/models/attachment.rb (working copy) | ||
---|---|---|
352 | 352 | |
353 | 353 |
Zip.unicode_names = true |
354 | 354 |
archived_file_names = [] |
355 |
Zip::File.open(out_file.path, Zip::File::CREATE) do |zip|
|
|
355 |
Zip::File.open(out_file, Zip::File::CREATE) do |zip| |
|
356 | 356 |
attachments.each do |attachment| |
357 | 357 |
filename = attachment.filename |
358 | 358 |
# rename the file if a file with the same name already exists |
test/unit/attachment_test.rb (working copy) | ||
---|---|---|
280 | 280 | |
281 | 281 |
def test_archive_attachments |
282 | 282 |
attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1) |
283 |
Tempfile.create('attachments_zip', Rails.root.join('tmp')) do |tempfile|
|
|
284 |
zip_file = Attachment.archive_attachments(tempfile, [attachment]) |
|
285 |
assert_instance_of File, zip_file
|
|
283 |
with_tempfile_path do |tempfile_path|
|
|
284 |
zip_file = Attachment.archive_attachments(tempfile_path, [attachment])
|
|
285 |
assert_instance_of Pathname, zip_file
|
|
286 | 286 |
end |
287 | 287 |
end |
288 | 288 | |
289 | 289 |
def test_archive_attachments_without_attachments |
290 |
Tempfile.create('attachments_zip', Rails.root.join('tmp')) do |tempfile|
|
|
291 |
zip_file = Attachment.archive_attachments(tempfile, []) |
|
290 |
with_tempfile_path do |tempfile_path|
|
|
291 |
zip_file = Attachment.archive_attachments(tempfile_path, [])
|
|
292 | 292 |
assert_nil zip_file |
293 | 293 |
end |
294 | 294 |
end |
... | ... | |
296 | 296 |
def test_archive_attachments_should_rename_duplicate_file_names |
297 | 297 |
attachment1 = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1) |
298 | 298 |
attachment2 = Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1) |
299 |
Tempfile.create('attachments_zip', Rails.root.join('tmp')) do |tempfile|
|
|
300 |
zip_file = Attachment.archive_attachments(tempfile, [attachment1, attachment2]) |
|
301 |
Zip::File.open(zip_file.path) do |z|
|
|
299 |
with_tempfile_path do |tempfile_path|
|
|
300 |
zip_file = Attachment.archive_attachments(tempfile_path, [attachment1, attachment2])
|
|
301 |
Zip::File.open(zip_file) do |z| |
|
302 | 302 |
assert_equal ['testfile.txt', 'testfile(1).txt'], z.map(&:name) |
303 | 303 |
end |
304 | 304 |
end |
... | ... | |
571 | 571 |
assert_equal expected, attachment.is_text?, attachment.inspect |
572 | 572 |
end |
573 | 573 |
end |
574 | ||
575 |
private |
|
576 | ||
577 |
def with_tempfile_path |
|
578 |
tempfile_path = Rails.root.join('tmp', "attachments_zip-#{SecureRandom.hex(8)}.zip") |
|
579 |
yield(tempfile_path) |
|
580 |
ensure |
|
581 |
FileUtils.rm_rf(tempfile_path) |
|
582 |
end |
|
574 | 583 |
end |