From 1311e4dd0055c45bca9024f42edbc75e41ff75f3 Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Tue, 27 Jul 2010 03:10:59 +0000 Subject: [PATCH] MDL-23268, throw error message instead of deleting existing file --- lang/en/error.php | 1 + repository/lib.php | 2 +- repository/local/lib.php | 5 ++++- repository/recent/lib.php | 2 +- repository/upload/lib.php | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lang/en/error.php b/lang/en/error.php index 5a43c94bd78..445744bbb53 100755 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -219,6 +219,7 @@ $string['errorunzippingfiles'] = 'Error unzipping files'; $string['expiredkey'] = 'Expired key'; $string['failtoloadblocks'] = 'One or more blocks are registered in the database, but they all failed to load!'; $string['fieldrequired'] = '"{$a}" is a required field'; +$string['fileexists'] = 'File exists'; $string['filemismatch'] = 'Non-core file name mismatch. The file "{$a->current}" should be {$a->file}'; $string['filenotfound'] = 'Sorry, the requested file could not be found'; $string['filenotreadable'] = 'File is not readable'; diff --git a/repository/lib.php b/repository/lib.php index b266ba88de8..50106de1ff9 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -829,7 +829,7 @@ abstract class repository { } $fs = get_file_storage(); if ($existingfile = $fs->get_file($context->id, $record->component, $record->filearea, $record->itemid, $record->filepath, $record->filename)) { - $existingfile->delete(); + throw new moodle_exception('fileexists'); } if ($file = $fs->create_file_from_pathname($record, $thefile)) { if (empty($CFG->repository_no_delete)) { diff --git a/repository/local/lib.php b/repository/local/lib.php index a36f77529e5..a36f759fe45 100755 --- a/repository/local/lib.php +++ b/repository/local/lib.php @@ -197,9 +197,12 @@ class repository_local extends repository { $filepath = clean_param($params['filepath'], PARAM_PATH);; $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT); $component = clean_param($params['component'], PARAM_ALPHAEXT); - $context = get_context_instance_by_id($contextid); + if ($existingfile = $fs->get_file($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename)) { + throw new moodle_exception('fileexists'); + } + $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename); $file_info->copy_to_storage($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename); diff --git a/repository/recent/lib.php b/repository/recent/lib.php index 01de2ae9d89..9f6315a3606 100755 --- a/repository/recent/lib.php +++ b/repository/recent/lib.php @@ -188,7 +188,7 @@ class repository_recent extends repository { $file_record = array('contextid'=>$user_context->id, 'component'=>'user', 'filearea'=>'draft', 'itemid'=>$draftitemid, 'filepath'=>$new_filepath, 'filename'=>$new_filename); if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename)) { - $file->delete(); + throw new moodle_exception('fileexists'); } $fs->create_file_from_storedfile($file_record, $stored_file); } diff --git a/repository/upload/lib.php b/repository/upload/lib.php index 2910a8ba454..bccaf344743 100755 --- a/repository/upload/lib.php +++ b/repository/upload/lib.php @@ -104,7 +104,7 @@ class repository_upload extends repository { } if ($file = $fs->get_file($context->id, $record->component, $record->filearea, $record->itemid, $record->filepath, $record->filename)) { - throw new moodle_exception('fileexist'); + throw new moodle_exception('fileexists'); } $record->contextid = $context->id;