Skip to content

Commit

Permalink
better media type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Apr 3, 2024
1 parent bf9730d commit b9c85b5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/Enums/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum MediaType: string
case Pdf = 'pdf';
case Other = 'other';

public static function tryFromMimeType(string $mimeType)
public static function tryFromMimeType(string $mimeType): self
{
if (str_starts_with($mimeType, 'image/')) {
return self::Image;
Expand All @@ -35,16 +35,16 @@ public static function tryFromMimeType(string $mimeType)
}

/**
* Some codec like 3GPP files can contain either audios or videos
* Some codec like 3GPP or MOV files can contain either audios or videos
* To determine the true type, we need to check which stream is defined
*/
public static function tryFromStreams(string $path)
public static function tryFromStreams(string $path): self
{
$type = self::tryFromMimeType(File::mimeType($path));

if (
$type === self::Video ||
$type === self::Audio
$type === self::Audio
) {
$ffprobe = FFProbe::create([
'ffmpeg.binaries' => config('laravel-ffmpeg.ffmpeg.binaries'),
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public static function duration(string $path): ?float
return null;
}

public static function dimension(string $path, ?MediaType $type = null, ?string $mime_type = null): ?Dimension
public static function dimension(string $path): ?Dimension
{
$type ??= (MediaType::tryFromMimeType($mime_type) ?? static::type($path));
$type = static::type($path);

return match ($type) {
MediaType::Video => Video::dimension($path),
Expand Down
9 changes: 5 additions & 4 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,9 @@ public function storeFileFromHttpFile(
$this->mime_type = File::mimeType($file);
$this->extension = File::extension($file);
$this->size = $file->getSize();
$this->type = MediaType::tryFromMimeType($this->mime_type);
$this->type = File::type($file->getPathname());

$dimension = File::dimension($file->getPathname(), type: $this->type);
$dimension = File::dimension($file->getPathname());

$this->height = $dimension?->getHeight();
$this->width = $dimension?->getWidth();
Expand Down Expand Up @@ -508,7 +508,7 @@ public function storeConversionFromHttpFile(
$file_name = "{$name}.{$extension}";
$mime_type = File::mimeType($file);
$type = File::type($file->getPathname());
$dimension = File::dimension($file->getPathname(), type: $type);
$dimension = File::dimension($file->getPathname());

$existingConversion = $this->getGeneratedConversion($conversion);

Expand Down Expand Up @@ -546,7 +546,8 @@ public function storeConversionFromHttpFile(
public function getResponsiveImages(?string $conversion = null): Collection
{
return collect(ResponsiveImagesConversionsPreset::$widths)
->when($conversion,
->when(
$conversion,
fn (Collection $collection) => $collection->map(fn (int $width) => $this->getGeneratedConversion("{$conversion}.{$width}")),
fn (Collection $collection) => $collection->map(fn (int $width) => $this->getGeneratedConversion($width)),
)
Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/FileTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Finller\Media\Enums\MediaType;
use Finller\Media\Helpers\File;
use Illuminate\Http\UploadedFile;

Expand All @@ -11,3 +12,12 @@

expect($name)->toBe('foo');
});

it('identity a mov file without video as an Audio', function () {

$file = $this->getTestFile('audios/audio-only.mov');

$type = File::type($file);

expect($type)->toBe(MediaType::Audio);
});
Binary file added tests/files/audios/audio-only.mov
Binary file not shown.

0 comments on commit b9c85b5

Please sign in to comment.