Skip to content

Commit

Permalink
Fix missing temp file deletion when using the mixin method
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasss93 committed Jun 23, 2023
1 parent 9982af4 commit 817fb1f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Mixins/MixinUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 817fb1f

Please sign in to comment.