Chris@18
|
1 <?php
|
Chris@18
|
2
|
Chris@18
|
3 namespace Drupal\jsonapi\Controller;
|
Chris@18
|
4
|
Chris@18
|
5 use Drupal\Component\Utility\Bytes;
|
Chris@18
|
6 use Drupal\Component\Utility\Crypt;
|
Chris@18
|
7 use Drupal\Component\Utility\Environment;
|
Chris@18
|
8 use Drupal\Core\Entity\EntityInterface;
|
Chris@18
|
9 use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
|
Chris@18
|
10 use Drupal\Core\Field\FieldDefinitionInterface;
|
Chris@18
|
11 use Drupal\Core\File\Exception\FileException;
|
Chris@18
|
12 use Drupal\Core\Validation\DrupalTranslator;
|
Chris@18
|
13 use Drupal\file\FileInterface;
|
Chris@18
|
14 use Drupal\Core\File\FileSystemInterface;
|
Chris@18
|
15 use Drupal\Core\Config\ConfigFactoryInterface;
|
Chris@18
|
16 use Drupal\Core\Lock\LockBackendInterface;
|
Chris@18
|
17 use Drupal\Core\Render\BubbleableMetadata;
|
Chris@18
|
18 use Drupal\Core\Session\AccountInterface;
|
Chris@18
|
19 use Drupal\Core\Utility\Token;
|
Chris@18
|
20 use Drupal\Component\Render\PlainTextOutput;
|
Chris@18
|
21 use Drupal\file\Entity\File;
|
Chris@18
|
22 use Drupal\file\Plugin\Field\FieldType\FileFieldItemList;
|
Chris@18
|
23 use Psr\Log\LoggerInterface;
|
Chris@18
|
24 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
|
Chris@18
|
25 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
Chris@18
|
26 use Symfony\Component\HttpFoundation\Request;
|
Chris@18
|
27 use Symfony\Component\HttpKernel\Exception\HttpException;
|
Chris@18
|
28 use Symfony\Component\Validator\ConstraintViolation;
|
Chris@18
|
29
|
Chris@18
|
30 /**
|
Chris@18
|
31 * Reads data from an upload stream and creates a corresponding file entity.
|
Chris@18
|
32 *
|
Chris@18
|
33 * This is implemented at the field level for the following reasons:
|
Chris@18
|
34 * - Validation for uploaded files is tied to fields (allowed extensions, max
|
Chris@18
|
35 * size, etc..).
|
Chris@18
|
36 * - The actual files do not need to be stored in another temporary location,
|
Chris@18
|
37 * to be later moved when they are referenced from a file field.
|
Chris@18
|
38 * - Permission to upload a file can be determined by a user's field- and
|
Chris@18
|
39 * entity-level access.
|
Chris@18
|
40 *
|
Chris@18
|
41 * @internal This will be removed once https://www.drupal.org/project/drupal/issues/2940383 lands.
|
Chris@18
|
42 */
|
Chris@18
|
43 class TemporaryJsonapiFileFieldUploader {
|
Chris@18
|
44
|
Chris@18
|
45 /**
|
Chris@18
|
46 * The regex used to extract the filename from the content disposition header.
|
Chris@18
|
47 *
|
Chris@18
|
48 * @var string
|
Chris@18
|
49 */
|
Chris@18
|
50 const REQUEST_HEADER_FILENAME_REGEX = '@\bfilename(?<star>\*?)=\"(?<filename>.+)\"@';
|
Chris@18
|
51
|
Chris@18
|
52 /**
|
Chris@18
|
53 * The amount of bytes to read in each iteration when streaming file data.
|
Chris@18
|
54 *
|
Chris@18
|
55 * @var int
|
Chris@18
|
56 */
|
Chris@18
|
57 const BYTES_TO_READ = 8192;
|
Chris@18
|
58
|
Chris@18
|
59 /**
|
Chris@18
|
60 * A logger instance.
|
Chris@18
|
61 *
|
Chris@18
|
62 * @var \Psr\Log\LoggerInterface
|
Chris@18
|
63 */
|
Chris@18
|
64 protected $logger;
|
Chris@18
|
65
|
Chris@18
|
66 /**
|
Chris@18
|
67 * The file system service.
|
Chris@18
|
68 *
|
Chris@18
|
69 * @var \Drupal\Core\File\FileSystemInterface
|
Chris@18
|
70 */
|
Chris@18
|
71 protected $fileSystem;
|
Chris@18
|
72
|
Chris@18
|
73 /**
|
Chris@18
|
74 * The MIME type guesser.
|
Chris@18
|
75 *
|
Chris@18
|
76 * @var \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface
|
Chris@18
|
77 */
|
Chris@18
|
78 protected $mimeTypeGuesser;
|
Chris@18
|
79
|
Chris@18
|
80 /**
|
Chris@18
|
81 * The token replacement instance.
|
Chris@18
|
82 *
|
Chris@18
|
83 * @var \Drupal\Core\Utility\Token
|
Chris@18
|
84 */
|
Chris@18
|
85 protected $token;
|
Chris@18
|
86
|
Chris@18
|
87 /**
|
Chris@18
|
88 * The lock service.
|
Chris@18
|
89 *
|
Chris@18
|
90 * @var \Drupal\Core\Lock\LockBackendInterface
|
Chris@18
|
91 */
|
Chris@18
|
92 protected $lock;
|
Chris@18
|
93
|
Chris@18
|
94 /**
|
Chris@18
|
95 * System file configuration.
|
Chris@18
|
96 *
|
Chris@18
|
97 * @var \Drupal\Core\Config\ImmutableConfig
|
Chris@18
|
98 */
|
Chris@18
|
99 protected $systemFileConfig;
|
Chris@18
|
100
|
Chris@18
|
101 /**
|
Chris@18
|
102 * Constructs a FileUploadResource instance.
|
Chris@18
|
103 *
|
Chris@18
|
104 * @param \Psr\Log\LoggerInterface $logger
|
Chris@18
|
105 * A logger instance.
|
Chris@18
|
106 * @param \Drupal\Core\File\FileSystemInterface $file_system
|
Chris@18
|
107 * The file system service.
|
Chris@18
|
108 * @param \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface $mime_type_guesser
|
Chris@18
|
109 * The MIME type guesser.
|
Chris@18
|
110 * @param \Drupal\Core\Utility\Token $token
|
Chris@18
|
111 * The token replacement instance.
|
Chris@18
|
112 * @param \Drupal\Core\Lock\LockBackendInterface $lock
|
Chris@18
|
113 * The lock service.
|
Chris@18
|
114 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
Chris@18
|
115 * The config factory.
|
Chris@18
|
116 */
|
Chris@18
|
117 public function __construct(LoggerInterface $logger, FileSystemInterface $file_system, MimeTypeGuesserInterface $mime_type_guesser, Token $token, LockBackendInterface $lock, ConfigFactoryInterface $config_factory) {
|
Chris@18
|
118 $this->logger = $logger;
|
Chris@18
|
119 $this->fileSystem = $file_system;
|
Chris@18
|
120 $this->mimeTypeGuesser = $mime_type_guesser;
|
Chris@18
|
121 $this->token = $token;
|
Chris@18
|
122 $this->lock = $lock;
|
Chris@18
|
123 $this->systemFileConfig = $config_factory->get('system.file');
|
Chris@18
|
124 }
|
Chris@18
|
125
|
Chris@18
|
126 /**
|
Chris@18
|
127 * Creates and validates a file entity for a file field from a file stream.
|
Chris@18
|
128 *
|
Chris@18
|
129 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
|
Chris@18
|
130 * The field definition of the field for which the file is to be uploaded.
|
Chris@18
|
131 * @param string $filename
|
Chris@18
|
132 * The name of the file.
|
Chris@18
|
133 * @param \Drupal\Core\Session\AccountInterface $owner
|
Chris@18
|
134 * The owner of the file. Note, it is the responsibility of the caller to
|
Chris@18
|
135 * enforce access.
|
Chris@18
|
136 *
|
Chris@18
|
137 * @return \Drupal\file\FileInterface|\Drupal\Core\Entity\EntityConstraintViolationListInterface
|
Chris@18
|
138 * The newly uploaded file entity, or a list of validation constraint
|
Chris@18
|
139 * violations
|
Chris@18
|
140 *
|
Chris@18
|
141 * @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
Chris@18
|
142 * Thrown when temporary files cannot be written, a lock cannot be acquired,
|
Chris@18
|
143 * or when temporary files cannot be moved to their new location.
|
Chris@18
|
144 */
|
Chris@18
|
145 public function handleFileUploadForField(FieldDefinitionInterface $field_definition, $filename, AccountInterface $owner) {
|
Chris@18
|
146 assert(is_a($field_definition->getClass(), FileFieldItemList::class, TRUE));
|
Chris@18
|
147 $destination = $this->getUploadLocation($field_definition->getSettings());
|
Chris@18
|
148
|
Chris@18
|
149 // Check the destination file path is writable.
|
Chris@18
|
150 if (!$this->fileSystem->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY)) {
|
Chris@18
|
151 throw new HttpException(500, 'Destination file path is not writable');
|
Chris@18
|
152 }
|
Chris@18
|
153
|
Chris@18
|
154 $validators = $this->getUploadValidators($field_definition);
|
Chris@18
|
155
|
Chris@18
|
156 $prepared_filename = $this->prepareFilename($filename, $validators);
|
Chris@18
|
157
|
Chris@18
|
158 // Create the file.
|
Chris@18
|
159 $file_uri = "{$destination}/{$prepared_filename}";
|
Chris@18
|
160
|
Chris@18
|
161 $temp_file_path = $this->streamUploadData();
|
Chris@18
|
162
|
Chris@18
|
163 $file_uri = $this->fileSystem->getDestinationFilename($file_uri, FileSystemInterface::EXISTS_RENAME);
|
Chris@18
|
164
|
Chris@18
|
165 // Lock based on the prepared file URI.
|
Chris@18
|
166 $lock_id = $this->generateLockIdFromFileUri($file_uri);
|
Chris@18
|
167
|
Chris@18
|
168 if (!$this->lock->acquire($lock_id)) {
|
Chris@18
|
169 throw new HttpException(503, sprintf('File "%s" is already locked for writing.'), NULL, ['Retry-After' => 1]);
|
Chris@18
|
170 }
|
Chris@18
|
171
|
Chris@18
|
172 // Begin building file entity.
|
Chris@18
|
173 $file = File::create([]);
|
Chris@18
|
174 $file->setOwnerId($owner->id());
|
Chris@18
|
175 $file->setFilename($prepared_filename);
|
Chris@18
|
176 $file->setMimeType($this->mimeTypeGuesser->guess($prepared_filename));
|
Chris@18
|
177 $file->setFileUri($file_uri);
|
Chris@18
|
178 // Set the size. This is done in File::preSave() but we validate the file
|
Chris@18
|
179 // before it is saved.
|
Chris@18
|
180 $file->setSize(@filesize($temp_file_path));
|
Chris@18
|
181
|
Chris@18
|
182 // Validate the file entity against entity-level validation and field-level
|
Chris@18
|
183 // validators.
|
Chris@18
|
184 $violations = $this->validate($file, $validators);
|
Chris@18
|
185 if ($violations->count() > 0) {
|
Chris@18
|
186 return $violations;
|
Chris@18
|
187 }
|
Chris@18
|
188
|
Chris@18
|
189 // Move the file to the correct location after validation. Use
|
Chris@18
|
190 // FILE_EXISTS_ERROR as the file location has already been determined above
|
Chris@18
|
191 // in FileSystem::getDestinationFilename().
|
Chris@18
|
192 try {
|
Chris@18
|
193 $this->fileSystem->move($temp_file_path, $file_uri, FileSystemInterface::EXISTS_ERROR);
|
Chris@18
|
194 }
|
Chris@18
|
195 catch (FileException $e) {
|
Chris@18
|
196 throw new HttpException(500, 'Temporary file could not be moved to file location');
|
Chris@18
|
197 }
|
Chris@18
|
198
|
Chris@18
|
199 $file->save();
|
Chris@18
|
200
|
Chris@18
|
201 $this->lock->release($lock_id);
|
Chris@18
|
202
|
Chris@18
|
203 return $file;
|
Chris@18
|
204 }
|
Chris@18
|
205
|
Chris@18
|
206 /**
|
Chris@18
|
207 * Validates and extracts the filename from the Content-Disposition header.
|
Chris@18
|
208 *
|
Chris@18
|
209 * @param \Symfony\Component\HttpFoundation\Request $request
|
Chris@18
|
210 * The request object.
|
Chris@18
|
211 *
|
Chris@18
|
212 * @return string
|
Chris@18
|
213 * The filename extracted from the header.
|
Chris@18
|
214 *
|
Chris@18
|
215 * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
Chris@18
|
216 * Thrown when the 'Content-Disposition' request header is invalid.
|
Chris@18
|
217 */
|
Chris@18
|
218 public function validateAndParseContentDispositionHeader(Request $request) {
|
Chris@18
|
219 // First, check the header exists.
|
Chris@18
|
220 if (!$request->headers->has('content-disposition')) {
|
Chris@18
|
221 throw new BadRequestHttpException('"Content-Disposition" header is required. A file name in the format "filename=FILENAME" must be provided.');
|
Chris@18
|
222 }
|
Chris@18
|
223
|
Chris@18
|
224 $content_disposition = $request->headers->get('content-disposition');
|
Chris@18
|
225
|
Chris@18
|
226 // Parse the header value. This regex does not allow an empty filename.
|
Chris@18
|
227 // i.e. 'filename=""'. This also matches on a word boundary so other keys
|
Chris@18
|
228 // like 'not_a_filename' don't work.
|
Chris@18
|
229 if (!preg_match(static::REQUEST_HEADER_FILENAME_REGEX, $content_disposition, $matches)) {
|
Chris@18
|
230 throw new BadRequestHttpException('No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided.');
|
Chris@18
|
231 }
|
Chris@18
|
232
|
Chris@18
|
233 // Check for the "filename*" format. This is currently unsupported.
|
Chris@18
|
234 if (!empty($matches['star'])) {
|
Chris@18
|
235 throw new BadRequestHttpException('The extended "filename*" format is currently not supported in the "Content-Disposition" header.');
|
Chris@18
|
236 }
|
Chris@18
|
237
|
Chris@18
|
238 // Don't validate the actual filename here, that will be done by the upload
|
Chris@18
|
239 // validators in validate().
|
Chris@18
|
240 // @see \Drupal\file\Plugin\rest\resource\FileUploadResource::validate()
|
Chris@18
|
241 $filename = $matches['filename'];
|
Chris@18
|
242
|
Chris@18
|
243 // Make sure only the filename component is returned. Path information is
|
Chris@18
|
244 // stripped as per https://tools.ietf.org/html/rfc6266#section-4.3.
|
Chris@18
|
245 return $this->fileSystem->basename($filename);
|
Chris@18
|
246 }
|
Chris@18
|
247
|
Chris@18
|
248 /**
|
Chris@18
|
249 * Checks if the current user has access to upload the file.
|
Chris@18
|
250 *
|
Chris@18
|
251 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@18
|
252 * The account for which file upload access should be checked.
|
Chris@18
|
253 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
|
Chris@18
|
254 * The field definition for which to get validators.
|
Chris@18
|
255 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@18
|
256 * (optional) The entity to which the file is to be uploaded, if it exists.
|
Chris@18
|
257 * If the entity does not exist and it is not given, create access to the
|
Chris@18
|
258 * file will be checked.
|
Chris@18
|
259 *
|
Chris@18
|
260 * @return \Drupal\Core\Access\AccessResultInterface
|
Chris@18
|
261 * The file upload access result.
|
Chris@18
|
262 */
|
Chris@18
|
263 public static function checkFileUploadAccess(AccountInterface $account, FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL) {
|
Chris@18
|
264 assert(is_null($entity) || $field_definition->getTargetEntityTypeId() === $entity->getEntityTypeId() && $field_definition->getTargetBundle() === $entity->bundle());
|
Chris@18
|
265 $entity_type_manager = \Drupal::entityTypeManager();
|
Chris@18
|
266 $entity_access_control_handler = $entity_type_manager->getAccessControlHandler($field_definition->getTargetEntityTypeId());
|
Chris@18
|
267 $bundle = $entity_type_manager->getDefinition($field_definition->getTargetEntityTypeId())->hasKey('bundle') ? $field_definition->getTargetBundle() : NULL;
|
Chris@18
|
268 $entity_access_result = $entity
|
Chris@18
|
269 ? $entity_access_control_handler->access($entity, 'update', $account, TRUE)
|
Chris@18
|
270 : $entity_access_control_handler->createAccess($bundle, $account, [], TRUE);
|
Chris@18
|
271 $field_access_result = $entity_access_control_handler->fieldAccess('edit', $field_definition, NULL, NULL, TRUE);
|
Chris@18
|
272 return $entity_access_result->andIf($field_access_result);
|
Chris@18
|
273 }
|
Chris@18
|
274
|
Chris@18
|
275 /**
|
Chris@18
|
276 * Streams file upload data to temporary file and moves to file destination.
|
Chris@18
|
277 *
|
Chris@18
|
278 * @return string
|
Chris@18
|
279 * The temp file path.
|
Chris@18
|
280 *
|
Chris@18
|
281 * @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
Chris@18
|
282 * Thrown when input data cannot be read, the temporary file cannot be
|
Chris@18
|
283 * opened, or the temporary file cannot be written.
|
Chris@18
|
284 */
|
Chris@18
|
285 protected function streamUploadData() {
|
Chris@18
|
286 // 'rb' is needed so reading works correctly on Windows environments too.
|
Chris@18
|
287 $file_data = fopen('php://input', 'rb');
|
Chris@18
|
288
|
Chris@18
|
289 $temp_file_path = $this->fileSystem->tempnam('temporary://', 'file');
|
Chris@18
|
290 $temp_file = fopen($temp_file_path, 'wb');
|
Chris@18
|
291
|
Chris@18
|
292 if ($temp_file) {
|
Chris@18
|
293 while (!feof($file_data)) {
|
Chris@18
|
294 $read = fread($file_data, static::BYTES_TO_READ);
|
Chris@18
|
295
|
Chris@18
|
296 if ($read === FALSE) {
|
Chris@18
|
297 // Close the file streams.
|
Chris@18
|
298 fclose($temp_file);
|
Chris@18
|
299 fclose($file_data);
|
Chris@18
|
300 $this->logger->error('Input data could not be read');
|
Chris@18
|
301 throw new HttpException(500, 'Input file data could not be read.');
|
Chris@18
|
302 }
|
Chris@18
|
303
|
Chris@18
|
304 if (fwrite($temp_file, $read) === FALSE) {
|
Chris@18
|
305 // Close the file streams.
|
Chris@18
|
306 fclose($temp_file);
|
Chris@18
|
307 fclose($file_data);
|
Chris@18
|
308 $this->logger->error('Temporary file data for "%path" could not be written', ['%path' => $temp_file_path]);
|
Chris@18
|
309 throw new HttpException(500, 'Temporary file data could not be written.');
|
Chris@18
|
310 }
|
Chris@18
|
311 }
|
Chris@18
|
312
|
Chris@18
|
313 // Close the temp file stream.
|
Chris@18
|
314 fclose($temp_file);
|
Chris@18
|
315 }
|
Chris@18
|
316 else {
|
Chris@18
|
317 // Close the input file stream since we can't proceed with the upload.
|
Chris@18
|
318 // Don't try to close $temp_file since it's FALSE at this point.
|
Chris@18
|
319 fclose($file_data);
|
Chris@18
|
320 $this->logger->error('Temporary file "%path" could not be opened for file upload.', ['%path' => $temp_file_path]);
|
Chris@18
|
321 throw new HttpException(500, 'Temporary file could not be opened');
|
Chris@18
|
322 }
|
Chris@18
|
323
|
Chris@18
|
324 // Close the input stream.
|
Chris@18
|
325 fclose($file_data);
|
Chris@18
|
326
|
Chris@18
|
327 return $temp_file_path;
|
Chris@18
|
328 }
|
Chris@18
|
329
|
Chris@18
|
330 /**
|
Chris@18
|
331 * Validates the file.
|
Chris@18
|
332 *
|
Chris@18
|
333 * @param \Drupal\file\FileInterface $file
|
Chris@18
|
334 * The file entity to validate.
|
Chris@18
|
335 * @param array $validators
|
Chris@18
|
336 * An array of upload validators to pass to file_validate().
|
Chris@18
|
337 *
|
Chris@18
|
338 * @return \Drupal\Core\Entity\EntityConstraintViolationListInterface
|
Chris@18
|
339 * The list of constraint violations, if any.
|
Chris@18
|
340 */
|
Chris@18
|
341 protected function validate(FileInterface $file, array $validators) {
|
Chris@18
|
342 $violations = $file->validate();
|
Chris@18
|
343
|
Chris@18
|
344 // Remove violations of inaccessible fields as they cannot stem from our
|
Chris@18
|
345 // changes.
|
Chris@18
|
346 $violations->filterByFieldAccess();
|
Chris@18
|
347
|
Chris@18
|
348 // Validate the file based on the field definition configuration.
|
Chris@18
|
349 $errors = file_validate($file, $validators);
|
Chris@18
|
350 if (!empty($errors)) {
|
Chris@18
|
351 $translator = new DrupalTranslator();
|
Chris@18
|
352 foreach ($errors as $error) {
|
Chris@18
|
353 $violation = new ConstraintViolation($translator->trans($error),
|
Chris@18
|
354 $error,
|
Chris@18
|
355 [],
|
Chris@18
|
356 EntityAdapter::createFromEntity($file),
|
Chris@18
|
357 '',
|
Chris@18
|
358 NULL
|
Chris@18
|
359 );
|
Chris@18
|
360 $violations->add($violation);
|
Chris@18
|
361 }
|
Chris@18
|
362 }
|
Chris@18
|
363
|
Chris@18
|
364 return $violations;
|
Chris@18
|
365 }
|
Chris@18
|
366
|
Chris@18
|
367 /**
|
Chris@18
|
368 * Prepares the filename to strip out any malicious extensions.
|
Chris@18
|
369 *
|
Chris@18
|
370 * @param string $filename
|
Chris@18
|
371 * The file name.
|
Chris@18
|
372 * @param array $validators
|
Chris@18
|
373 * The array of upload validators.
|
Chris@18
|
374 *
|
Chris@18
|
375 * @return string
|
Chris@18
|
376 * The prepared/munged filename.
|
Chris@18
|
377 */
|
Chris@18
|
378 protected function prepareFilename($filename, array &$validators) {
|
Chris@18
|
379 if (!empty($validators['file_validate_extensions'][0])) {
|
Chris@18
|
380 // If there is a file_validate_extensions validator and a list of
|
Chris@18
|
381 // valid extensions, munge the filename to protect against possible
|
Chris@18
|
382 // malicious extension hiding within an unknown file type. For example,
|
Chris@18
|
383 // "filename.html.foo".
|
Chris@18
|
384 $filename = file_munge_filename($filename, $validators['file_validate_extensions'][0]);
|
Chris@18
|
385 }
|
Chris@18
|
386
|
Chris@18
|
387 // Rename potentially executable files, to help prevent exploits (i.e. will
|
Chris@18
|
388 // rename filename.php.foo and filename.php to filename.php.foo.txt and
|
Chris@18
|
389 // filename.php.txt, respectively). Don't rename if 'allow_insecure_uploads'
|
Chris@18
|
390 // evaluates to TRUE.
|
Chris@18
|
391 if (!$this->systemFileConfig->get('allow_insecure_uploads') && preg_match(FILE_INSECURE_EXTENSION_REGEX, $filename) && (substr($filename, -4) != '.txt')) {
|
Chris@18
|
392 // The destination filename will also later be used to create the URI.
|
Chris@18
|
393 $filename .= '.txt';
|
Chris@18
|
394
|
Chris@18
|
395 // The .txt extension may not be in the allowed list of extensions. We
|
Chris@18
|
396 // have to add it here or else the file upload will fail.
|
Chris@18
|
397 if (!empty($validators['file_validate_extensions'][0])) {
|
Chris@18
|
398 $validators['file_validate_extensions'][0] .= ' txt';
|
Chris@18
|
399 }
|
Chris@18
|
400 }
|
Chris@18
|
401
|
Chris@18
|
402 return $filename;
|
Chris@18
|
403 }
|
Chris@18
|
404
|
Chris@18
|
405 /**
|
Chris@18
|
406 * Determines the URI for a file field.
|
Chris@18
|
407 *
|
Chris@18
|
408 * @param array $settings
|
Chris@18
|
409 * The array of field settings.
|
Chris@18
|
410 *
|
Chris@18
|
411 * @return string
|
Chris@18
|
412 * An un-sanitized file directory URI with tokens replaced. The result of
|
Chris@18
|
413 * the token replacement is then converted to plain text and returned.
|
Chris@18
|
414 */
|
Chris@18
|
415 protected function getUploadLocation(array $settings) {
|
Chris@18
|
416 $destination = trim($settings['file_directory'], '/');
|
Chris@18
|
417
|
Chris@18
|
418 // Replace tokens. As the tokens might contain HTML we convert it to plain
|
Chris@18
|
419 // text.
|
Chris@18
|
420 $destination = PlainTextOutput::renderFromHtml($this->token->replace($destination, [], [], new BubbleableMetadata()));
|
Chris@18
|
421 return $settings['uri_scheme'] . '://' . $destination;
|
Chris@18
|
422 }
|
Chris@18
|
423
|
Chris@18
|
424 /**
|
Chris@18
|
425 * Retrieves the upload validators for a field definition.
|
Chris@18
|
426 *
|
Chris@18
|
427 * This is copied from \Drupal\file\Plugin\Field\FieldType\FileItem as there
|
Chris@18
|
428 * is no entity instance available here that that a FileItem would exist for.
|
Chris@18
|
429 *
|
Chris@18
|
430 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
|
Chris@18
|
431 * The field definition for which to get validators.
|
Chris@18
|
432 *
|
Chris@18
|
433 * @return array
|
Chris@18
|
434 * An array suitable for passing to file_save_upload() or the file field
|
Chris@18
|
435 * element's '#upload_validators' property.
|
Chris@18
|
436 */
|
Chris@18
|
437 protected function getUploadValidators(FieldDefinitionInterface $field_definition) {
|
Chris@18
|
438 $validators = [
|
Chris@18
|
439 // Add in our check of the file name length.
|
Chris@18
|
440 'file_validate_name_length' => [],
|
Chris@18
|
441 ];
|
Chris@18
|
442 $settings = $field_definition->getSettings();
|
Chris@18
|
443
|
Chris@18
|
444 // Cap the upload size according to the PHP limit.
|
Chris@18
|
445 $max_filesize = Bytes::toInt(Environment::getUploadMaxSize());
|
Chris@18
|
446 if (!empty($settings['max_filesize'])) {
|
Chris@18
|
447 $max_filesize = min($max_filesize, Bytes::toInt($settings['max_filesize']));
|
Chris@18
|
448 }
|
Chris@18
|
449
|
Chris@18
|
450 // There is always a file size limit due to the PHP server limit.
|
Chris@18
|
451 $validators['file_validate_size'] = [$max_filesize];
|
Chris@18
|
452
|
Chris@18
|
453 // Add the extension check if necessary.
|
Chris@18
|
454 if (!empty($settings['file_extensions'])) {
|
Chris@18
|
455 $validators['file_validate_extensions'] = [$settings['file_extensions']];
|
Chris@18
|
456 }
|
Chris@18
|
457
|
Chris@18
|
458 return $validators;
|
Chris@18
|
459 }
|
Chris@18
|
460
|
Chris@18
|
461 /**
|
Chris@18
|
462 * Generates a lock ID based on the file URI.
|
Chris@18
|
463 *
|
Chris@18
|
464 * @param string $file_uri
|
Chris@18
|
465 * The file URI.
|
Chris@18
|
466 *
|
Chris@18
|
467 * @return string
|
Chris@18
|
468 * The generated lock ID.
|
Chris@18
|
469 */
|
Chris@18
|
470 protected static function generateLockIdFromFileUri($file_uri) {
|
Chris@18
|
471 return 'file:jsonapi:' . Crypt::hashBase64($file_uri);
|
Chris@18
|
472 }
|
Chris@18
|
473
|
Chris@18
|
474 }
|