Skip to content

Instantly share code, notes, and snippets.

@IT-Cru
Created September 1, 2017 13:38
Show Gist options
  • Save IT-Cru/866dc78a766821121c8559f08456f2ec to your computer and use it in GitHub Desktop.
Save IT-Cru/866dc78a766821121c8559f08456f2ec to your computer and use it in GitHub Desktop.
DCX Integration: Optimize file create
diff --git a/modules/dcx_migration/src/Plugin/migrate/process/FileFromURL.php b/modules/dcx_migration/src/Plugin/migrate/process/FileFromURL.php
index c753e60..e9266ab 100644
--- a/modules/dcx_migration/src/Plugin/migrate/process/FileFromURL.php
+++ b/modules/dcx_migration/src/Plugin/migrate/process/FileFromURL.php
@@ -124,14 +124,20 @@ class FileFromUrl extends ProcessPluginBase implements ContainerFactoryPluginInt
// Remove.
unlink($tmp_name);
- $file = File::create([
- 'uri' => $uri,
- 'filename' => $file_name,
- ]);
-
- $file->save();
-
- return $file->id();
+ // Create file entity.
+ $file = File::create();
+ $file->setFileUri($uri);
+ $file->setOwnerId(\Drupal::currentUser()->id());
+ $file->setMimeType(\Drupal::service('file.mime_type.guesser')->guess($uri));
+ $file->setFileName($file_name);
+ $file->setTemporary();
+
+ if (empty(file_validate($file))) {
+ $file->setPermanent();
+ $file->save();
+ return $file->id();
+ }
+ return FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment