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. 137
      main/inc/lib/fileUpload.lib.php

@ -88,13 +88,13 @@ $htmlHeadXtra[] = '<script>
function check_unzip() {
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[2].disabled=true;
//document.upload.if_exists[2].disabled=true;
} else {
document.upload.if_exists[0].checked=true;
document.upload.if_exists[0].disabled=false;
document.upload.if_exists[2].disabled=false;
document.upload.if_exists[2].checked=true;
//document.upload.if_exists[0].disabled=false;
//document.upload.if_exists[2].disabled=false;
}
}
@ -205,7 +205,6 @@ if (!empty($_FILES)) {
);
$redirectUrl = api_get_self().'?'.api_get_cidreq().$certificateLink;
if ($document_data) {
$redirectUrl .= '&'.http_build_query(
[
@ -261,10 +260,10 @@ $form = new FormValidator(
$form->addElement('hidden', 'id', $document_id);
$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 =
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('text', 'title', get_lang('Title'), ['id' => 'title_file']);
@ -285,12 +284,12 @@ $form->addElement(
if (api_get_setting('search_enabled') === 'true') {
//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(
'checkbox',
'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', '<div class="label">'.get_lang('SearchFeatureDocumentLanguage').'</div>');

@ -2726,7 +2726,7 @@ class DocumentManager
* @param string $title
* @param string $comment
* @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 $show_output print html messages
* @param string $fileKey
@ -2740,7 +2740,7 @@ class DocumentManager
$title = '',
$comment = '',
$unzip = 0,
$if_exists = '',
$ifExists = '',
$index_document = false,
$show_output = false,
$fileKey = 'file',
@ -2764,7 +2764,7 @@ class DocumentManager
api_get_group_id(),
null,
$unzip,
$if_exists,
$ifExists,
$show_output,
false,
null,
@ -2826,7 +2826,7 @@ class DocumentManager
null,
$_POST['language'],
$_REQUEST,
$if_exists
$ifExists
);
}

@ -265,7 +265,8 @@ function handle_uploaded_document(
$sessionId,
$groupId,
$output,
$onlyUploadFile
$onlyUploadFile,
$whatIfFileExists
);
} elseif ($unzip == 1 && !preg_match('/.zip$/', strtolower($uploadedFile['name']))) {
// We can only unzip ZIP files (no gz, tar,...)
@ -360,27 +361,6 @@ function handle_uploaded_document(
$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
switch ($whatIfFileExists) {
// 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
item_property_update_on_folder($courseInfo, $uploadPath, $userId);
// Display success message with extra info to user
if ($output) {
Display::addFlash(
Display::return_message(
get_lang('UplUploadSucceeded').'<br /> '.$documentTitle.' '.get_lang('UplFileOverwritten'),
get_lang('UplUploadSucceeded').'<br /> '.
$documentTitle.' '.get_lang('UplFileOverwritten'),
'confirmation',
false
)
@ -611,7 +593,8 @@ function handle_uploaded_document(
if ($output) {
Display::addFlash(
Display::return_message(
get_lang('UplUploadSucceeded').'<br />'.get_lang('UplFileSavedAs').' '.$documentTitle,
get_lang('UplUploadSucceeded').'<br />'.
get_lang('UplFileSavedAs').' '.$documentTitle,
'success',
false
)
@ -633,12 +616,27 @@ function handle_uploaded_document(
return false;
}
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:
// Only save the file if it doesn't exist or warn user if it does exist
if (file_exists($fullPath) && $docId) {
if ($output) {
Display::addFlash(
Display::return_message($cleanName.' '.get_lang('UplAlreadyExists'), 'error', false)
Display::return_message($cleanName.' '.get_lang('UplAlreadyExists'), 'warning', false)
);
}
} else {
@ -1095,6 +1093,7 @@ function unzip_uploaded_file($uploaded_file, $upload_path, $base_work_dir, $max_
* @param int $groupId group.id
* @param bool $output Optional. If no output not wanted on success, set to false.
* @param bool $onlyUploadFile
* @param string $whatIfFileExists
*
* @return boolean true if it succeeds false otherwise
*/
@ -1108,7 +1107,8 @@ function unzip_uploaded_document(
$sessionId = 0,
$groupId = 0,
$output = true,
$onlyUploadFile = false
$onlyUploadFile = false,
$whatIfFileExists = 'overwrite'
) {
$zip = new PclZip($uploaded_file['tmp_name']);
@ -1128,7 +1128,7 @@ function unzip_uploaded_document(
$destinationDir = api_get_path(SYS_ARCHIVE_PATH).$folder;
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
$zip->extract(
PCLZIP_OPT_PATH,
@ -1148,7 +1148,8 @@ function unzip_uploaded_document(
$sessionId,
$groupId,
$output,
['path' => $uploadPath]
['path' => $uploadPath],
$whatIfFileExists
);
} else {
// Copy result
@ -1650,7 +1651,7 @@ function create_unexisting_directory(
$visibilities = [
0 => 'invisible',
1 => 'visible',
2 => 'delete'
2 => 'delete',
];
api_item_property_update(
$_course,
@ -1859,13 +1860,15 @@ function build_missing_files_form($missing_files, $upload_path, $file_name)
*
* @param array $courseInfo
* @param array $userInfo
* @param string $base_work_dir
* @param string $folderPath
* @param string $base_work_dir course document dir
* @param string $folderPath folder to read
* @param int $sessionId
* @param int $groupId group.id
* @param bool $output
* @param array $parent
* @param string $uploadPath
* @param string $whatIfFileExists
*
* @return bool
*
*/
function add_all_documents_in_folder_to_database(
@ -1876,7 +1879,8 @@ function add_all_documents_in_folder_to_database(
$sessionId = 0,
$groupId = 0,
$output = false,
$parent = []
$parent = [],
$whatIfFileExists = 'overwrite'
) {
if (empty($userInfo) || empty($courseInfo)) {
return false;
@ -1894,12 +1898,11 @@ function add_all_documents_in_folder_to_database(
continue;
}
$parentPath = null;
$parentPath = '';
if (!empty($parent) && isset($parent['path'])) {
$parentPath = $parent['path'];
if ($parentPath == '/') {
$parentPath = null;
$parentPath = '';
}
}
@ -1916,14 +1919,52 @@ function add_all_documents_in_folder_to_database(
);
if ($folderExists === true) {
$documentId = DocumentManager::get_document_id($courseInfo, $completePath, $sessionId);
if ($documentId) {
$newFolderData = DocumentManager::get_document_data_by_id(
$documentId,
$courseInfo['code'],
false,
$sessionId
);
switch ($whatIfFileExists) {
case 'overwrite':
$documentId = DocumentManager::get_document_id($courseInfo, $completePath, $sessionId);
if ($documentId) {
$newFolderData = DocumentManager::get_document_data_by_id(
$documentId,
$courseInfo['code'],
false,
$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 {
$newFolderData = create_unexisting_directory(
@ -1949,17 +1990,17 @@ function add_all_documents_in_folder_to_database(
$sessionId,
$groupId,
$output,
$newFolderData
$newFolderData,
$whatIfFileExists
);
} else {
// Rename
$uploadedFile = [
'name' => $file,
'tmp_name' => $sysFolderPath,
'size' => filesize($sysFolderPath),
'type' => null,
'from_file' => true,
'move_file' => true
'move_file' => true,
];
handle_uploaded_document(
@ -1971,7 +2012,7 @@ function add_all_documents_in_folder_to_database(
$groupId,
null,
0,
'overwrite',
$whatIfFileExists,
$output,
false,
null,

Loading…
Cancel
Save