Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
typo3
qfq
Commits
2eabc916
Commit
2eabc916
authored
Oct 02, 2018
by
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
Changes
5
Pipelines
1
Show whitespace changes
Inline
Side-by-side
extension/qfq/api/file.php
View file @
2eabc916
...
...
@@ -40,6 +40,7 @@ $answer[API_STATUS] = API_ANSWER_STATUS_ERROR;
$answer
[
API_MESSAGE
]
=
''
;
try
{
try
{
$fileUpload
=
new
File
();
...
...
@@ -49,10 +50,11 @@ try {
// $answer[API_REDIRECT] = API_ANSWER_REDIRECT_NO;
$answer
[
API_STATUS
]
=
API_ANSWER_STATUS_SUCCESS
;
}
catch
(
qfq\UserFormException
$e
)
{
}
catch
(
qfq\UserFormException
$e
)
{
$answer
[
API_MESSAGE
]
=
$e
->
formatMessage
();
}
catch
(
qfq\CodeException
$e
)
{
}
catch
(
qfq\CodeException
$e
)
{
$answer
[
API_MESSAGE
]
=
$e
->
formatMessage
();
}
}
catch
(
\
Exception
$e
)
{
$answer
[
API_MESSAGE
]
=
"Generic Exception: "
.
$e
->
getMessage
();
}
...
...
extension/qfq/qfq/Constants.php
View file @
2eabc916
...
...
@@ -979,6 +979,10 @@ const FE_FILE_SPLIT = 'fileSplit';
const
FE_FILE_SPLIT_SVG
=
'svg'
;
const
FE_FILE_SPLIT_TABLE_NAME
=
'tableNameSplit'
;
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
const
FE_IMPORT_TO_TABLE
=
'importToTable'
;
...
...
extension/qfq/qfq/File.php
View file @
2eabc916
...
...
@@ -129,7 +129,7 @@ class File {
$this
->
checkMaxFileSize
(
$statusUpload
[
'size'
]);
$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
);
}
...
...
@@ -140,63 +140,6 @@ class File {
$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 $statusUpload
...
...
extension/qfq/qfq/Save.php
View file @
2eabc916
...
...
@@ -776,11 +776,44 @@ class Save {
$overwrite
=
isset
(
$formElement
[
FE_FILE_REPLACE_MODE
])
&&
$formElement
[
FE_FILE_REPLACE_MODE
]
==
FE_FILE_REPLACE_MODE_ALWAYS
;
Support
::
copyFile
(
$srcFile
,
$pathFileName
,
$overwrite
);
$this
->
autoOrient
(
$formElement
,
$pathFileName
);
$this
->
splitUpload
(
$formElement
,
$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.
* The only possible split target file format is 'svg': fileSplit=svg.
...
...
extension/qfq/qfq/helper/HelperFile.php
View file @
2eabc916
...
...
@@ -165,4 +165,60 @@ class HelperFile {
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment