MDL-32639 files: Improved File Manager download and zip support

This commit is contained in:
Frederic Massart 2012-11-06 14:44:56 +08:00
parent 697ade2850
commit 638d72cd0b
6 changed files with 46 additions and 26 deletions

View file

@ -463,23 +463,36 @@ function file_rewrite_pluginfile_urls($text, $file, $contextid, $component, $fil
* @global stdClass $CFG * @global stdClass $CFG
* @global stdClass $USER * @global stdClass $USER
* @param int $draftitemid the draft area item id. * @param int $draftitemid the draft area item id.
* @param string $filepath path to the directory from which the information have to be retrieved.
* @return array with the following entries: * @return array with the following entries:
* 'filecount' => number of files in the draft area. * 'filecount' => number of files in the draft area.
* 'filesize' => total size of the files in the draft area.
* 'foldercount' => number of folders in the draft area.
* (more information will be added as needed). * (more information will be added as needed).
*/ */
function file_get_draft_area_info($draftitemid) { function file_get_draft_area_info($draftitemid, $filepath = '/') {
global $CFG, $USER; global $CFG, $USER;
$usercontext = context_user::instance($USER->id); $usercontext = context_user::instance($USER->id);
$fs = get_file_storage(); $fs = get_file_storage();
$results = array(); $results = array(
'filecount' => 0,
'foldercount' => 0,
'filesize' => 0
);
// The number of files if ($filepath != '/') {
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', false); $draftfiles = $fs->get_directory_files($usercontext->id, 'user', 'draft', $draftitemid, $filepath, true, true);
$results['filecount'] = count($draftfiles); } else {
$results['filesize'] = 0; $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', true);
}
foreach ($draftfiles as $file) { foreach ($draftfiles as $file) {
if ($file->is_directory()) {
$results['foldercount'] += 1;
} else {
$results['filecount'] += 1;
}
$results['filesize'] += $file->get_filesize(); $results['filesize'] += $file->get_filesize();
} }

View file

@ -137,6 +137,12 @@ class zip_packer extends file_packer {
} }
} }
// We can consider that there was an error if the file generated does not contain anything.
if ($ziparch->count() == 0) {
$result = false;
debugging("Nothing was added to the zip file", DEBUG_DEVELOPER);
}
return ($ziparch->close() && $result); return ($ziparch->close() && $result);
} }

View file

@ -220,9 +220,9 @@ M.form_filemanager.init = function(Y, options) {
params: {'filepath':filepath}, params: {'filepath':filepath},
callback: function(id, obj, args) { callback: function(id, obj, args) {
scope.filecount = obj.filecount; scope.filecount = obj.filecount;
scope.check_buttons();
scope.options = obj; scope.options = obj;
scope.lazyloading = {}; scope.lazyloading = {};
scope.check_buttons();
scope.render(obj); scope.render(obj);
} }
}, true); }, true);

View file

