-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathFileFactory.php
More file actions
141 lines (126 loc) · 5.22 KB
/
FileFactory.php
File metadata and controls
141 lines (126 loc) · 5.22 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
namespace In2code\Powermail\Domain\Factory;
use In2code\Powermail\Domain\Model\Answer;
use In2code\Powermail\Domain\Model\File;
use In2code\Powermail\Domain\Model\Form;
use In2code\Powermail\Domain\Repository\FieldRepository;
use In2code\Powermail\Exception\DeprecatedException;
use In2code\Powermail\Utility\FrontendUtility;
use In2code\Powermail\Utility\StringUtility;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException;
use TYPO3\CMS\Core\Type\File\FileInfo;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\Exception as ExceptionExtbaseObject;
use TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException;
/**
* Class FileFactory
*/
class FileFactory
{
public function __construct(protected array $settings)
{
}
/**
* Get instance of File from Files Array
*
* @param array $filesArray normally $_FILES['tx_powermail_pi1']
* @throws ExtensionConfigurationExtensionNotConfiguredException
* @throws ExtensionConfigurationPathDoesNotExistException
* @throws InvalidQueryException
* @throws ExceptionExtbaseObject
*/
public function getInstanceFromFilesArray(array $filesArray, string $marker, int $key): ?File
{
$originalName = $filesArray['name']['field'][$marker][$key] ?? '';
$size = $filesArray['size']['field'][$marker][$key] ?? 0;
$type = $filesArray['type']['field'][$marker][$key] ?? '';
$temporaryName = $filesArray['tmp_name']['field'][$marker][$key] ?? '';
if (!empty($originalName) && !empty($temporaryName) && $size > 0) {
return $this->makeFileInstance($marker, $originalName, $size, $type, $temporaryName);
}
return null;
}
/**
* Get instance of File from arguments
*
* @throws ExtensionConfigurationExtensionNotConfiguredException
* @throws ExtensionConfigurationPathDoesNotExistException
* @throws InvalidQueryException
* @throws DeprecatedException
* @throws ExceptionExtbaseObject
*/
public function getInstanceFromUploadArguments(string $marker, string $value, array $arguments): ?File
{
$fieldRepository = GeneralUtility::makeInstance(FieldRepository::class);
$field = $fieldRepository->findByMarkerAndForm($marker, (int)$arguments['mail']['form']);
if ($field !== null && $field->dataTypeFromFieldType($field->getType()) === 3 && ($value !== '' && $value !== '0')) {
return $this->makeFileInstance($marker, $value, 0, '', '', true);
}
return null;
}
/**
* Get instance of File from existing answer
*
* @throws ExtensionConfigurationExtensionNotConfiguredException
* @throws ExtensionConfigurationPathDoesNotExistException
* @throws InvalidQueryException
* @throws ExceptionExtbaseObject
*/
public function getInstanceFromExistingAnswerValue(string $fileName, Answer $answer): File
{
$form = $answer->getField()->getPage()->getForm();
$marker = $answer->getField()->getMarker();
return $this->makeFileInstance($marker, $fileName, 0, '', '', true, $form);
}
/**
* This subfunction is used to create a file instance. E.g. when a file was just uploaded or when a confirmation
* page is active, when a file was already uploaded in the step before.
*
* @throws ExtensionConfigurationExtensionNotConfiguredException
* @throws ExtensionConfigurationPathDoesNotExistException
* @throws InvalidQueryException
* @throws ExceptionExtbaseObject
*/
protected function makeFileInstance(
string $marker,
string $originalName,
int $size = 0,
string $type = '',
string $temporaryName = '',
bool $uploaded = false,
?Form $form = null
): File {
$file = GeneralUtility::makeInstance(File::class, $marker, $originalName, $temporaryName);
$file->setNewName(StringUtility::cleanString($originalName));
$file->setUploadFolder($this->getUploadFolder());
if ($size === 0) {
$size = (int)filesize(
$uploaded ? $file->getNewPathAndFilename(true) : $file->getTemporaryName()
);
}
$file->setSize($size);
if ($type === '') {
$type = (new FileInfo($file->getTemporaryName()))->getMimeType() ?: 'application/octet-stream';
}
$file->setType($type);
$file->setUploaded($uploaded);
/* @var FieldRepository $fieldRepository */
$fieldRepository = GeneralUtility::makeInstance(FieldRepository::class);
$file->setField($fieldRepository->findByMarkerAndForm($marker, $this->getFormUid($form)));
return $file;
}
protected function getUploadFolder(): string
{
return $this->settings['misc']['file']['folder'];
}
protected function getFormUid(?Form $form = null): int
{
if (!$form instanceof \In2code\Powermail\Domain\Model\Form) {
$arguments = FrontendUtility::getArguments(FrontendUtility::getPluginName());
return (int)$arguments['mail']['form'];
}
return $form->getUid();
}
}