Skip to content

Commit

Permalink
databox: removebg fix cache on error
Browse files Browse the repository at this point in the history
  • Loading branch information
4rthem committed Nov 18, 2024
1 parent 5028a63 commit 51edc61
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions databox/api/src/Integration/RemoveBg/RemoveBgClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand Down

0 comments on commit 51edc61

Please sign in to comment.