From 51edc617686ce282f16ac212284bd23f63414c69 Mon Sep 17 00:00:00 2001 From: Arthur de Moulins Date: Mon, 18 Nov 2024 10:27:30 +0100 Subject: [PATCH] databox: removebg fix cache on error --- .../src/Integration/RemoveBg/RemoveBgClient.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/databox/api/src/Integration/RemoveBg/RemoveBgClient.php b/databox/api/src/Integration/RemoveBg/RemoveBgClient.php index 88f30a8dd..41fc94059 100644 --- a/databox/api/src/Integration/RemoveBg/RemoveBgClient.php +++ b/databox/api/src/Integration/RemoveBg/RemoveBgClient.php @@ -24,7 +24,7 @@ public function getBgRemoved(File $file, string $apiKey): string } $path = $this->fileFetcher->getFile($file); - $md5 = md5_file($path); + $md5 = md5_file($path).'-v2'; $cacheFile = sprintf('%s/%s', $this->cacheDir, $md5); if (file_exists($cacheFile)) { @@ -41,11 +41,17 @@ public function getBgRemoved(File $file, string $apiKey): string ], ]); - $fileHandler = fopen($cacheFile, 'w'); - foreach ($this->removeBgClient->stream($res) as $chunk) { - fwrite($fileHandler, $chunk->getContent()); + try { + $fileHandler = fopen($cacheFile, 'w'); + foreach ($this->removeBgClient->stream($res) as $chunk) { + fwrite($fileHandler, $chunk->getContent()); + } + fclose($fileHandler); + } catch (\Throwable $e) { + fclose($fileHandler); + @unlink($cacheFile); + throw $e; } - fclose($fileHandler); return $cacheFile; }