diff --git a/lib/private/TextToImage/Manager.php b/lib/private/TextToImage/Manager.php index a48b202239d0a..f7abc791a2d44 100644 --- a/lib/private/TextToImage/Manager.php +++ b/lib/private/TextToImage/Manager.php @@ -147,8 +147,11 @@ public function runTask(Task $task): void { } $this->logger->debug('Creating result files for Text2Image task'); $resources = []; + $files = []; for ($i = 0; $i < $task->getNumberOfImages(); $i++) { - $resources[] = $folder->newFile((string) $i)->write(); + $file = $folder->newFile((string) $i); + $files[] = $file; + $resources[] = $file->write(); if ($resource[count($resources) - 1] === false) { throw new RuntimeException('Text2Image generation using provider ' . $provider->getName() . ' failed: Couldn\'t open file to write.'); } @@ -158,7 +161,7 @@ public function runTask(Task $task): void { for ($i = 0; $i < $task->getNumberOfImages(); $i++) { if (is_resource($resources[$i])) { // If $resource hasn't been closed yet, we'll do that here - fclose($resource[$i]); + fclose($resources[$i]); } } $task->setStatus(Task::STATUS_SUCCESSFUL); @@ -166,17 +169,20 @@ public function runTask(Task $task): void { $this->taskMapper->update(DbTask::fromPublicTask($task)); return; } catch (\RuntimeException|\Throwable $e) { - if (isset($resource) && is_resource($resource)) { - // If $resource hasn't been closed yet, we'll do that here - fclose($resource); - } - try { - if (isset($file)) { - $file->delete(); + for ($i = 0; $i < $task->getNumberOfImages(); $i++) { + if (isset($resources[$i]) && is_resource($resources[$i])) { + // If $resource hasn't been closed yet, we'll do that here + fclose($resources[$i]); + } + if (isset($files, $files[$i])) { + try { + $files[$i]->delete(); + } catch(NotPermittedException $e) { + $this->logger->warning('Failed to clean up Text2Image result file after error', ['exception' => $e]); + } } - } catch(NotPermittedException $e) { - $this->logger->warning('Failed to clean up Text2Image result file after error', ['exception' => $e]); } + $this->logger->info('Text2Image generation using provider ' . $provider->getName() . ' failed', ['exception' => $e]); $task->setStatus(Task::STATUS_FAILED); try {