-
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
Open
TatevikGr
wants to merge
6
commits into
dev
Choose a base branch
from
feat/attachment
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Domain/Messaging/Exception/AttachmentFileNotFoundException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PhpList\Core\Domain\Messaging\Exception; | ||
|
|
||
| use RuntimeException; | ||
|
|
||
| class AttachmentFileNotFoundException extends RuntimeException | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ) { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/Domain/Messaging/Service/AttachmentDownloadService.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PhpList\Core\Domain\Messaging\Service; | ||
|
|
||
| use GuzzleHttp\Psr7\Utils; | ||
| use PhpList\Core\Domain\Messaging\Exception\AttachmentFileNotFoundException; | ||
| use PhpList\Core\Domain\Messaging\Exception\SubscriberNotFoundException; | ||
| use PhpList\Core\Domain\Messaging\Model\Attachment; | ||
| use PhpList\Core\Domain\Messaging\Model\Dto\DownloadableAttachment; | ||
| use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository; | ||
| use Psr\Http\Message\StreamInterface; | ||
| use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
| use Symfony\Component\Mime\MimeTypes; | ||
|
|
||
| class AttachmentDownloadService | ||
| { | ||
| public function __construct( | ||
| private readonly SubscriberRepository $subscriberRepository, | ||
| #[Autowire('%phplist.attachment_repository_path%')] private readonly string $attachmentRepositoryPath = '/tmp', | ||
| ) { | ||
| } | ||
|
|
||
| public function getDownloadable(Attachment $attachment, string $uid): DownloadableAttachment | ||
| { | ||
| $this->validateUid($uid); | ||
|
|
||
| $original = $attachment->getFilename(); | ||
| if ($original === null || $original === '') { | ||
| throw new AttachmentFileNotFoundException('Attachment has no filename.'); | ||
| } | ||
| $filename = basename($original); | ||
| $filePath = $this->validateFilePath($filename, $original); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| $mimeType = $attachment->getMimeType() | ||
| ?? MimeTypes::getDefault()->guessMimeType($filePath) | ||
| ?? 'application/octet-stream'; | ||
|
|
||
| $size = filesize($filePath); | ||
| $size = $size === false ? null : $size; | ||
|
|
||
| /** @var StreamInterface $stream */ | ||
| $stream = Utils::streamFor(Utils::tryFopen($filePath, 'rb')); | ||
|
|
||
| return new DownloadableAttachment( | ||
| filename: $filename, | ||
| mimeType: $mimeType, | ||
| size: $size, | ||
| content: $stream, | ||
| ); | ||
| } | ||
|
|
||
| private function validateUid(string $uid): void | ||
| { | ||
| if ($uid === Attachment::FORWARD) { | ||
| return; | ||
| } | ||
|
|
||
| $subscriber = $this->subscriberRepository->findOneByEmail($uid); | ||
| if ($subscriber === null) { | ||
| throw new SubscriberNotFoundException(); | ||
| } | ||
| } | ||
TatevikGr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private function validateFilePath(string $filename, ?string $original): string | ||
| { | ||
| if ($filename === '' || $filename !== $original) { | ||
| throw new AttachmentFileNotFoundException('Invalid attachment filename: ' . $original); | ||
| } | ||
|
|
||
| $baseDir = realpath($this->attachmentRepositoryPath); | ||
| if ($baseDir === false) { | ||
| throw new AttachmentFileNotFoundException('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('Attachment file not available'); | ||
| } | ||
|
|
||
| return $filePath; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
tests/Unit/Domain/Messaging/Service/AttachmentDownloadServiceTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| <?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 PhpList\Core\Domain\Subscription\Model\Subscriber; | ||
| use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository; | ||
| 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 | ||
| { | ||
| $subscriberRepo = $this->createMock(SubscriberRepository::class); | ||
| $service = new AttachmentDownloadService($subscriberRepo, $this->tempDir); | ||
|
|
||
| $attachment = $this->createMock(Attachment::class); | ||
| $attachment->method('getFilename')->willReturn(''); | ||
|
|
||
| $this->expectException(AttachmentFileNotFoundException::class); | ||
| $service->getDownloadable($attachment, 'forwarded'); | ||
| } | ||
|
|
||
| public function testThrowsWhenFileDoesNotExist(): void | ||
| { | ||
| $subscriberRepo = $this->createMock(SubscriberRepository::class); | ||
| $service = new AttachmentDownloadService($subscriberRepo, $this->tempDir); | ||
|
|
||
| $attachment = $this->createMock(Attachment::class); | ||
| $attachment->method('getFilename')->willReturn('missing-file.pdf'); | ||
|
|
||
| $this->expectException(AttachmentFileNotFoundException::class); | ||
| $service->getDownloadable($attachment, 'forwarded'); | ||
| } | ||
|
|
||
| public function testReturnsDownloadableWithExplicitMimeType(): void | ||
| { | ||
| $subscriberRepo = $this->createMock(SubscriberRepository::class); | ||
| $subscriberRepo->method('findOneByEmail')->with('user@example.com')->willReturn(new Subscriber()); | ||
| $service = new AttachmentDownloadService($subscriberRepo, $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, 'user@example.com'); | ||
|
|
||
| $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 | ||
| { | ||
| $subscriberRepo = $this->createMock(SubscriberRepository::class); | ||
| $subscriberRepo->method('findOneByEmail')->with('user@example.com')->willReturn(new Subscriber()); | ||
| $service = new AttachmentDownloadService($subscriberRepo, $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, 'user@example.com'); | ||
|
|
||
| $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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.