Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 15 additions & 1 deletion src/PhpImap/IncomingMailAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,24 @@ public function addDataPartInfo(DataPartInfo $dataInfo): void
* @psalm-param fileinfoconst $fileinfo_const
*/
public function getFileInfo(int $fileinfo_const = FILEINFO_NONE): string
{
$fileInfo = $this->detectFileInfo($fileinfo_const, $this->getContents());

if (false === $fileInfo) {
return '';
}

return $fileInfo;
}

/**
* @return false|string
*/
protected function detectFileInfo(int $fileinfo_const, string $contents)
{
$finfo = new finfo($fileinfo_const);

return $finfo->buffer($this->getContents());
return $finfo->buffer($contents);
}

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/unit/IncomingMailAttachmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace PhpImap;

use const FILEINFO_MIME_TYPE;

use PHPUnit\Framework\TestCase;

final class IncomingMailAttachmentTest extends TestCase
{
public function testGetFileInfoReturnsEmptyStringWhenFinfoBufferFails(): void
{
$attachment = new class() extends IncomingMailAttachment {
public function getContents(): string
{
return 'broken-image-contents';
}

protected function detectFileInfo(int $fileinfo_const, string $contents)
{
return false;
}
};

$this->assertSame('', $attachment->getFileInfo(FILEINFO_MIME_TYPE));
}

public function testGetFileInfoReturnsDetectedStringWhenFinfoBufferSucceeds(): void
{
$attachment = new class() extends IncomingMailAttachment {
public function getContents(): string
{
return 'png-contents';
}

protected function detectFileInfo(int $fileinfo_const, string $contents)
{
return 'image/png';
}
};

$this->assertSame('image/png', $attachment->getFileInfo(FILEINFO_MIME_TYPE));
}
}
Loading