MDL-68481 mod_folder: stream files when creating zip
authorMark Nelson <mdjnelson@gmail.com>
Sat, 28 Nov 2020 18:42:18 +0000 (02:42 +0800)
committerMark Nelson <mdjnelson@gmail.com>
Sat, 12 Dec 2020 04:49:04 +0000 (12:49 +0800)
mod/folder/download_folder.php
mod/folder/renderer.php

index 53d0005..b3ed5b7 100644 (file)
@@ -40,20 +40,29 @@ if (!$downloadable) {
     print_error('cannotdownloaddir', 'repository');
 }
 
-folder_downloaded($folder, $course, $cm, $context);
-
 $fs = get_file_storage();
-$file = $fs->get_file($context->id, 'mod_folder', 'content', 0, '/', '.');
-if (!$file) {
+$files = $fs->get_area_files($context->id, 'mod_folder', 'content');
+if (empty($files)) {
     print_error('cannotdownloaddir', 'repository');
 }
 
-$zipper   = get_file_packer('application/zip');
+// Log zip as downloaded.
+folder_downloaded($folder, $course, $cm, $context);
+
+// Close the session.
+\core\session\manager::write_close();
+
 $filename = shorten_filename(clean_filename($folder->name . "-" . date("Ymd")) . ".zip");
-$temppath = make_request_directory() . $filename;
+$zipwriter = \core_files\archive_writer::get_stream_writer($filename, \core_files\archive_writer::ZIP_WRITER);
 
-if ($zipper->archive_to_pathname(array('/' => $file), $temppath)) {
-    send_temp_file($temppath, $filename);
-} else {
-    print_error('cannotdownloaddir', 'repository');
+foreach ($files as $file) {
+    if ($file->is_directory()) {
+        continue;
+    }
+    $pathinzip = $file->get_filepath() . $file->get_filename();
+    $zipwriter->add_file_from_stored_file($pathinzip, $file);
 }
+
+// Finish the archive.
+$zipwriter->finish();
+exit();
index 74732f4..91364b4 100644 (file)
@@ -72,7 +72,8 @@ class mod_folder_renderer extends plugin_renderer_base {
         if ($downloadable) {
             $downloadbutton = $this->output->single_button(
                 new moodle_url('/mod/folder/download_folder.php', array('id' => $cm->id)),
-                get_string('downloadfolder', 'folder')
+                get_string('downloadfolder', 'folder'),
+                'get'
             );
 
             $buttons .= $downloadbutton;