-
Notifications
You must be signed in to change notification settings - Fork 31
AttachmentDownloadService #379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PhpList\Core\Domain\Messaging\Exception; | ||
|
|
||
| class AttachmentFileNotFoundException extends \RuntimeException | ||
| { | ||
| public function __construct() | ||
| { | ||
| parent::__construct('Attachment file not available'); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PhpList\Core\Domain\Messaging\Model\Dto; | ||
|
|
||
| use Psr\Http\Message\StreamInterface; | ||
|
|
||
| final class DownloadableAttachment | ||
| { | ||
| public function __construct( | ||
| public readonly string $filename, | ||
| public readonly string $mimeType, | ||
| public readonly ?int $size, | ||
| public readonly StreamInterface $content, | ||
| ) { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,64 @@ | ||||||||||
| <?php | ||||||||||
|
|
||||||||||
| declare(strict_types=1); | ||||||||||
|
|
||||||||||
| namespace PhpList\Core\Domain\Messaging\Service; | ||||||||||
|
|
||||||||||
| use GuzzleHttp\Psr7\Utils; | ||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| use PhpList\Core\Domain\Messaging\Exception\AttachmentFileNotFoundException; | ||||||||||
| use PhpList\Core\Domain\Messaging\Model\Attachment; | ||||||||||
| use PhpList\Core\Domain\Messaging\Model\Dto\DownloadableAttachment; | ||||||||||
| use Psr\Http\Message\StreamInterface; | ||||||||||
| use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||||||||||
| use Symfony\Component\Mime\MimeTypes; | ||||||||||
|
|
||||||||||
| class AttachmentDownloadService | ||||||||||
| { | ||||||||||
| public function __construct( | ||||||||||
| #[Autowire('%phplist.attachment_repository_path%')] private readonly string $attachmentRepositoryPath = '/tmp', | ||||||||||
| ) { | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public function getDownloadable(Attachment $attachment): DownloadableAttachment | ||||||||||
| { | ||||||||||
| $original = $attachment->getFilename(); | ||||||||||
| $filename = basename($original); | ||||||||||
|
|
||||||||||
| if ($filename === '' || $filename !== $original) { | ||||||||||
| throw new AttachmentFileNotFoundException(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| $baseDir = realpath($this->attachmentRepositoryPath); | ||||||||||
| if ($baseDir === false) { | ||||||||||
| throw new \RuntimeException('Attachment repository path does not exist.'); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| $filePath = $baseDir . DIRECTORY_SEPARATOR . $filename; | ||||||||||
| $realPath = realpath($filePath); | ||||||||||
|
|
||||||||||
| if ($realPath === false || | ||||||||||
| !str_starts_with($realPath, $baseDir . DIRECTORY_SEPARATOR) || | ||||||||||
| !is_file($realPath) || | ||||||||||
| !is_readable($realPath) | ||||||||||
| ) { | ||||||||||
| throw new AttachmentFileNotFoundException(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| $mimeType = $attachment->getMimeType() | ||||||||||
| ?? MimeTypes::getDefault()->guessMimeType($filePath) | ||||||||||
| ?? 'application/octet-stream'; | ||||||||||
|
|
||||||||||
| $size = filesize($filePath); | ||||||||||
| $size = $size === false ? null : $size; | ||||||||||
|
Comment on lines
+51
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is the root cause of the type mismatch flagged on the DTO. Either coalesce here or fix the DTO type. Quick fix: coalesce to 0- $size = $size === false ? null : $size;
+ $size = $size === false ? 0 : $size;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
|
|
||||||||||
| /** @var StreamInterface $stream */ | ||||||||||
| $stream = Utils::streamFor(Utils::tryFopen($filePath, 'rb')); | ||||||||||
|
|
||||||||||
| return new DownloadableAttachment( | ||||||||||
| filename: $filename, | ||||||||||
| mimeType: $mimeType, | ||||||||||
| size: $size, | ||||||||||
| content: $stream, | ||||||||||
| ); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PhpList\Core\Tests\Unit\Domain\Messaging\Service; | ||
|
|
||
| use PhpList\Core\Domain\Messaging\Exception\AttachmentFileNotFoundException; | ||
| use PhpList\Core\Domain\Messaging\Model\Attachment; | ||
| use PhpList\Core\Domain\Messaging\Service\AttachmentDownloadService; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class AttachmentDownloadServiceTest extends TestCase | ||
| { | ||
| private string $tempDir; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->tempDir = sys_get_temp_dir() . '/phplist-att-dl-' . bin2hex(random_bytes(5)); | ||
| if (!is_dir($this->tempDir)) { | ||
| mkdir($this->tempDir, 0777, true); | ||
| } | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| // cleanup temp directory | ||
| if (is_dir($this->tempDir)) { | ||
| $files = scandir($this->tempDir) ?: []; | ||
| foreach ($files as $f) { | ||
| if ($f === '.' || $f === '..') { | ||
| continue; | ||
| } | ||
| unlink($this->tempDir . '/' . $f); | ||
| } | ||
| rmdir($this->tempDir); | ||
| } | ||
| } | ||
|
|
||
| public function testThrowsWhenFilenameIsEmpty(): void | ||
| { | ||
| $service = new AttachmentDownloadService($this->tempDir); | ||
|
|
||
| $attachment = $this->createMock(Attachment::class); | ||
| $attachment->method('getFilename')->willReturn(''); | ||
|
|
||
| $this->expectException(AttachmentFileNotFoundException::class); | ||
| $service->getDownloadable($attachment); | ||
| } | ||
|
|
||
| public function testThrowsWhenFileDoesNotExist(): void | ||
| { | ||
| $service = new AttachmentDownloadService($this->tempDir); | ||
|
|
||
| $attachment = $this->createMock(Attachment::class); | ||
| $attachment->method('getFilename')->willReturn('missing-file.pdf'); | ||
|
|
||
| $this->expectException(AttachmentFileNotFoundException::class); | ||
| $service->getDownloadable($attachment); | ||
| } | ||
|
|
||
| public function testReturnsDownloadableWithExplicitMimeType(): void | ||
| { | ||
| $service = new AttachmentDownloadService($this->tempDir); | ||
|
|
||
| $filename = 'doc.pdf'; | ||
| $content = '%PDF-1.4\n'; | ||
| file_put_contents($this->tempDir . '/' . $filename, $content); | ||
|
|
||
| $attachment = $this->createMock(Attachment::class); | ||
| $attachment->method('getFilename')->willReturn($filename); | ||
| $attachment->method('getMimeType')->willReturn('application/pdf'); | ||
|
|
||
| $dl = $service->getDownloadable($attachment); | ||
|
|
||
| $this->assertSame($filename, $dl->filename); | ||
| $this->assertSame('application/pdf', $dl->mimeType); | ||
| $this->assertSame(strlen($content), $dl->size); | ||
| $this->assertSame($content, (string)$dl->content); | ||
| } | ||
|
|
||
| public function testGuessesMimeTypeAndProvidesStream(): void | ||
| { | ||
| $service = new AttachmentDownloadService($this->tempDir); | ||
|
|
||
| $filename = 'note.txt'; | ||
| $content = "Hello, world!\n"; | ||
| file_put_contents($this->tempDir . '/' . $filename, $content); | ||
|
|
||
| $attachment = $this->createMock(Attachment::class); | ||
| $attachment->method('getFilename')->willReturn($filename); | ||
| $attachment->method('getMimeType')->willReturn(null); | ||
|
|
||
| $dl = $service->getDownloadable($attachment); | ||
|
|
||
| $this->assertSame($filename, $dl->filename); | ||
| // Symfony MimeTypes should detect text/plain for .txt | ||
| $this->assertSame('text/plain', $dl->mimeType); | ||
| $this->assertSame(strlen($content), $dl->size); | ||
| $this->assertSame($content, (string)$dl->content); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider URL-encoding the
uidquery parameter value.$hashcan be an email address containing characters like+that have special meaning in query strings. Withouturlencode(), a+would be interpreted as a space by the receiving end.Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents