From fa3108c807b18bc22a78e5e26075bfdf4e0ffbda Mon Sep 17 00:00:00 2001 From: Cameron Sutter Date: Thu, 15 Dec 2016 13:56:48 -0700 Subject: [PATCH] zip up course files closes OFFW-49 test plan: - Sign in as a user in a course that has Offline Web Downloads turned on - navigate to the modules page - Click on 'Download Course Content' - go to the Rails Console - type: `Attachment.all.last.id` - take the ID it spits out - request that file through the API - ** if the URL attribute has epub_export/:id/ before files/:id/... then delete that part (expub_export/:id/) so that it reads something like localhost:3000/files/:id/download?download_frd=blahblah - in a course without any course files, export the course content and you should get an empty zip file (you need to do all the stuff above) Change-Id: I91fcbf35a1c482eab476dbcf4565f3499453253a Reviewed-on: https://gerrit.instructure.com/97849 Tested-by: Jenkins Reviewed-by: Mysti Sadler QA-Review: Nathan Rogowski Product-Review: Cameron Sutter --- app/models/web_zip_export.rb | 13 +++++++++---- lib/cc/exporter/web_zip/exportable.rb | 4 ++-- lib/cc/exporter/web_zip/zip_package.rb | 17 +++++++++++++++++ spec/models/web_zip_export_spec.rb | 9 +++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/app/models/web_zip_export.rb b/app/models/web_zip_export.rb index 6c16b208025..7708ce8c104 100644 --- a/app/models/web_zip_export.rb +++ b/app/models/web_zip_export.rb @@ -1,4 +1,5 @@ class WebZipExport < EpubExport + include CC::Exporter::WebZip::Exportable def export MustViewModuleProgressor.new(user, course).make_progress @@ -12,19 +13,23 @@ class WebZipExport < EpubExport end handle_asynchronously :generate, priority: Delayed::LOW_PRIORITY, max_attempts: 1 + # WebZip Exportable overrides + def content_cartridge + self.content_export.attachment + end + def convert_to_offline_web_zip begin set_locale - # TODO: connect to cameron's patchset - # file_path = super - file_path = "/Users/mysti/canvas/chaos.mov.zip" + file_path = super I18n.locale = :en + + create_attachment_from_path!(file_path) rescue => e mark_as_failed raise e end - create_attachment_from_path!(file_path) mark_as_generated cleanup_file_path!(file_path) end diff --git a/lib/cc/exporter/web_zip/exportable.rb b/lib/cc/exporter/web_zip/exportable.rb index 5679860f2d2..44931ab3b4b 100644 --- a/lib/cc/exporter/web_zip/exportable.rb +++ b/lib/cc/exporter/web_zip/exportable.rb @@ -7,9 +7,9 @@ module CC::Exporter::WebZip def convert_to_offline_web_zip exporter = CC::Exporter::WebZip::Exporter.new(content_cartridge.open, false) zip = CC::Exporter::WebZip::ZipPackage.new(exporter) - result = zip.create + file_path = zip.create || zip.empty_zip_file exporter.cleanup_files - result + file_path end end end \ No newline at end of file diff --git a/lib/cc/exporter/web_zip/zip_package.rb b/lib/cc/exporter/web_zip/zip_package.rb index 02f67cf4ebf..8a96bd6843c 100644 --- a/lib/cc/exporter/web_zip/zip_package.rb +++ b/lib/cc/exporter/web_zip/zip_package.rb @@ -1,5 +1,22 @@ module CC::Exporter::WebZip class ZipPackage < CC::Exporter::Epub::FilesDirectory + def initialize(exporter) + @files = exporter.unsupported_files + exporter.cartridge_json[:files] + @filename_prefix = exporter.filename_prefix + end + attr_reader :files + + def empty_zip_file + zip_file = Zip::File.new( + File.join(export_directory, filename), + Zip::File::CREATE + ) + tempfile = 'empty.txt' + f = File.new(tempfile, "w+") + zip_file.add(tempfile, f) { f.close } + zip_file.close + zip_file.name + end end end \ No newline at end of file diff --git a/spec/models/web_zip_export_spec.rb b/spec/models/web_zip_export_spec.rb index 2e8b10d14af..486d8f9956b 100644 --- a/spec/models/web_zip_export_spec.rb +++ b/spec/models/web_zip_export_spec.rb @@ -24,5 +24,14 @@ describe WebZipExport do web_zip_export.generate_without_send_later expect(web_zip_export.generating?).to be_truthy end + + it 'should create and associate an attachment' do + web_zip_export.export_without_send_later + web_zip_export.content_export.export_without_send_later + expect(web_zip_export.zip_attachment).to be_nil, 'precondition' + expect{web_zip_export.convert_to_offline_web_zip_without_send_later}.to change{Attachment.count}.by(1) + web_zip_export.reload + expect(web_zip_export.zip_attachment).not_to be_nil + end end end