Skip to content

Commit f9d19af

Browse files
committed
feat: parses mime type from url extension
1 parent ec93ff4 commit f9d19af

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/Files/DTO/RemoteFile.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use WordPress\AiClient\Common\Contracts\WithJsonSchemaInterface;
88
use WordPress\AiClient\Files\Contracts\FileInterface;
99
use WordPress\AiClient\Files\Traits\HasMimeType;
10+
use WordPress\AiClient\Files\Utilities\MimeTypeUtil;
1011

1112
/**
1213
* Represents a file accessible via a remote URL.
@@ -31,12 +32,36 @@ class RemoteFile implements FileInterface, WithJsonSchemaInterface
3132
* @since n.e.x.t
3233
*
3334
* @param string $url The URL to the remote file.
34-
* @param string $mimeType The MIME type of the file.
35+
* @param string|null $mimeType The MIME type of the file.
3536
*/
36-
public function __construct(string $url, string $mimeType)
37+
public function __construct(string $url, string $mimeType = null)
3738
{
3839
$this->url = $url;
39-
$this->mimeType = $mimeType;
40+
41+
if ($mimeType !== null) {
42+
$this->mimeType = $mimeType;
43+
} else {
44+
// Parse URL to extract filename and extension
45+
$parsedUrl = parse_url($url);
46+
$path = $parsedUrl['path'] ?? '';
47+
48+
// Remove query string and fragment if present in the path
49+
$cleanPath = strtok($path, '?#');
50+
51+
if ($cleanPath === false) {
52+
$cleanPath = $path;
53+
}
54+
55+
// Extract extension from the path
56+
$extension = pathinfo($cleanPath, PATHINFO_EXTENSION);
57+
58+
if (!empty($extension)) {
59+
$this->mimeType = MimeTypeUtil::getMimeTypeForExtension($extension);
60+
} else {
61+
// No extension found, default to text/plain
62+
$this->mimeType = 'text/plain';
63+
}
64+
}
4065
}
4166

4267
/**

0 commit comments

Comments
 (0)