Skip to content

Commit

Permalink
Merge pull request #500 from nutgram/fix-mixin-tmp-file
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasss93 committed Jun 23, 2023
2 parents 6896832 + 79909e1 commit 6a38eae
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Laravel/Mixins/MixinUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace SergiX44\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 @@ -35,13 +34,23 @@ public static function saveFileToDisk(File $file, string $path, ?string $disk =
}

//create temp file
$tmpFile = tempnam(sys_get_temp_dir(), uniqid(strftime('%G-%m-%d')));
$maxMemory = 20 * 1024 * 1024;
$tmpFile = fopen(sprintf("php://temp/maxmemory:%d", $maxMemory), 'wb+');

//download file to temp file
$http = $file->getContainer()->get(ClientInterface::class);
$http->get($file->downloadUrl($file), array_merge(['sink' => $tmpFile], $clientOpt));
$response = $http->get($file->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 6a38eae

Please sign in to comment.