Allow rename/overwrite/do nothing if file exists when unzipping file

See BT#10887
pull/2458/head
jmontoyaa 8 years ago
parent d3a36eff3a
commit cf36145daa
  1. 19
      main/document/upload.php
  2. 8
      main/inc/lib/document.lib.php
  3. 121
      main/inc/lib/fileUpload.lib.php

@ -88,13 +88,13 @@ $htmlHeadXtra[] = '<script>
function check_unzip() { function check_unzip() {
if (document.upload.unzip.checked){ if (document.upload.unzip.checked){
document.upload.if_exists[0].disabled=true; //document.upload.if_exists[0].disabled=true;
document.upload.if_exists[1].checked=true; document.upload.if_exists[1].checked=true;
document.upload.if_exists[2].disabled=true; //document.upload.if_exists[2].disabled=true;
} else { } else {
document.upload.if_exists[0].checked=true; document.upload.if_exists[2].checked=true;
document.upload.if_exists[0].disabled=false; //document.upload.if_exists[0].disabled=false;
document.upload.if_exists[2].disabled=false; //document.upload.if_exists[2].disabled=false;
} }
} }
@ -205,7 +205,6 @@ if (!empty($_FILES)) {
); );
$redirectUrl = api_get_self().'?'.api_get_cidreq().$certificateLink; $redirectUrl = api_get_self().'?'.api_get_cidreq().$certificateLink;
if ($document_data) { if ($document_data) {
$redirectUrl .= '&'.http_build_query( $redirectUrl .= '&'.http_build_query(
[ [
@ -261,10 +260,10 @@ $form = new FormValidator(
$form->addElement('hidden', 'id', $document_id); $form->addElement('hidden', 'id', $document_id);
$form->addElement('hidden', 'curdirpath', $path); $form->addElement('hidden', 'curdirpath', $path);
$course_quota = format_file_size(DocumentManager::get_course_quota() - DocumentManager::documents_total_space()); $courseQuota = format_file_size(DocumentManager::get_course_quota() - DocumentManager::documents_total_space());
$label = $label =
get_lang('MaxFileSize').': '.ini_get('upload_max_filesize').'<br/>'. get_lang('MaxFileSize').': '.ini_get('upload_max_filesize').'<br/>'.
get_lang('DocumentQuota').': '.$course_quota; get_lang('DocumentQuota').': '.$courseQuota;
$form->addElement('file', 'file', [get_lang('File'), $label], 'style="width: 250px" id="user_upload"'); $form->addElement('file', 'file', [get_lang('File'), $label], 'style="width: 250px" id="user_upload"');
$form->addElement('text', 'title', get_lang('Title'), ['id' => 'title_file']); $form->addElement('text', 'title', get_lang('Title'), ['id' => 'title_file']);
@ -285,12 +284,12 @@ $form->addElement(
if (api_get_setting('search_enabled') === 'true') { if (api_get_setting('search_enabled') === 'true') {
//TODO: include language file //TODO: include language file
$supported_formats = get_lang('SupportedFormatsForIndex').': HTML, PDF, TXT, PDF, Postscript, MS Word, RTF, MS Power Point'; $supportedFormats = get_lang('SupportedFormatsForIndex').': HTML, PDF, TXT, PDF, Postscript, MS Word, RTF, MS Power Point';
$form->addElement( $form->addElement(
'checkbox', 'checkbox',
'index_document', 'index_document',
'', '',
get_lang('SearchFeatureDoIndexDocument').'<div style="font-size: 80%" >'.$supported_formats.'</div>' get_lang('SearchFeatureDoIndexDocument').'<div style="font-size: 80%" >'.$supportedFormats.'</div>'
); );
$form->addElement('html', '<br /><div class="sub-form">'); $form->addElement('html', '<br /><div class="sub-form">');
$form->addElement('html', '<div class="label">'.get_lang('SearchFeatureDocumentLanguage').'</div>'); $form->addElement('html', '<div class="label">'.get_lang('SearchFeatureDocumentLanguage').'</div>');

@ -2726,7 +2726,7 @@ class DocumentManager
* @param string $title * @param string $title
* @param string $comment * @param string $comment
* @param int $unzip unzip or not the file * @param int $unzip unzip or not the file
* @param string $if_exists overwrite, rename or warn (default) * @param string $ifExists overwrite, rename or warn (default)
* @param bool $index_document index document (search xapian module) * @param bool $index_document index document (search xapian module)
* @param bool $show_output print html messages * @param bool $show_output print html messages
* @param string $fileKey * @param string $fileKey
@ -2740,7 +2740,7 @@ class DocumentManager
$title = '', $title = '',
$comment = '', $comment = '',
$unzip = 0, $unzip = 0,
$if_exists = '', $ifExists = '',
$index_document = false, $index_document = false,
$show_output = false, $show_output = false,
$fileKey = 'file', $fileKey = 'file',
@ -2764,7 +2764,7 @@ class DocumentManager
api_get_group_id(), api_get_group_id(),
null, null,
$unzip, $unzip,
$if_exists, $ifExists,
$show_output, $show_output,
false, false,
null, null,
@ -2826,7 +2826,7 @@ class DocumentManager
null, null,
$_POST['language'], $_POST['language'],
$_REQUEST, $_REQUEST,
$if_exists $ifExists
); );
} }

@ -265,7 +265,8 @@ function handle_uploaded_document(
$sessionId, $sessionId,
$groupId, $groupId,
$output, $output,
$onlyUploadFile $onlyUploadFile,
$whatIfFileExists
); );
} elseif ($unzip == 1 && !preg_match('/.zip$/', strtolower($uploadedFile['name']))) { } elseif ($unzip == 1 && !preg_match('/.zip$/', strtolower($uploadedFile['name']))) {
// We can only unzip ZIP files (no gz, tar,...) // We can only unzip ZIP files (no gz, tar,...)
@ -360,27 +361,6 @@ function handle_uploaded_document(
$sessionId $sessionId
); );
$documentList = DocumentManager::getDocumentByPathInCourse(
$courseInfo,
$filePath //$filePath
);
// This means that the path already exists in this course.
if (!empty($documentList) && $whatIfFileExists != 'overwrite') {
//$found = false;
// Checking if we are talking about the same course + session
/*foreach ($documentList as $document) {
if ($document['session_id'] == $sessionId) {
$found = true;
break;
}
}*/
//if ($found == false) {
//$whatIfFileExists = 'rename';
//}
}
// What to do if the target file exists // What to do if the target file exists
switch ($whatIfFileExists) { switch ($whatIfFileExists) {
// Overwrite the file if it exists // Overwrite the file if it exists
@ -470,11 +450,13 @@ function handle_uploaded_document(
// If the file is in a folder, we need to update all parent folders // If the file is in a folder, we need to update all parent folders
item_property_update_on_folder($courseInfo, $uploadPath, $userId); item_property_update_on_folder($courseInfo, $uploadPath, $userId);
// Display success message with extra info to user // Display success message with extra info to user
if ($output) { if ($output) {
Display::addFlash( Display::addFlash(
Display::return_message( Display::return_message(
get_lang('UplUploadSucceeded').'<br /> '.$documentTitle.' '.get_lang('UplFileOverwritten'), get_lang('UplUploadSucceeded').'<br /> '.
$documentTitle.' '.get_lang('UplFileOverwritten'),
'confirmation', 'confirmation',
false false
) )
@ -611,7 +593,8 @@ function handle_uploaded_document(
if ($output) { if ($output) {
Display::addFlash( Display::addFlash(
Display::return_message( Display::return_message(
get_lang('UplUploadSucceeded').'<br />'.get_lang('UplFileSavedAs').' '.$documentTitle, get_lang('UplUploadSucceeded').'<br />'.
get_lang('UplFileSavedAs').' '.$documentTitle,
'success', 'success',
false false
) )
@ -633,12 +616,27 @@ function handle_uploaded_document(
return false; return false;
} }
break; break;
case 'nothing':
$fileExists = file_exists($fullPath);
if ($fileExists) {
if ($output) {
Display::addFlash(
Display::return_message(
$uploadPath.$cleanName.' '.get_lang('UplAlreadyExists'),
'warning',
false
)
);
}
break;
}
// no break
default: default:
// Only save the file if it doesn't exist or warn user if it does exist // Only save the file if it doesn't exist or warn user if it does exist
if (file_exists($fullPath) && $docId) { if (file_exists($fullPath) && $docId) {
if ($output) { if ($output) {
Display::addFlash( Display::addFlash(
Display::return_message($cleanName.' '.get_lang('UplAlreadyExists'), 'error', false) Display::return_message($cleanName.' '.get_lang('UplAlreadyExists'), 'warning', false)
); );
} }
} else { } else {
@ -1095,6 +1093,7 @@ function unzip_uploaded_file($uploaded_file, $upload_path, $base_work_dir, $max_
* @param int $groupId group.id * @param int $groupId group.id
* @param bool $output Optional. If no output not wanted on success, set to false. * @param bool $output Optional. If no output not wanted on success, set to false.
* @param bool $onlyUploadFile * @param bool $onlyUploadFile
* @param string $whatIfFileExists
* *
* @return boolean true if it succeeds false otherwise * @return boolean true if it succeeds false otherwise
*/ */
@ -1108,7 +1107,8 @@ function unzip_uploaded_document(
$sessionId = 0, $sessionId = 0,
$groupId = 0, $groupId = 0,
$output = true, $output = true,
$onlyUploadFile = false $onlyUploadFile = false,
$whatIfFileExists = 'overwrite'
) { ) {
$zip = new PclZip($uploaded_file['tmp_name']); $zip = new PclZip($uploaded_file['tmp_name']);
@ -1128,7 +1128,7 @@ function unzip_uploaded_document(
$destinationDir = api_get_path(SYS_ARCHIVE_PATH).$folder; $destinationDir = api_get_path(SYS_ARCHIVE_PATH).$folder;
mkdir($destinationDir, api_get_permissions_for_new_directories(), true); mkdir($destinationDir, api_get_permissions_for_new_directories(), true);
/* Uncompress zip file*/ // Uncompress zip file
// We extract using a callback function that "cleans" the path // We extract using a callback function that "cleans" the path
$zip->extract( $zip->extract(
PCLZIP_OPT_PATH, PCLZIP_OPT_PATH,
@ -1148,7 +1148,8 @@ function unzip_uploaded_document(
$sessionId, $sessionId,
$groupId, $groupId,
$output, $output,
['path' => $uploadPath] ['path' => $uploadPath],
$whatIfFileExists
); );
} else { } else {
// Copy result // Copy result
@ -1650,7 +1651,7 @@ function create_unexisting_directory(
$visibilities = [ $visibilities = [
0 => 'invisible', 0 => 'invisible',
1 => 'visible', 1 => 'visible',
2 => 'delete' 2 => 'delete',
]; ];
api_item_property_update( api_item_property_update(
$_course, $_course,
@ -1859,13 +1860,15 @@ function build_missing_files_form($missing_files, $upload_path, $file_name)
* *
* @param array $courseInfo * @param array $courseInfo
* @param array $userInfo * @param array $userInfo
* @param string $base_work_dir * @param string $base_work_dir course document dir
* @param string $folderPath * @param string $folderPath folder to read
* @param int $sessionId * @param int $sessionId
* @param int $groupId group.id * @param int $groupId group.id
* @param bool $output * @param bool $output
* @param array $parent * @param array $parent
* @param string $uploadPath * @param string $whatIfFileExists
*
* @return bool
* *
*/ */
function add_all_documents_in_folder_to_database( function add_all_documents_in_folder_to_database(
@ -1876,7 +1879,8 @@ function add_all_documents_in_folder_to_database(
$sessionId = 0, $sessionId = 0,
$groupId = 0, $groupId = 0,
$output = false, $output = false,
$parent = [] $parent = [],
$whatIfFileExists = 'overwrite'
) { ) {
if (empty($userInfo) || empty($courseInfo)) { if (empty($userInfo) || empty($courseInfo)) {
return false; return false;
@ -1894,12 +1898,11 @@ function add_all_documents_in_folder_to_database(
continue; continue;
} }
$parentPath = null; $parentPath = '';
if (!empty($parent) && isset($parent['path'])) { if (!empty($parent) && isset($parent['path'])) {
$parentPath = $parent['path']; $parentPath = $parent['path'];
if ($parentPath == '/') { if ($parentPath == '/') {
$parentPath = null; $parentPath = '';
} }
} }
@ -1916,6 +1919,8 @@ function add_all_documents_in_folder_to_database(
); );
if ($folderExists === true) { if ($folderExists === true) {
switch ($whatIfFileExists) {
case 'overwrite':
$documentId = DocumentManager::get_document_id($courseInfo, $completePath, $sessionId); $documentId = DocumentManager::get_document_id($courseInfo, $completePath, $sessionId);
if ($documentId) { if ($documentId) {
$newFolderData = DocumentManager::get_document_data_by_id( $newFolderData = DocumentManager::get_document_data_by_id(
@ -1925,6 +1930,42 @@ function add_all_documents_in_folder_to_database(
$sessionId $sessionId
); );
} }
break;
case 'rename':
$newFolderData = create_unexisting_directory(
$courseInfo,
$userId,
$sessionId,
$groupId,
null,
$base_work_dir,
$completePath,
null,
null,
true
);
break;
case 'nothing':
if ($output) {
$documentId = DocumentManager::get_document_id($courseInfo, $completePath, $sessionId);
if ($documentId) {
$folderData = DocumentManager::get_document_data_by_id(
$documentId,
$courseInfo['code'],
false,
$sessionId
);
Display::addFlash(
Display::return_message(
$folderData['path']. ' '.get_lang('UplAlreadyExists'),
'warning'
)
);
}
}
continue 2;
break;
}
} else { } else {
$newFolderData = create_unexisting_directory( $newFolderData = create_unexisting_directory(
$courseInfo, $courseInfo,
@ -1949,17 +1990,17 @@ function add_all_documents_in_folder_to_database(
$sessionId, $sessionId,
$groupId, $groupId,
$output, $output,
$newFolderData $newFolderData,
$whatIfFileExists
); );
} else { } else {
// Rename
$uploadedFile = [ $uploadedFile = [
'name' => $file, 'name' => $file,
'tmp_name' => $sysFolderPath, 'tmp_name' => $sysFolderPath,
'size' => filesize($sysFolderPath), 'size' => filesize($sysFolderPath),
'type' => null, 'type' => null,
'from_file' => true, 'from_file' => true,
'move_file' => true 'move_file' => true,
]; ];
handle_uploaded_document( handle_uploaded_document(
@ -1971,7 +2012,7 @@ function add_all_documents_in_folder_to_database(
$groupId, $groupId,
null, null,
0, 0,
'overwrite', $whatIfFileExists,
$output, $output,
false, false,
null, null,

Loading…
Cancel
Save