@ -219,7 +219,7 @@ switch ($action) {
$file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.'); $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
$parent_path = $file->get_parent_directory()->get_filepath(); $parent_path = $file->get_parent_directory()->get_filepath();
$filepath = explode('/', trim($file->get_filepath(), '/')); $filepath = explode('/', trim($file->get_filepath(), '/'));
$filepath = array_pop($filepath); $filepath = array_pop($filepath);
@ -237,19 +237,18 @@ switch ($action) {
$zipper = get_file_packer('application/zip'); $zipper = get_file_packer('application/zip');
$fs = get_file_storage(); $fs = get_file_storage();
$area = file_get_draft_area_info($draftid); $area = file_get_draft_area_info($draftid, $filepath);
if ($area['filecount'] == 0) { if ($area['filecount'] == 0 && $area['foldercount'] == 0) {
echo json_encode(false); echo json_encode(false);
die; die;
} }
$stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.'); $stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
if ($filepath === '/') { if ($filepath === '/') {
$parent_path = '/';
$filename = get_string('files').'.zip'; $filename = get_string('files').'.zip';
} else { } else {
$parent_path = $stored_file->get_parent_directory()->get_filepath(); $filename = explode('/', trim($filepath, '/'));
$filename = trim($filepath, '/').'.zip'; $filename = array_pop($filename) . '.zip';
} }
// archive compressed file to an unused draft area // archive compressed file to an unused draft area
@ -257,7 +256,7 @@ switch ($action) {
if ($newfile = $zipper->archive_to_storage(array('/' => $stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) { if ($newfile = $zipper->archive_to_storage(array('/' => $stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
$return = new stdClass(); $return = new stdClass();
$return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out(); $return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
$return->filepath = $parent_path; $return->filepath = $filepath;
echo json_encode($return); echo json_encode($return);
} else { } else {
echo json_encode(false); echo json_encode(false);

View file

@ -139,17 +139,17 @@ case 'downloaddir':
$zipper = new zip_packer(); $zipper = new zip_packer();
$file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.'); $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
if ($file->get_parent_directory()) { if ($draftpath === '/') {
$parent_path = $file->get_parent_directory()->get_filepath();
$filename = trim($draftpath, '/').'.zip';
} else {
$parent_path = '/';
$filename = get_string('files').'.zip'; $filename = get_string('files').'.zip';
} else {
$filename = explode('/', trim($draftpath, '/'));
$filename = array_pop($filename) . '.zip';
} }
if ($newfile = $zipper->archive_to_storage(array('/' => $file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id)) { $newdraftitemid = file_get_unused_draft_itemid();
$fileurl = moodle_url::make_draftfile_url($itemid, '/', $filename)->out(); if ($newfile = $zipper->archive_to_storage(array('/' => $file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
header('Location: ' . $fileurl ); $fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
header('Location: ' . $fileurl);
} else { } else {
print_error('cannotdownloaddir', 'repository'); print_error('cannotdownloaddir', 'repository');
} }
@ -271,7 +271,7 @@ default:
$path = '/' . trim($draftpath, '/') . '/'; $path = '/' . trim($draftpath, '/') . '/';
$parts = explode('/', $path); $parts = explode('/', $path);
foreach ($parts as $part) { foreach ($parts as $part) {
if (!empty($part)) { if ($part != '') {
$trail .= ('/'.$part.'/'); $trail .= ('/'.$part.'/');
$data->path[] = array('name'=>$part, 'path'=>$trail); $data->path[] = array('name'=>$part, 'path'=>$trail);
$home_url->param('draftpath', $trail); $home_url->param('draftpath', $trail);
@ -297,8 +297,10 @@ default:
$home_url->param('action', 'mkdirform'); $home_url->param('action', 'mkdirform');
echo ' <a href="'.$home_url->out().'">'.get_string('makeafolder', 'moodle').'</a>'; echo ' <a href="'.$home_url->out().'">'.get_string('makeafolder', 'moodle').'</a>';
} }
$home_url->param('action', 'downloaddir'); if (!empty($files->list)) {
echo html_writer::link($home_url, get_string('downloadfolder', 'repository'), array('target'=>'_blank')); $home_url->param('action', 'downloaddir');
echo ' ' . html_writer::link($home_url, get_string('downloadfolder', 'repository'), array('target'=>'_blank'));
}
} }
echo '</div>'; echo '</div>';

View file

@ -268,7 +268,7 @@ a.ygtvspacer:hover {color: transparent;text-decoration: none;}
.filemanager.fm-loaded .filemanager-loading {display:none;} .filemanager.fm-loaded .filemanager-loading {display:none;}
.filemanager.fm-maxfiles .fp-btn-add {display:none;} .filemanager.fm-maxfiles .fp-btn-add {display:none;}
.filemanager.fm-maxfiles .dndupload-message {display:none;} .filemanager.fm-maxfiles .dndupload-message {display:none;}
.filemanager.fm-nofiles .fp-btn-download {display:none;} .filemanager.fm-noitems .fp-btn-download {display:none;}
.filemanager .fm-empty-container {display:none;} .filemanager .fm-empty-container {display:none;}
.filemanager.fm-noitems .filemanager-container .fp-content {display:none;} .filemanager.fm-noitems .filemanager-container .fp-content {display:none;}
.filemanager .filemanager-updating {display:none;text-align:center;} .filemanager .filemanager-updating {display:none;text-align:center;}