-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdfUploadProcessor.php
More file actions
86 lines (68 loc) · 2.3 KB
/
PdfUploadProcessor.php
File metadata and controls
86 lines (68 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
namespace Fab\Formule\Processor;
/*
* This file is part of the Fab/Formule project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use Fab\Formule\Processor\AbstractProcessor;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class FileUploadProcessor
*/
class PdfUploadProcessor extends AbstractProcessor
{
/**
* @param array $values
* @param string $insertOrUpdate
* @return array
*/
public function process(array $values, string $insertOrUpdate = ''): array
{
$savedFieldName = $this->savePdf();
if (strlen($savedFieldName) > 0) {
$values['file'] = $savedFieldName;
} else {
unset($values['file']);
}
return $values;
}
/**
* Save the uploaded PDF if valid or delete it if marked to be deleted
*
* @return string
*/
private function savePdf(): string
{
$fileName = '';
$storage = GeneralUtility::makeInstance(ResourceFactory::class)->getStorageObject('uid');
if (isset($_FILES['file'])) {
$uploadedFile = $_FILES['file'];
$fileSize = (int)$uploadedFile['size'];
// Only save if we successfully uploaded something
if ($uploadedFile['error'] === UPLOAD_ERR_OK && $fileSize > 0 && $fileSize <= GeneralUtility::getMaxUploadFileSize()) {
// Cancel if not a PDF
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
$mime = finfo_file($finfo, $uploadedFile['tmp_name']);
finfo_close($finfo);
if ($mime === 'application/pdf') {
$file = $storage->addUploadedFile($uploadedFile);
$fileName = $file->getName();
}
}
}
return $fileName;
}
/**
* Returns an instance of the current Frontend User.
*
* @return \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
*/
protected function getFrontendUser(): \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
{
return $GLOBALS['TSFE']->fe_user;
}
}