Skip to content

Commit ec93ff4

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

File tree

2 files changed

+121
-3
lines changed

2 files changed

+121
-3
lines changed

src/Files/DTO/LocalFile.php

Lines changed: 17 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 stored locally on the filesystem.
@@ -31,12 +32,25 @@ class LocalFile implements FileInterface, WithJsonSchemaInterface
3132
* @since n.e.x.t
3233
*
3334
* @param string $path The local filesystem path to the 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 $path, string $mimeType)
37+
public function __construct(string $path, string $mimeType = null)
3738
{
3839
$this->path = $path;
39-
$this->mimeType = $mimeType;
40+
41+
if ($mimeType !== null) {
42+
$this->mimeType = $mimeType;
43+
} else {
44+
// Extract extension from path
45+
$extension = pathinfo($path, PATHINFO_EXTENSION);
46+
47+
if (!empty($extension)) {
48+
$this->mimeType = MimeTypeUtil::getMimeTypeForExtension($extension);
49+
} else {
50+
// No extension found, default to text/plain
51+
$this->mimeType = 'text/plain';
52+
}
53+
}
4054
}
4155

4256
/**

src/Files/Utilities/MimeTypeUtil.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Files\Utilities;
6+
7+
/**
8+
* Utility class for MIME type operations.
9+
*
10+
* Provides static methods for working with MIME types, including
11+
* determining MIME types from file extensions.
12+
*
13+
* @since n.e.x.t
14+
*/
15+
class MimeTypeUtil
16+
{
17+
/**
18+
* Common MIME type mappings for file extensions.
19+
*
20+
* @var array<string, string>
21+
*/
22+
private static array $mimeTypes = [
23+
// Text
24+
'txt' => 'text/plain',
25+
'html' => 'text/html',
26+
'htm' => 'text/html',
27+
'css' => 'text/css',
28+
'js' => 'application/javascript',
29+
'json' => 'application/json',
30+
'xml' => 'application/xml',
31+
'csv' => 'text/csv',
32+
'md' => 'text/markdown',
33+
34+
// Images
35+
'jpg' => 'image/jpeg',
36+
'jpeg' => 'image/jpeg',
37+
'png' => 'image/png',
38+
'gif' => 'image/gif',
39+
'bmp' => 'image/bmp',
40+
'webp' => 'image/webp',
41+
'svg' => 'image/svg+xml',
42+
'ico' => 'image/x-icon',
43+
44+
// Documents
45+
'pdf' => 'application/pdf',
46+
'doc' => 'application/msword',
47+
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
48+
'xls' => 'application/vnd.ms-excel',
49+
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
50+
'ppt' => 'application/vnd.ms-powerpoint',
51+
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
52+
'odt' => 'application/vnd.oasis.opendocument.text',
53+
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
54+
55+
// Archives
56+
'zip' => 'application/zip',
57+
'tar' => 'application/x-tar',
58+
'gz' => 'application/gzip',
59+
'rar' => 'application/x-rar-compressed',
60+
'7z' => 'application/x-7z-compressed',
61+
62+
// Audio
63+
'mp3' => 'audio/mpeg',
64+
'wav' => 'audio/wav',
65+
'ogg' => 'audio/ogg',
66+
'flac' => 'audio/flac',
67+
'm4a' => 'audio/m4a',
68+
69+
// Video
70+
'mp4' => 'video/mp4',
71+
'avi' => 'video/x-msvideo',
72+
'mov' => 'video/quicktime',
73+
'wmv' => 'video/x-ms-wmv',
74+
'flv' => 'video/x-flv',
75+
'webm' => 'video/webm',
76+
'mkv' => 'video/x-matroska',
77+
78+
// Fonts
79+
'ttf' => 'font/ttf',
80+
'otf' => 'font/otf',
81+
'woff' => 'font/woff',
82+
'woff2' => 'font/woff2',
83+
84+
// Other
85+
'php' => 'application/x-httpd-php',
86+
'sh' => 'application/x-sh',
87+
'exe' => 'application/x-msdownload',
88+
];
89+
90+
/**
91+
* Gets the MIME type for a given file extension.
92+
*
93+
* @since n.e.x.t
94+
*
95+
* @param string $extension The file extension (without the dot).
96+
* @return string The MIME type, or 'text/plain' if unknown.
97+
*/
98+
public static function getMimeTypeForExtension(string $extension): string
99+
{
100+
$extension = strtolower($extension);
101+
102+
return self::$mimeTypes[$extension] ?? 'text/plain';
103+
}
104+
}

0 commit comments

Comments
 (0)