@@ -223,46 +223,51 @@ public function saveFileUpload(UploadedFile $uploaded_file, array $settings): Fi
223223 return $ response ;
224224 }
225225
226- // Begin building file entity.
227- /** @var \Drupal\file\FileInterface $file */
228- $ file = $ this ->fileStorage ->create ([]);
229- $ file ->setOwnerId ($ this ->currentUser ->id ());
230- $ file ->setFilename ($ prepared_filename );
231- $ file ->setMimeType ($ this ->mimeTypeGuesser ->guess ($ prepared_filename ));
232- $ file ->setFileUri ($ file_uri );
233- // Set the size. This is done in File::preSave() but we validate the file
234- // before it is saved.
235- $ file ->setSize (@filesize ($ temp_file_path ));
236-
237- // Validate the file entity against entity-level validation and field-level
238- // validators.
239- if (!$ this ->validate ($ file , $ validators , $ response )) {
240- return $ response ;
241- }
242-
243- // Move the file to the correct location after validation. Use
244- // FileSystemInterface::EXISTS_ERROR as the file location has already been
245- // determined above in FileSystem::getDestinationFilename().
246226 try {
247- $ this ->fileSystem ->move ($ temp_file_path , $ file_uri , FileSystemInterface::EXISTS_ERROR );
248- }
249- catch (FileException $ e ) {
250- $ response ->addViolation ($ this ->t ('Unknown error while uploading the file "@file". ' , [
251- '@file ' => $ uploaded_file ->getClientOriginalName (),
252- ]));
253- $ this ->logger ->error ('Unable to move file from "@file" to "@destination". ' , [
254- '@file ' => $ uploaded_file ->getRealPath (),
255- '@destination ' => $ file ->getFileUri (),
256- ]);
257- return $ response ;
258- }
227+ // Begin building file entity.
228+ /** @var \Drupal\file\FileInterface $file */
229+ $ file = $ this ->fileStorage ->create ([]);
230+ $ file ->setOwnerId ($ this ->currentUser ->id ());
231+ $ file ->setFilename ($ prepared_filename );
232+ $ file ->setMimeType ($ this ->mimeTypeGuesser ->guess ($ prepared_filename ));
233+ $ file ->setFileUri ($ file_uri );
234+ // Set the size. This is done in File::preSave() but we validate the file
235+ // before it is saved.
236+ $ file ->setSize (@filesize ($ temp_file_path ));
237+
238+ // Validate the file entity against entity-level validation and
239+ // field-level validators.
240+ if (!$ this ->validate ($ file , $ validators , $ response )) {
241+ return $ response ;
242+ }
259243
260- $ file ->save ();
244+ // Move the file to the correct location after validation. Use
245+ // FileSystemInterface::EXISTS_ERROR as the file location has already been
246+ // determined above in FileSystem::getDestinationFilename().
247+ try {
248+ $ this ->fileSystem ->move ($ temp_file_path , $ file_uri , FileSystemInterface::EXISTS_ERROR );
249+ }
250+ catch (FileException $ e ) {
251+ $ response ->addViolation ($ this ->t ('Unknown error while uploading the file "@file". ' , [
252+ '@file ' => $ uploaded_file ->getClientOriginalName (),
253+ ]));
254+ $ this ->logger ->error ('Unable to move file from "@file" to "@destination". ' , [
255+ '@file ' => $ uploaded_file ->getRealPath (),
256+ '@destination ' => $ file ->getFileUri (),
257+ ]);
258+ return $ response ;
259+ }
261260
262- $ this -> lock -> release ( $ lock_id );
261+ $ file -> save ( );
263262
264- $ response ->setFileEntity ($ file );
265- return $ response ;
263+ $ response ->setFileEntity ($ file );
264+ return $ response ;
265+ }
266+ finally {
267+ // This will always be executed before any return statement or exception
268+ // in the try {} block.
269+ $ this ->lock ->release ($ lock_id );
270+ }
266271 }
267272
268273 /**
0 commit comments