Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/Files/DTO/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ private function detectAndProcessFile(string $file, ?string $providedMimeType):
return;
}

// Check if it's a data URI
// Data URI pattern.
$dataUriPattern = '/^data:(?:([a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*\/[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*'
. '(?:;[a-zA-Z0-9\-]+=[a-zA-Z0-9\-]+)*)?;)?base64,([A-Za-z0-9+\/]*={0,2})$/';
. '(?:;[a-zA-Z0-9\-]+=[a-zA-Z0-9\-]+)*)?;)?base64,([A-Za-z0-9+\/]*={0,2})$/';

// Check if it's a data URI.
if (preg_match($dataUriPattern, $file, $matches)) {
$this->fileType = FileTypeEnum::inline();
$this->base64Data = $matches[2]; // Extract just the base64 data
Expand Down Expand Up @@ -285,6 +286,20 @@ public function isText(): bool
return $this->mimeType->isText();
}

/**
* Checks if the file is a specific MIME type.
*
* @since n.e.x.t
*
* @param string $type The type to check.
*
* @return bool True if the file is of the specified type.
*/
public function isType(string $type): bool
{
return $this->mimeType->isType($type);
}

/**
* Determines the MIME type from various sources.
*
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/Files/DTO/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ public function testMimeTypeMethods(): void
$this->assertFalse($file->isImage());
$this->assertFalse($file->isAudio());
$this->assertFalse($file->isText());
$this->assertTrue($file->isType('video'));
$this->assertFalse($file->isType('image'));
$this->assertFalse($file->isType('audio'));
$this->assertFalse($file->isType('text'));
}

/**
Expand Down