Skip to content

Commit

Permalink
MDL-39177 Perform all checks on draftfile area before saving the files
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed May 14, 2013
1 parent 7627b99 commit 02890b9
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions lib/filelib.php
Expand Up @@ -741,11 +741,29 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea
// we change time modified for all new and changed files, we keep time created as is

$newhashes = array();
$filecount = 0;
foreach ($draftfiles as $file) {
if (!$options['subdirs'] && ($file->get_filepath() !== '/' or $file->is_directory())) {
continue;
}
if (!$allowreferences && $file->is_external_file()) {
continue;
}
if (!$file->is_directory()) {
if ($options['maxbytes'] and $options['maxbytes'] < $file->get_filesize()) {
// oversized file - should not get here at all
continue;
}
if ($options['maxfiles'] != -1 and $options['maxfiles'] <= $filecount) {
// more files - should not get here at all
continue;
}
$filecount++;
}
$newhash = $fs->get_pathname_hash($contextid, $component, $filearea, $itemid, $file->get_filepath(), $file->get_filename());
$newhashes[$newhash] = $file;
}
$filecount = 0;

// Loop through oldfiles and decide which we need to delete and which to update.
// After this cycle the array $newhashes will only contain the files that need to be added.
foreach ($oldfiles as $oldfile) {
Expand Down Expand Up @@ -812,9 +830,6 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea
$oldfile->set_timemodified($newfile->get_timemodified());
}

if ($newfile->is_external_file() && !$allowreferences) {
continue;
}
// Replaced file content
if (!$oldfile->is_directory() &&
($oldfile->get_contenthash() != $newfile->get_contenthash() ||
Expand All @@ -827,9 +842,6 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea

// unchanged file or directory - we keep it as is
unset($newhashes[$oldhash]);
if (!$oldfile->is_directory()) {
$filecount++;
}
}

// Add fresh file or the file which has changed status
Expand All @@ -840,27 +852,8 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea
// Field files.source for draftarea files contains serialised object with source and original information.
$file_record['source'] = $source->source;
}
if (!$options['subdirs']) {
if ($file->get_filepath() !== '/' or $file->is_directory()) {
continue;
}
}
if ($options['maxbytes'] and $options['maxbytes'] < $file->get_filesize()) {
// oversized file - should not get here at all
continue;
}
if ($options['maxfiles'] != -1 and $options['maxfiles'] <= $filecount) {
// more files - should not get here at all
break;
}
if (!$file->is_directory()) {
$filecount++;
}

if ($file->is_external_file()) {
if (!$allowreferences) {
continue;
}
$repoid = $file->get_repository_id();
if (!empty($repoid)) {
$file_record['repositoryid'] = $repoid;
Expand Down

0 comments on commit 02890b9

Please sign in to comment.