Skip to content

Commit

Permalink
(FIX) [URL] Fine tuned the mime type for video and audio media
Browse files Browse the repository at this point in the history
  • Loading branch information
JibayMcs committed Aug 13, 2024
1 parent 9816e07 commit ad075c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions resources/views/actions/media-modal-content.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@

<audio x-ref="mediaFrame" class="rounded-lg w-full" controls @canplaythrough="loading = false"
@loadeddata="loading = false">
<source src="{{ $media }}" type="audio/{{ pathinfo($media, PATHINFO_EXTENSION) }}">
<source src="{{ $media }}" type="{{ $mime }}">
Your browser does not support the audio element.
</audio>

@elseif ($mediaType === 'video')

<video x-ref="mediaFrame" class="rounded-lg" width="100%" style="aspect-ratio: 16 / 9;" controls
@canplaythrough="loading = false">
<source src="{{ $media }}" type="video/{{ pathinfo($media, PATHINFO_EXTENSION) }}">
<source src="{{ $media }}" type="{{ $mime }}">
Your browser does not support the video tag.
</video>

Expand Down
22 changes: 18 additions & 4 deletions src/Concerns/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ trait HasMedia

public ?string $mediaType;

public ?string $mime;

public static function getDefaultName(): ?string
{
return 'media';
Expand Down Expand Up @@ -63,6 +65,11 @@ protected function getMediaType(?string $url): ?string
// Parse the URL to remove query parameters
$parsedUrl = parse_url($url, PHP_URL_PATH);

// Handle cases where the URL path ends with a slash (no file)
if (substr($parsedUrl, -1) === '/') {
$parsedUrl = rtrim($parsedUrl, '/');
}

// Get path info from the parsed URL
$pathInfo = pathinfo($parsedUrl);
$extension = strtolower($pathInfo['extension'] ?? '');
Expand All @@ -78,25 +85,31 @@ protected function getMediaType(?string $url): ?string
// Check if the extension matches any media type
foreach ($mediaTypes as $type => $extensions) {
if (in_array($extension, $extensions)) {
$this->mime = "$type/$extension"; // Set the MIME type
return $type;
}
}

//if the $url has no extension, make a get request to the url and check the content type
$headers = get_headers($url, 1);
if (isset($headers['Content-Type'])) {
$contentType = $headers['Content-Type'];
// If the extension is not found, use HTTP headers to detect the content type
$headers = @get_headers($url, 1);
if ($headers && isset($headers['Content-Type'])) {
$contentType = is_array($headers['Content-Type']) ? $headers['Content-Type'][0] : $headers['Content-Type'];
if (strpos($contentType, 'audio') !== false) {
$this->mime = $contentType;
return 'audio';
} elseif (strpos($contentType, 'video') !== false) {
$this->mime = $contentType;
return 'video';
} elseif (strpos($contentType, 'image') !== false) {
$this->mime = $contentType;
return 'image';
} elseif (strpos($contentType, 'pdf') !== false) {
$this->mime = $contentType;
return 'pdf';
}
}

$this->mime = 'unknown';
return 'unknown';
}

Expand All @@ -107,6 +120,7 @@ public function getContentView(): View|Htmlable
return view('filament-media-action::actions.media-modal-content', [
'mediaType' => $this->mediaType,
'media' => $this->getMedia(),
'mime' => $this->mime,
]);
}

Expand Down

0 comments on commit ad075c0

Please sign in to comment.