Commit 2eabc916 authored by Carsten  Rose's avatar Carsten Rose
Browse files

F6886 / Upload: Auto Orient - first implementation - refs #6886

parent d4597e8b
Pipeline #932 passed with stage
in 1 minute and 46 seconds
...@@ -40,19 +40,21 @@ $answer[API_STATUS] = API_ANSWER_STATUS_ERROR; ...@@ -40,19 +40,21 @@ $answer[API_STATUS] = API_ANSWER_STATUS_ERROR;
$answer[API_MESSAGE] = ''; $answer[API_MESSAGE] = '';
try { try {
try {
$fileUpload = new File(); $fileUpload = new File();
$fileUpload->process(); $fileUpload->process();
$answer[API_MESSAGE] = 'upload: success'; $answer[API_MESSAGE] = 'upload: success';
// $answer[API_REDIRECT] = API_ANSWER_REDIRECT_NO; // $answer[API_REDIRECT] = API_ANSWER_REDIRECT_NO;
$answer[API_STATUS] = API_ANSWER_STATUS_SUCCESS; $answer[API_STATUS] = API_ANSWER_STATUS_SUCCESS;
} catch (qfq\UserFormException $e) { } catch (qfq\UserFormException $e) {
$answer[API_MESSAGE] = $e->formatMessage(); $answer[API_MESSAGE] = $e->formatMessage();
} catch (qfq\CodeException $e) { } catch (qfq\CodeException $e) {
$answer[API_MESSAGE] = $e->formatMessage(); $answer[API_MESSAGE] = $e->formatMessage();
}
} catch (\Exception $e) { } catch (\Exception $e) {
$answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage(); $answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage();
} }
......
...@@ -979,6 +979,10 @@ const FE_FILE_SPLIT = 'fileSplit'; ...@@ -979,6 +979,10 @@ const FE_FILE_SPLIT = 'fileSplit';
const FE_FILE_SPLIT_SVG = 'svg'; const FE_FILE_SPLIT_SVG = 'svg';
const FE_FILE_SPLIT_TABLE_NAME = 'tableNameSplit'; const FE_FILE_SPLIT_TABLE_NAME = 'tableNameSplit';
const FE_FILE_DOWNLOAD_BUTTON = 'downloadButton'; const FE_FILE_DOWNLOAD_BUTTON = 'downloadButton';
const FE_FILE_AUTO_ORIENT = 'autoOrient';
const FE_FILE_AUTO_ORIENT_CMD = 'autoOrientCmd';
const FE_FILE_AUTO_ORIENT_CMD_DEFAULT = 'convert -auto-orient {{fileDestination:V}} {{fileDestination:V}}.new; mv {{fileDestination:V}}.new {{fileDestination:V}}';
const FE_FILE_AUTO_ORIENT_MIME_TYPE = 'autoOrientMimeType';
// Excel Import // Excel Import
const FE_IMPORT_TO_TABLE = 'importToTable'; const FE_IMPORT_TO_TABLE = 'importToTable';
......
...@@ -129,7 +129,7 @@ class File { ...@@ -129,7 +129,7 @@ class File {
$this->checkMaxFileSize($statusUpload['size']); $this->checkMaxFileSize($statusUpload['size']);
$accept = $this->store->getVar(FE_FILE_MIME_TYPE_ACCEPT, STORE_SIP); $accept = $this->store->getVar(FE_FILE_MIME_TYPE_ACCEPT, STORE_SIP);
if (!$this->checkFileType($statusUpload['tmp_name'], $statusUpload['name'], $accept)) { if (!HelperFile::checkFileType($statusUpload['tmp_name'], $statusUpload['name'], $accept)) {
throw new UserFormException('Filetype not allowed. Allowed: ' . $accept, ERROR_UPLOAD_FILE_TYPE); throw new UserFormException('Filetype not allowed. Allowed: ' . $accept, ERROR_UPLOAD_FILE_TYPE);
} }
...@@ -140,63 +140,6 @@ class File { ...@@ -140,63 +140,6 @@ class File {
$this->store->setVar($sipUpload, $statusUpload, STORE_EXTRA); $this->store->setVar($sipUpload, $statusUpload, STORE_EXTRA);
} }
/**
* Checks the file filetype against the allowed mimetype definition. Return true as soon as one match is found.
* Types recognized:
* * 'mime type' as delivered by `file` which matches a definition on
* http://www.iana.org/assignments/media-types/media-types.xhtml
* * Joker based: audio/*, video/*, image/*
* * Filename extension based: .pdf,.doc,..
*
* @param string $tmp_name
* @param string $name
* @param string $accept
*
* @return bool
* @throws UserFormException
*/
private function checkFileType($tmp_name, $name, $accept) {
// E.g.: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=binary'
$fileMimeType = HelperFile::getMimeType($tmp_name);
// Strip optional '; charset=binary'
$arr = explode(';', $fileMimeType, 2);
$fileMimeType = $arr[0];
// Split between 'Media Type' and 'Media Subtype'
$fileMimeTypeSplitted = explode('/', $arr[0], 2);
$path_parts = pathinfo($name); // to extract the filename extension of the uploaded file.
// Process all listed mimetypes (incl. filename extension and joker)
// $accept e.g.: 'image/*,application/pdf,.pdf'
$arr = explode(',', $accept); // Split multiple defined mimetypes/extensions in single chunks.
foreach ($arr as $listElementMimeType) {
$listElementMimeType = trim(strtolower($listElementMimeType));
if ($listElementMimeType == '') {
continue; // will be skipped
} elseif ($listElementMimeType[0] == '.') { // Check for definition 'filename extension'
if ('.' . strtolower($path_parts['extension']) == $listElementMimeType) {
return true;
}
} else {
// Check for Joker, e.g.: 'image/*'
$splitted = explode('/', $listElementMimeType, 2);
if ($splitted[1] == '*') {
if ($splitted[0] == $fileMimeTypeSplitted[0]) {
return true;
}
} elseif ($fileMimeType == $listElementMimeType) {
return true;
}
}
}
return false;
}
/** /**
* @param $sipUpload * @param $sipUpload
* @param $statusUpload * @param $statusUpload
......
...@@ -776,11 +776,44 @@ class Save { ...@@ -776,11 +776,44 @@ class Save {
$overwrite = isset($formElement[FE_FILE_REPLACE_MODE]) && $formElement[FE_FILE_REPLACE_MODE] == FE_FILE_REPLACE_MODE_ALWAYS; $overwrite = isset($formElement[FE_FILE_REPLACE_MODE]) && $formElement[FE_FILE_REPLACE_MODE] == FE_FILE_REPLACE_MODE_ALWAYS;
Support::copyFile($srcFile, $pathFileName, $overwrite); Support::copyFile($srcFile, $pathFileName, $overwrite);
$this->autoOrient($formElement, $pathFileName);
$this->splitUpload($formElement, $pathFileName); $this->splitUpload($formElement, $pathFileName);
return $pathFileName; return $pathFileName;
} }
/**
* If fe['autoOrient'] is given and the MimeType corresponds to fe['autoOrientMimeType']: the given {{pathFileName:V}} will be converted.
* ImageMagick 'convert' seems to do a better job than GraficsMagick (Orientation is stable even if multiple applied).
*
* @param array $formElement
* @param $pathFileName
* @throws CodeException
* @throws DbException
* @throws UserFormException
* @throws UserReportException
*/
private function autoOrient(array $formElement, $pathFileName){
// 'autoOrient' wished?
if(!isset($formElement[FE_FILE_AUTO_ORIENT]) || $formElement[FE_FILE_AUTO_ORIENT]=='0' ){
return; // No
}
// Upload has matching MimeType?
$mimeTypeList = empty($formElement[FE_FILE_AUTO_ORIENT_MIME_TYPE]) ? 'image/jpeg,image/png' : $formElement[FE_FILE_AUTO_ORIENT_MIME_TYPE];
if(!HelperFile::checkFileType($pathFileName, $pathFileName, $mimeTypeList)) {
return;
}
// Get 'autoOrient' command
$cmd = empty($formElement[FE_FILE_AUTO_ORIENT_CMD]) ? FE_FILE_AUTO_ORIENT_CMD_DEFAULT : $formElement[FE_FILE_AUTO_ORIENT_CMD];
$cmd = $this->evaluate->parse($cmd);
// Do 'autoOrient' command
$rc = exec($cmd);
}
/** /**
* Check's if the file $pathFileName should be splitted in one file per page. If no: do nothing and return. * Check's if the file $pathFileName should be splitted in one file per page. If no: do nothing and return.
* The only possible split target file format is 'svg': fileSplit=svg. * The only possible split target file format is 'svg': fileSplit=svg.
......
...@@ -165,4 +165,60 @@ class HelperFile { ...@@ -165,4 +165,60 @@ class HelperFile {
return $vars; return $vars;
} }
/**
* Checks the file filetype against the allowed mimeType definition. Return true as soon as one match is found.
* Types recognized:
* * 'mime type' as delivered by `file` which matches a definition on
* http://www.iana.org/assignments/media-types/media-types.xhtml
* * Joker based: audio/*, video/*, image/*
* * Filename extension based: .pdf,.doc,..
*
* @param string $pathFileName
* @param string $origFileName
* @param string $mimeTypeListAccept
*
* @return bool
* @throws UserFormException
*/
public static function checkFileType($pathFileName, $origFileName, $mimeTypeListAccept) {
// E.g.: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=binary'
$fileMimeType = self::getMimeType($pathFileName);
// Strip optional '; charset=binary'
$arr = explode(';', $fileMimeType, 2);
$fileMimeType = $arr[0];
// Split between 'Media Type' and 'Media Subtype'
$fileMimeTypeSplitted = explode('/', $arr[0], 2);
$path_parts = pathinfo($origFileName); // to extract the filename extension of the uploaded file.
// Process all listed mimeTypes (incl. filename extension and joker)
// $accept e.g.: 'image/*,application/pdf,.pdf'
$arr = explode(',', $mimeTypeListAccept); // Split multiple defined mimetypes/extensions in single chunks.
foreach ($arr as $listElementMimeType) {
$listElementMimeType = trim(strtolower($listElementMimeType));
if ($listElementMimeType == '') {
continue; // will be skipped
} elseif ($listElementMimeType[0] == '.') { // Check for definition 'filename extension'
if ('.' . strtolower($path_parts['extension']) == $listElementMimeType) {
return true;
}
} else {
// Check for Joker, e.g.: 'image/*'
$splitted = explode('/', $listElementMimeType, 2);
if ($splitted[1] == '*') {
if ($splitted[0] == $fileMimeTypeSplitted[0]) {
return true;
}
} elseif ($fileMimeType == $listElementMimeType) {
return true;
}
}
}
return false;
}
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment