-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[document_repository] Fixed multiple files with same name when uploading to document repository #3700
[document_repository] Fixed multiple files with same name when uploading to document repository #3700
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE document_repository ADD `UUID` varchar(36) DEFAULT ''; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,18 +10,23 @@ | |
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
* @link https://github.com/aces/Loris | ||
*/ | ||
|
||
$user =& User::singleton(); | ||
|
||
if (!$user->hasPermission('document_repository_delete')) { | ||
header("HTTP/1.1 403 Forbidden"); | ||
exit; | ||
} | ||
|
||
set_include_path(get_include_path().":../../project/libraries:../../php/libraries:"); | ||
|
||
require_once "NDB_Client.class.inc"; | ||
require_once "NDB_Config.class.inc"; | ||
require_once "Email.class.inc"; | ||
|
||
$client = new NDB_Client(); | ||
$client->initialize("../../project/config.xml"); | ||
|
||
$factory = NDB_Factory::singleton(); | ||
$baseURL = $factory->settings()->getBaseURL(); | ||
|
||
|
@@ -67,4 +72,18 @@ | |
unlink($path); | ||
} | ||
|
||
// Cleanup empty directories | ||
set_error_handler( | ||
function () { | ||
// Silence the E_WARNING when files exist in the directory. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if this is the best way to do this. Shouldn't we delete the files first in this case? I'm a little worried about this silencing useful warnings. |
||
} | ||
); | ||
$rm_directory = __DIR__ . '/../user_uploads/' | ||
. substr($dataDir, 0, strlen($dataDir)-(strlen($fileName)+1)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you trying to chop off the filename from |
||
rmdir($rm_directory); | ||
$rm_directory = __DIR__ . '/../user_uploads/' | ||
. $userName . '/' . $fileName; | ||
rmdir($rm_directory); | ||
restore_error_handler(); | ||
|
||
?> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,15 +25,17 @@ | |
require_once "NDB_Client.class.inc"; | ||
require_once "NDB_Config.class.inc"; | ||
require_once "Email.class.inc"; | ||
|
||
$client = new NDB_Client(); | ||
$client->initialize("../../project/config.xml"); | ||
|
||
$factory = NDB_Factory::singleton(); | ||
$baseURL = $factory->settings()->getBaseURL(); | ||
|
||
$config = NDB_Config::singleton(); | ||
|
||
// create Database object | ||
$DB =& Database::singleton(); | ||
// Setup Database object. | ||
$config =& \NDB_Config::singleton(); | ||
$db_config = $config->getSetting('database'); | ||
$db =& \Database::singleton(); | ||
|
||
$editNotifier = new NDB_Notifier( | ||
"document_repository", | ||
|
@@ -58,26 +60,61 @@ | |
$instrument = $_POST['instrument'] !== '' ? $_POST['instrument'] : null; | ||
$pscid = $_POST['pscid'] !== '' ? $_POST['pscid'] : null; | ||
$visit = $_POST['visit'] !== '' ? $_POST['visit'] : null; | ||
$comments = $_POST['comments'] !== '' ? $_POST['commnets'] : null; | ||
$comments = $_POST['comments'] !== '' ? $_POST['comments'] : null; | ||
$version = $_POST['version'] !== '' ? $_POST['version'] : null; | ||
$uuid = uuid4(); | ||
|
||
$fileSize = $_FILES["file"]["size"]; | ||
$fileName = $_FILES["file"]["name"]; | ||
$fileType = end((explode(".", $fileName))); | ||
$fileType = ''; | ||
// Handle retrieving the file type. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is better done using It's also more accurate to say that you are grabbing the uploaded file's path extension rather than its actual type. Properly getting the file type would involve looking at the file signatures. |
||
if (preg_match('/\./', $fileName)) { | ||
$pos = strrpos($fileName, '.', -1); | ||
if ($pos+1 != strlen($fileName)) { | ||
$fileType = substr( | ||
$fileName, | ||
strrpos($fileName, '.', -1)+1 | ||
); | ||
} | ||
} | ||
$sql_statement = $db->prepare( | ||
'SELECT File_name, version FROM document_repository ' | ||
.'WHERE File_name=? AND uploaded_by=?' | ||
); | ||
$sql_statement->bindParam(1, $fileName, PDO::PARAM_STR); | ||
$sql_statement->bindParam(2, $puser, PDO::PARAM_STR); | ||
$sql_statement->execute(); | ||
$sql_result = $sql_statement->fetchAll(PDO::FETCH_ASSOC); | ||
|
||
// __DIR__ is the document_repository ajax directory | ||
// when this script is executing. Go up a level to the | ||
// document_repository module directory, and use a | ||
// user_uploads directory as a base for user uploads | ||
$base_path = __DIR__ . "/../user_uploads/"; | ||
$fileBase = $puser . "/" . $fileName; | ||
$base_path = realpath(__DIR__ . '/..') . '/user_uploads/'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind changing this to |
||
$fileBase = $puser . '/' | ||
. $fileName | ||
. '/' . $uuid | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put all the front-slashes at the front of the line or the back of the line for consistency. |
||
. '/' . $fileName; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $filename is used twice. Is it supposed to be like that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe the the first There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PapillonMcGill um4r12 is correct and so it's supposed to be like that. |
||
|
||
// Create user directory /base_path/user | ||
if (!file_exists($base_path . $puser)) { | ||
mkdir($base_path . $puser, 0777); | ||
mkdir($base_path . $puser, 0770); | ||
} | ||
|
||
|
||
$target_path = $base_path . $fileBase; | ||
// Create filename directory /base_path/user/fileName | ||
if (!file_exists($base_path . $puser . '/' . $fileName)) { | ||
mkdir($base_path . $puser . '/' . $fileName, 0770); | ||
} | ||
// Create uuid directory /base_path/user/fileName/uuid | ||
if (!file_exists( | ||
$base_path . $puser . '/' . $fileName . '/' . $uuid | ||
) | ||
) { | ||
mkdir( | ||
$base_path . $puser . '/' . $fileName . '/' . $uuid, | ||
0770 | ||
); | ||
} | ||
$target_path = $base_path . $fileBase; | ||
|
||
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_path)) { | ||
$success = $DB->insert( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (and same with line 174 & 176) |
||
|
@@ -95,10 +132,10 @@ | |
'PSCID' => $pscid, | ||
'visitLabel' => $visit, | ||
'File_type' => $fileType, | ||
'UUID' => $uuid, | ||
) | ||
); | ||
$msg_data['newDocument'] | ||
= $baseURL . "/document_repository/"; | ||
$msg_data['newDocument'] = $baseURL . "/document_repository/"; | ||
$msg_data['document'] = $fileName; | ||
|
||
$uploadNotifier->notify($msg_data); | ||
|
@@ -110,7 +147,7 @@ | |
} else { | ||
echo "There was an error uploading the file"; | ||
} | ||
} elseif ($action == 'edit') { | ||
} else if ($action == 'edit') { | ||
$id = $_POST['idEdit']; | ||
$category = $_POST['categoryEdit']; | ||
$instrument = $_POST['instrumentEdit']; | ||
|
@@ -147,4 +184,21 @@ | |
} | ||
} | ||
|
||
/** | ||
* Create a UUID v4 string. | ||
* | ||
* Source from comments: | ||
* http://php.net/manual/en/function.com-create-guid.php | ||
* Maybe move to Utilities class. | ||
* | ||
* @return String $version | ||
*/ | ||
function uuid4() | ||
{ | ||
$data = openssl_random_pseudo_bytes(16); | ||
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 | ||
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 | ||
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); | ||
} | ||
|
||
?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this patch should go into just the Archive directory? we're now on the 20 release. also, patches usually get moved to the version folders after a release if on minor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we are at this, when would you submit the patches into the
New_patches
dir?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@um4r12 when you are on a branch that has these new directories. i'm going to bring this up in tomorrow's meeting :)