Browse files

MDL-39177 Original info is not preserved when overwriting a file in f…

…ilemanager

original file location is used by filemanager to indicate that file is a source of reference and file the list of referencing files
  • Loading branch information...
1 parent d50f747 commit d262212a1f21bb5165b67a8ccfd086a421e11da2 @marinaglancy marinaglancy committed with stronk7 May 8, 2013
Showing with 18 additions and 8 deletions.
  1. +10 −1 repository/lib.php
  2. +8 −7 repository/upload/lib.php
View
11 repository/lib.php
@@ -2546,9 +2546,10 @@ public static function overwrite_existing_draftfile($itemid, $filepath, $filenam
$user_context = context_user::instance($USER->id);
if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $filepath, $filename)) {
if ($tempfile = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $newfilepath, $newfilename)) {
+ // Remember original file source field.
+ $source = @unserialize($file->get_source());
if ($tempfile->is_external_file()) {
// New file is a reference. Check that existing file does not have any other files referencing to it
- $source = @unserialize($file->get_source());
if (isset($source->original) && $fs->search_references_count($source->original)) {
return (object)array('error' => get_string('errordoublereference', 'repository'));
}
@@ -2557,6 +2558,14 @@ public static function overwrite_existing_draftfile($itemid, $filepath, $filenam
$file->delete();
// create new file
$newfile = $fs->create_file_from_storedfile(array('filepath'=>$filepath, 'filename'=>$filename), $tempfile);
+ // Preserve original file location (stored in source field) for handling references
+ if (isset($source->original)) {
+ if (!($newfilesource = @unserialize($newfile->get_source()))) {
+ $newfilesource = new stdClass();
+ }
+ $newfilesource->original = $source->original;
+ $newfile->set_source(serialize($newfilesource));
+ }
// remove temp file
$tempfile->delete();
return true;
View
15 repository/upload/lib.php
@@ -202,13 +202,14 @@ public function process_upload($saveas_filename, $maxbytes, $types = '*', $savep
$record->userid = $USER->id;
if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
+ $existingfilename = $record->filename;
+ $unused_filename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename);
+ $record->filename = $unused_filename;
+ $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
if ($overwriteexisting) {
- repository::delete_tempfile_from_draft($record->itemid, $record->filepath, $record->filename);
+ repository::overwrite_existing_draftfile($record->itemid, $record->filepath, $existingfilename, $record->filepath, $record->filename);
+ $record->filename = $existingfilename;
} else {
- $existingfilename = $record->filename;
- $unused_filename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename);
- $record->filename = $unused_filename;
- $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
$event = array();
$event['event'] = 'fileexists';
$event['newfile'] = new stdClass;
@@ -222,10 +223,10 @@ public function process_upload($saveas_filename, $maxbytes, $types = '*', $savep
$event['existingfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $existingfilename)->out(false);
return $event;
}
+ } else {
+ $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
}
- $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
-
return array(
'url'=>moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(false),
'id'=>$record->itemid,

0 comments on commit d262212

Please sign in to comment.