diff --git a/app/Lib/CafelApi/Models/CafelClientModel.php b/app/Lib/CafelApi/Models/CafelClientModel.php index b03aa10..47d6f5d 100644 --- a/app/Lib/CafelApi/Models/CafelClientModel.php +++ b/app/Lib/CafelApi/Models/CafelClientModel.php @@ -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; } } diff --git a/app/Lib/FileHelper.php b/app/Lib/FileHelper.php index e0c7335..38320cc 100644 --- a/app/Lib/FileHelper.php +++ b/app/Lib/FileHelper.php @@ -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")); } }