7
7
use WordPress \AiClient \Common \Contracts \WithJsonSchemaInterface ;
8
8
use WordPress \AiClient \Files \Contracts \FileInterface ;
9
9
use WordPress \AiClient \Files \Traits \HasMimeType ;
10
+ use WordPress \AiClient \Files \Utilities \MimeTypeUtil ;
10
11
11
12
/**
12
13
* Represents a file accessible via a remote URL.
@@ -31,12 +32,36 @@ class RemoteFile implements FileInterface, WithJsonSchemaInterface
31
32
* @since n.e.x.t
32
33
*
33
34
* @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.
35
36
*/
36
- public function __construct (string $ url , string $ mimeType )
37
+ public function __construct (string $ url , string $ mimeType = null )
37
38
{
38
39
$ 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
+ }
40
65
}
41
66
42
67
/**
0 commit comments