Skip to content

Commit

Permalink
Merge pull request #266 from plank/fix-case-insensitive
Browse files Browse the repository at this point in the history
Fix media uploader matching aggregateTypes with case mismatch
  • Loading branch information
frasmage committed Oct 28, 2021
2 parents 427c259 + 1706ea0 commit 8bc6efb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/Media.php
Expand Up @@ -4,6 +4,7 @@
namespace Plank\Mediable;

use Carbon\Carbon;
use GuzzleHttp\Psr7\Utils;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
Expand Down Expand Up @@ -397,9 +398,11 @@ public function contents(): string
*/
public function stream()
{
return \GuzzleHttp\Psr7\stream_for(
$this->storage()->readStream($this->getDiskPath())
);
$stream = $this->storage()->readStream($this->getDiskPath());
if (method_exists(Utils::class, 'streamFor')) {
return Utils::streamFor($stream);
}
return \GuzzleHttp\Psr7\stream_for($stream);
}

/**
Expand Down
16 changes: 10 additions & 6 deletions src/MediaUploader.php
Expand Up @@ -354,8 +354,8 @@ public function setAllowUnrecognizedTypes(bool $allow): self
public function setTypeDefinition(string $type, array $mimeTypes, array $extensions): self
{
$this->config['aggregate_types'][$type] = [
'mime_types' => $mimeTypes,
'extensions' => $extensions,
'mime_types' => array_map('strtolower', $mimeTypes),
'extensions' => array_map('strtolower', $extensions),
];

return $this;
Expand Down Expand Up @@ -439,6 +439,8 @@ public function withOptions(array $options): self
*/
public function inferAggregateType(string $mimeType, string $extension): string
{
$mimeType = strtolower($mimeType);
$extension = strtolower($extension);
$allowedTypes = $this->config['allowed_aggregate_types'] ?? [];
$typesForMime = $this->possibleAggregateTypesForMimeType($mimeType);
$typesForExtension = $this->possibleAggregateTypesForExtension($extension);
Expand Down Expand Up @@ -782,9 +784,10 @@ private function verifySource(): void
*/
private function verifyMimeType(string $mimeType): string
{
$mimeType = strtolower($mimeType);
$allowed = $this->config['allowed_mime_types'] ?? [];
if (!empty($allowed) && !in_array(strtolower($mimeType), $allowed)) {
throw FileNotSupportedException::mimeRestricted(strtolower($mimeType), $allowed);
if (!empty($allowed) && !in_array($mimeType, $allowed)) {
throw FileNotSupportedException::mimeRestricted($mimeType, $allowed);
}

return $mimeType;
Expand All @@ -798,9 +801,10 @@ private function verifyMimeType(string $mimeType): string
*/
private function verifyExtension(string $extension): string
{
$extension = strtolower($extension);
$allowed = $this->config['allowed_extensions'] ?? [];
if (!empty($allowed) && !in_array(strtolower($extension), $allowed)) {
throw FileNotSupportedException::extensionRestricted(strtolower($extension), $allowed);
if (!empty($allowed) && !in_array($extension, $allowed)) {
throw FileNotSupportedException::extensionRestricted($extension, $allowed);
}

return $extension;
Expand Down
11 changes: 11 additions & 0 deletions tests/Integration/MediaUploaderTest.php
Expand Up @@ -166,6 +166,17 @@ public function test_it_validates_allowed_types()
$uploader->inferAggregateType('text/foo', 'bar');
}

public function test_it_infers_type_case_insensitive()
{
$uploader = $this->getUploader();
$uploader->setTypeDefinition('foo', ['TeXT/foo'], ['FOo']);

$this->assertEquals(
'foo',
$uploader->inferAggregateType('tExt/fOo', 'foO'),
);
}

public function test_it_can_restrict_to_known_types()
{
$uploader = $this->getUploader();
Expand Down

0 comments on commit 8bc6efb

Please sign in to comment.