14 import(
'lib.pkp.classes.handler.APIHandler');
21 $this->_handlerPath =
'_uploadPublicFile';
22 $roles = [ROLE_ID_SITE_ADMIN, ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR, ROLE_ID_REVIEWER, ROLE_ID_AUTHOR, ROLE_ID_ASSISTANT, ROLE_ID_READER];
23 $this->_endpoints = array(
27 'handler' => array($this,
'getOptions'),
34 'handler' => array($this,
'uploadFile'),
40 parent::__construct();
46 public function authorize($request, &$args, $roleAssignments) {
47 import(
'lib.pkp.classes.security.authorization.PolicySet');
48 $rolePolicy =
new PolicySet(COMBINING_PERMIT_OVERRIDES);
50 import(
'lib.pkp.classes.security.authorization.RoleBasedHandlerOperationPolicy');
51 foreach($roleAssignments as $role => $operations) {
56 return parent::authorize($request, $args, $roleAssignments);
66 private function getResponse($response) {
67 return $response->withHeader(
'Access-Control-Allow-Headers',
'Content-Type, X-Requested-With, X-PINGOTHER, X-File-Name, Cache-Control');
78 public function uploadFile($slimRequest, $response, $args) {
81 if (empty($_FILES) || empty($_FILES[
'file'])) {
82 return $response->withStatus(400)->withJsonError(
'api.temporaryFiles.400.noUpload');
87 if (!file_exists($siteDir) || !is_writeable($siteDir)) {
88 return $response->withStatus(500)->withJsonError(
'api.publicFiles.500.badFilesDir');
90 $userDir = $siteDir .
'/images/' . $request->getUser()->getUsername();
91 $isUserAllowed =
true;
92 $allowedDirSize =
Config::getVar(
'files',
'public_user_dir_size', 5000) * 1024;
93 $allowedFileTypes = [
'gif',
'jpg',
'png'];
105 if (!$isUserAllowed) {
106 return $response->withStatus(403)->withJsonError(
'api.publicFiles.403.unauthorized');
111 if ($allowedDirSize > 0 && file_exists($userDir)) {
112 foreach (
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($userDir, FilesystemIterator::SKIP_DOTS)) as $object) {
113 $currentSize += $object->getSize();
116 if (($currentSize + $_FILES[
'file'][
'size']) > $allowedDirSize) {
117 return $response->withStatus(413)->withJsonError(
'api.publicFiles.413.noDirSpace', [
118 'fileUploadSize' => ceil($_FILES[
'file'][
'size']/1024),
119 'dirSizeLeft' => ceil(($allowedDirSize - $currentSize)/1024),
123 import(
'lib.pkp.classes.file.FileManager');
125 $filename = $fileManager->getUploadedFileName(
'file');
133 strtolower($filename)
137 $extension = pathinfo(strtolower(trim($filename)), PATHINFO_EXTENSION);
140 if (!in_array($extension, $allowedFileTypes)) {
141 return $response->withStatus(400)->withJsonError(
'api.publicFiles.400.extensionNotSupported', [
142 'fileTypes' => join(__(
'common.commaListSeparator'), $allowedFileTypes)
147 if (in_array($extension, [
'gif',
'jpg',
'jpeg',
'png',
'jpe'])) {
148 if (getimagesize($_FILES[
'file'][
'tmp_name']) ===
false) {
149 return $response->withStatus(400)->withJsonError(
'api.publicFiles.400.invalidImage');
152 if ($extensionFromMimeType !==
'.' . $extension) {
153 return $response->withStatus(400)->withJsonError(
'api.publicFiles.400.mimeTypeNotMatched');
158 $destinationPath = $siteDir .
'/images/' . $request->getUser()->getUsername() .
'/' . $filename;
159 $success = $fileManager->uploadFile(
'file', $destinationPath);
161 if ($success ===
false) {
162 if ($fileManager->uploadError($filename)) {
163 switch ($fileManager->getUploadErrorCode($filename)) {
164 case UPLOAD_ERR_INI_SIZE:
165 case UPLOAD_ERR_FORM_SIZE:
167 case UPLOAD_ERR_PARTIAL:
168 return $response->withStatus(400)->withJsonError(
'api.temporaryFiles.409.uploadFailed');
169 case UPLOAD_ERR_NO_FILE:
170 return $response->withStatus(400)->withJsonError(
'api.temporaryFiles.400.noUpload');
171 case UPLOAD_ERR_NO_TMP_DIR:
172 case UPLOAD_ERR_CANT_WRITE:
173 case UPLOAD_ERR_EXTENSION:
174 return $response->withStatus(400)->withJsonError(
'api.temporaryFiles.400.config');
177 return $response->withStatus(400)->withJsonError(
'api.temporaryFiles.409.uploadFailed');
180 return $this->getResponse($response->withJson([
181 'url' => $request->getBaseUrl() .
'/' .
183 $request->getUser()->getUsername() .
'/' .
198 return $this->getResponse($response);