From 817fb1fe4ed4e68bae87de853edfc1411659b0c0 Mon Sep 17 00:00:00 2001 From: Luca Patera Date: Fri, 23 Jun 2023 23:03:17 +0200 Subject: [PATCH] Fix missing temp file deletion when using the mixin method --- src/Mixins/MixinUtils.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Mixins/MixinUtils.php b/src/Mixins/MixinUtils.php index 95af653..73b1f4f 100644 --- a/src/Mixins/MixinUtils.php +++ b/src/Mixins/MixinUtils.php @@ -2,7 +2,6 @@ namespace Nutgram\Laravel\Mixins; -use Illuminate\Http\File as LaravelFile; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Psr\Container\ContainerExceptionInterface; @@ -42,13 +41,23 @@ public static function saveFileToDisk(File $file, string $path, ?string $disk = } //create temp file - $tmpFile = tempnam(sys_get_temp_dir(), uniqid(time(), true)); + $maxMemory = 20 * 1024 * 1024; + $tmpFile = fopen(sprintf("php://temp/maxmemory:%d", $maxMemory), 'wb+'); //download file to temp file $http = $bot->getContainer()->get(ClientInterface::class); - $http->get($bot->downloadUrl($file), array_merge(['sink' => $tmpFile], $clientOpt)); + $response = $http->get($bot->downloadUrl($file), array_merge(['sink' => $tmpFile], $clientOpt)); + + //detach resource + $response->getBody()->detach(); //save temp file to disk - return $storage->putFileAs('/', new LaravelFile($tmpFile), $path); + $result = $storage->put($path, $tmpFile); + + //close temp file + fclose($tmpFile); + + //return result + return $result; } }