From ae55874269b2c2ffc1619e7974bbdd1c92f038e7 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Wed, 8 May 2013 14:37:29 +1000 Subject: [PATCH] MDL-39177 Original info is not preserved when overwriting a file in filemanager original file location is used by filemanager to indicate that file is a source of reference and file the list of referencing files --- repository/lib.php | 11 ++++++++++- repository/upload/lib.php | 15 ++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/repository/lib.php b/repository/lib.php index a1c38600b2c..30562a0ea21 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -2410,9 +2410,10 @@ abstract class repository { $user_context = get_context_instance(CONTEXT_USER, $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')); } @@ -2421,6 +2422,14 @@ abstract class repository { $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; diff --git a/repository/upload/lib.php b/repository/upload/lib.php index 72799babe5b..9b13775f93f 100644 --- a/repository/upload/lib.php +++ b/repository/upload/lib.php @@ -194,13 +194,14 @@ class repository_upload extends repository { $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; @@ -214,10 +215,10 @@ class repository_upload extends repository { $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,