Skip to content

Commit 8bc6efb

Browse files
authored
Merge pull request #266 from plank/fix-case-insensitive
Fix media uploader matching aggregateTypes with case mismatch
2 parents 427c259 + 1706ea0 commit 8bc6efb

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/Media.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Plank\Mediable;
55

66
use Carbon\Carbon;
7+
use GuzzleHttp\Psr7\Utils;
78
use Illuminate\Contracts\Filesystem\Filesystem;
89
use Illuminate\Database\Eloquent\Builder;
910
use Illuminate\Database\Eloquent\Collection;
@@ -397,9 +398,11 @@ public function contents(): string
397398
*/
398399
public function stream()
399400
{
400-
return \GuzzleHttp\Psr7\stream_for(
401-
$this->storage()->readStream($this->getDiskPath())
402-
);
401+
$stream = $this->storage()->readStream($this->getDiskPath());
402+
if (method_exists(Utils::class, 'streamFor')) {
403+
return Utils::streamFor($stream);
404+
}
405+
return \GuzzleHttp\Psr7\stream_for($stream);
403406
}
404407

405408
/**

src/MediaUploader.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ public function setAllowUnrecognizedTypes(bool $allow): self
354354
public function setTypeDefinition(string $type, array $mimeTypes, array $extensions): self
355355
{
356356
$this->config['aggregate_types'][$type] = [
357-
'mime_types' => $mimeTypes,
358-
'extensions' => $extensions,
357+
'mime_types' => array_map('strtolower', $mimeTypes),
358+
'extensions' => array_map('strtolower', $extensions),
359359
];
360360

361361
return $this;
@@ -439,6 +439,8 @@ public function withOptions(array $options): self
439439
*/
440440
public function inferAggregateType(string $mimeType, string $extension): string
441441
{
442+
$mimeType = strtolower($mimeType);
443+
$extension = strtolower($extension);
442444
$allowedTypes = $this->config['allowed_aggregate_types'] ?? [];
443445
$typesForMime = $this->possibleAggregateTypesForMimeType($mimeType);
444446
$typesForExtension = $this->possibleAggregateTypesForExtension($extension);
@@ -782,9 +784,10 @@ private function verifySource(): void
782784
*/
783785
private function verifyMimeType(string $mimeType): string
784786
{
787+
$mimeType = strtolower($mimeType);
785788
$allowed = $this->config['allowed_mime_types'] ?? [];
786-
if (!empty($allowed) && !in_array(strtolower($mimeType), $allowed)) {
787-
throw FileNotSupportedException::mimeRestricted(strtolower($mimeType), $allowed);
789+
if (!empty($allowed) && !in_array($mimeType, $allowed)) {
790+
throw FileNotSupportedException::mimeRestricted($mimeType, $allowed);
788791
}
789792

790793
return $mimeType;
@@ -798,9 +801,10 @@ private function verifyMimeType(string $mimeType): string
798801
*/
799802
private function verifyExtension(string $extension): string
800803
{
804+
$extension = strtolower($extension);
801805
$allowed = $this->config['allowed_extensions'] ?? [];
802-
if (!empty($allowed) && !in_array(strtolower($extension), $allowed)) {
803-
throw FileNotSupportedException::extensionRestricted(strtolower($extension), $allowed);
806+
if (!empty($allowed) && !in_array($extension, $allowed)) {
807+
throw FileNotSupportedException::extensionRestricted($extension, $allowed);
804808
}
805809

806810
return $extension;

tests/Integration/MediaUploaderTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ public function test_it_validates_allowed_types()
166166
$uploader->inferAggregateType('text/foo', 'bar');
167167
}
168168

169+
public function test_it_infers_type_case_insensitive()
170+
{
171+
$uploader = $this->getUploader();
172+
$uploader->setTypeDefinition('foo', ['TeXT/foo'], ['FOo']);
173+
174+
$this->assertEquals(
175+
'foo',
176+
$uploader->inferAggregateType('tExt/fOo', 'foO'),
177+
);
178+
}
179+
169180
public function test_it_can_restrict_to_known_types()
170181
{
171182
$uploader = $this->getUploader();

0 commit comments

Comments
 (0)