Skip to content

Commit 817fb1f

Browse files
committed
Fix missing temp file deletion when using the mixin method
1 parent 9982af4 commit 817fb1f

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/Mixins/MixinUtils.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Nutgram\Laravel\Mixins;
44

5-
use Illuminate\Http\File as LaravelFile;
65
use Illuminate\Support\Facades\Storage;
76
use Illuminate\Support\Str;
87
use Psr\Container\ContainerExceptionInterface;
@@ -42,13 +41,23 @@ public static function saveFileToDisk(File $file, string $path, ?string $disk =
4241
}
4342

4443
//create temp file
45-
$tmpFile = tempnam(sys_get_temp_dir(), uniqid(time(), true));
44+
$maxMemory = 20 * 1024 * 1024;
45+
$tmpFile = fopen(sprintf("php://temp/maxmemory:%d", $maxMemory), 'wb+');
4646

4747
//download file to temp file
4848
$http = $bot->getContainer()->get(ClientInterface::class);
49-
$http->get($bot->downloadUrl($file), array_merge(['sink' => $tmpFile], $clientOpt));
49+
$response = $http->get($bot->downloadUrl($file), array_merge(['sink' => $tmpFile], $clientOpt));
50+
51+
//detach resource
52+
$response->getBody()->detach();
5053

5154
//save temp file to disk
52-
return $storage->putFileAs('/', new LaravelFile($tmpFile), $path);
55+
$result = $storage->put($path, $tmpFile);
56+
57+
//close temp file
58+
fclose($tmpFile);
59+
60+
//return result
61+
return $result;
5362
}
5463
}

0 commit comments

Comments
 (0)