摘要:OCS: File Extension of Paper Files
OCS: File Extension of Paper Files
1. Code Recapped
There are two place recapped for checking the extenstion of file uploaded, Class PaperFileManager and AuthorSubmitStep2Form. In PaperFileManager class, I've created checkUploadedFileType and getFileExtension methods to be utilized by AuthorSubmitStep2Form. It's convenient for AuthorSubmitStep2Form to use getFileExtension method to obtain the uploaded file extension, and checkUploadedFileType method to get known if the extension is valid.
Class file location:
- PaperFileManager: C:\wamp\www\ocs2\classes\author\form\submit\AuthorSubmitStep2Form.inc.php.
- AuthorSubmitStep2Form: C:\wamp\www\ocs2\classes\author\form\submit\AuthorSubmitStep2Form.inc.php
The details pasted as follow:
AuthorSubmitStep2Form:
function uploadSubmissionFile($fileName) {
...
// NeiL
if (!$paperFileManager->checkUploadedFileType($fileName)) {
$fn = $paperFileManager->getFileExtension($fileName);
$this->addError('uploadSubmissionFile', $fn.' '.__('common.uploadFailed.fileType'));
return false;
}
...
}
PaperFileManager:
/**
* Creator: NeiL
* @param $fileName string the name of the file used in the POST form
* @return bool correct file type or not
*/
function checkUploadedFileType($fileName) {
if (!isset($fileName)) return false;
$fn = $this->getUploadedFileName($fileName);
$ft = $this->getUploadedFileType($fileName);
$fExt = $this->getExtension($fn);
$alowTypes = array('application/pdf');
$alowExts = array('pdf');
if (isset($ft) && isset($fExt) && in_array($ft, $alowTypes)) {
return in_array(strtolower($fExt), $alowExts);
}
return false;
}
/**
* Creator: NeiL
* @param $fileName string the name of the file used in the POST form
* @return string file extension
*/
function getFileExtension($fileName) {
if (!isset($fileName)) return '';
$fn = $this->getUploadedFileName($fileName);
return $this->getExtension($fn);
}
2. The Message prompting to Invalid File Extension
The configuration of localized message is setted up in common.xml, which is in C:\wamp\www\ocs2\lib\pkp\locale\zh_TW\common.xml. Two new elements added in common.xml are ones with key "common.uploadFailed" and "common.uploadFailed.fileType". Just check it and recap the context of them you like. For completeness, the elements are listed below.
common.xml:
<message key="common.uploadFailed">檔案上傳失敗</message>
<message key="common.uploadFailed.fileType">檔案類型無法上傳</message>