Skip to content
Snippets Groups Projects

Don't load the whole file into memory

2 unresolved threads
Files
3
@@ -138,11 +138,15 @@ class PhysicalFile extends ImportProcessorPluginBase implements PluginFormInterf
if ($this->fileSystem->prepareDirectory($directory_uri, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
try {
$response = $this->remoteManager->request($runtime_import_context->getRemote(), 'GET', $remote_file_url);
$file_content = (string) $response->getBody();
$result = @file_put_contents($file_destination, $file_content);
if (!$result) {
throw new \Exception('Error writing file to ' . $file_destination);
if (!$response) {
throw new \Exception('Error request to ' . $remote_file_url);
}
$local_file = fopen($file_destination, 'w');
Please register or sign in to reply
while (!$response->getBody()->eof()) {
fwrite($local_file, $response->getBody()->read(100000));
}
fclose($local_file);
}
catch (ClientException $e) {
$this->logger->warning('Error importing file id %id. Missing file: %url', $log_variables);
@@ -151,7 +155,8 @@ class PhysicalFile extends ImportProcessorPluginBase implements PluginFormInterf
catch (\Throwable $e) {
$log_variables['@msg'] = $e->getMessage();
$this->logger->error('Caught exception trying to import the file %url to %uri. Error message was @msg', $log_variables);
$this->messenger()->addError($this->t('Caught exception trying to import the file %url to %uri', $log_variables));
$this->messenger()
->addError($this->t('Caught exception trying to import the file %url to %uri', $log_variables));
}
}
else {
Loading