Reputation: 126
There is an issue with one of my jobs, where I'm using a code block
$mimeType = \File::mimeType($localFile); // $localFile - absolute path of the file
This brings an memory issues with message below
local.ERROR: Allowed memory size of 134217728 bytes exhausted (tried to allocate 156161944 bytes) {"exception":"[object] (Symfony\Component\Debug\Exception\FatalErrorException(code: 1): Allowed memory size of 134217728 bytes exhausted (tried to allocate 156161944 bytes) at /var/www/project/vendor/league/flysystem/src/Util/MimeType.php:209)
It's clear, that by increasing memory limit this issue will be resolved, but I'm looking for something more, let's say flexible or elegant way of detecting file mime type like I have done for file upload to s3 and avoids memory issues using below code
$disk = Storage::disk('s3');
$disk->put($path, fopen($localFile, 'r+'));
Is there any way to detect mime type without increasing memory limit?
Thanks
Upvotes: 1
Views: 1916
Reputation: 1135
So it looks like the root cause is coming from the encoding of libmagic. For v1 of Flysystems(which laravel is currently using in V8) they added a dependency that you can use to get the mimetype by file type, which should resolve the issue.
Original Issue - https://github.com/thephpleague/flysystem/issues/1172
Dependency - https://github.com/thephpleague/mime-type-detection
So you would include the new detector and use one of the different types to get the mime type without relying on libmagic.
$detector = new \League\MimeTypeDetection\FinfoMimeTypeDetector();
$mimeType = $detector->detectMimeTypeFromPath($localFile);
Storage::put($path, $contents, ['mimetype' => $mimeType]);
Upvotes: 2