Skip to content

Commit 7adf74f

Browse files
committed
fix(file-upldoad): Validate files in the correct order
1 parent 92d3ec4 commit 7adf74f

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

src/GraphQL/Utility/FileUpload.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,20 @@ public function saveFileUpload(UploadedFile $uploaded_file, array $settings): Fi
241241
$file->setOwnerId($this->currentUser->id());
242242
$file->setFilename($prepared_filename);
243243
$file->setMimeType($this->mimeTypeGuesser->guess($prepared_filename));
244-
$file->setFileUri($file_uri);
244+
$file->setFileUri($temp_file_path);
245245
// Set the size. This is done in File::preSave() but we validate the file
246246
// before it is saved.
247247
$file->setSize(@filesize($temp_file_path));
248248

249-
// Validate the file entity against entity-level validation and
250-
// field-level validators.
251-
if (!$this->validate($file, $validators, $response)) {
249+
// Validate against file_validate() first with the temporary path.
250+
$errors = file_validate($file, $validators);
251+
252+
if (!empty($errors)) {
253+
$response->addViolations($errors);
252254
return $response;
253255
}
254256

257+
$file->setFileUri($file_uri);
255258
// Move the file to the correct location after validation. Use
256259
// FileSystemInterface::EXISTS_ERROR as the file location has already been
257260
// determined above in FileSystem::getDestinationFilename().
@@ -269,6 +272,12 @@ public function saveFileUpload(UploadedFile $uploaded_file, array $settings): Fi
269272
return $response;
270273
}
271274

275+
// Validate the file entity against entity-level validation now after the
276+
// file has moved.
277+
if (!$this->validate($file, $validators, $response)) {
278+
return $response;
279+
}
280+
272281
$file->save();
273282

274283
$response->setFileEntity($file);
@@ -347,14 +356,6 @@ protected function validate(FileInterface $file, array $validators, FileUploadRe
347356
}
348357
}
349358

350-
// Validate the file based on the field definition configuration.
351-
$errors = file_validate($file, $validators);
352-
353-
if (!empty($errors)) {
354-
$response->addViolations($errors);
355-
return FALSE;
356-
}
357-
358359
return TRUE;
359360
}
360361

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type: module
2+
name: GraphQL File Validate Test
3+
description: Tests hook_file_validate() on uploads.
4+
package: Testing
5+
core_version_requirement: ^8 || ^9
6+
hidden: TRUE
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Test module for file validation.
6+
*/
7+
8+
use Drupal\file\FileInterface;
9+
10+
/**
11+
* Implements hook_file_validate().
12+
*/
13+
function graphql_file_validate(FileInterface $file) {
14+
if (!file_exists($file->getFileUri())) {
15+
throw new \Exception('File does not exist during validation: ' . $file->getFileUri());
16+
}
17+
}

tests/src/Kernel/Framework/UploadFileServiceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class UploadFileServiceTest extends GraphQLTestBase {
1818
/**
1919
* {@inheritdoc}
2020
*/
21-
protected static $modules = ['file'];
21+
protected static $modules = ['file', 'graphql_file_validate'];
2222

2323
/**
2424
* The FileUpload object we want to test, gets prepared in setUp().

0 commit comments

Comments
 (0)