Fix file descriptor overload on fetch Cafel Breeders pictures

This commit is contained in:
Jérémy Munsch 2022-12-06 10:24:00 +01:00
parent f8a6bf967b
commit b94ca32ebc
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
2 changed files with 17 additions and 16 deletions

View file

@ -228,15 +228,25 @@ abstract class CafelClientModel implements \JsonSerializable
// * Filter Keys using intersection
\extract(\array_intersect_key($response->body, ['Ext' => '', 'Name' => '', 'Data' => '']));
// * Create temp file and get its absolute path
if (!($tempFile = tmpfile())) {
new \RuntimeException('Could not create temp file');
}
return ToolboxHelper::storeImage(
$filePath = ToolboxHelper::storeImage(
$folder,
FileHelper::fromBase64(
$tempFile,
Str::lower($Name),
Str::lower($Ext),
$Data
),
false
);
// * Closing the file will cause to remove it from temp dir!
fclose($tempFile);
return $filePath;
}
}

View file

@ -51,21 +51,18 @@ class FileHelper
}
/**
* Generate from blob
* Generate from base64 blob
*
* @param string $fileName
* @param string $extension
* @param string $base64String
* @param resource $tempFile A temp file where to store data.
* @param string $fileName The target filename.
* @param string $extension The file extension to use.
* @param string $base64String The base64 encode picture.
* @return SplFileInfo
* @throws \ErrorException If writing to temp file failed.
* @throws \App\Exceptions\CafelApiImageFetchException If invalid image was given or ext could not be confirmed.
*/
public static function fromBase64(string $fileName, string $extension, string $base64String): SplFileInfo
public static function fromBase64($tempFile, string $fileName, string $extension, string $base64String): SplFileInfo
{
// * Create temp file and get its absolute path
if (!($tempFile = tmpfile())) {
new \ErrorException('Could not create temp file');
}
$tempFilePath = stream_get_meta_data($tempFile)['uri'];
if (!($blob = \base64_decode($base64String, true))) {
@ -93,12 +90,6 @@ class FileHelper
throw new \ErrorException('Could not write on temp file');
}
// Close this file after response is sent.
// Closing the file will cause to remove it from temp director!
app()->terminating(function () use ($tempFile) {
fclose($tempFile);
});
return new SplFileInfo(\storage_path("tmp/$filePath.$extension"));
}
}