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
This commit is contained in:
+10
-1
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user