Skip to content

Commit

Permalink
MDL-68481 mod_folder: stream files when creating zip
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Dec 12, 2020
1 parent 3b68723 commit 9fff03a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
29 changes: 19 additions & 10 deletions mod/folder/download_folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,29 @@
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();
3 changes: 2 additions & 1 deletion mod/folder/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public function display_folder(stdClass $folder) {
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;
Expand Down

0 comments on commit 9fff03a

Please sign in to comment.