Skip to content

fix: read attachments from the path they were written to - #12

Open
daraghscopey wants to merge 1 commit into
basementdevs:5.xfrom
daraghscopey:fix/attachment-storage-path-config-key
Open

fix: read attachments from the path they were written to#12
daraghscopey wants to merge 1 commit into
basementdevs:5.xfrom
daraghscopey:fix/attachment-storage-path-config-key

Conversation

@daraghscopey

Copy link
Copy Markdown

Fixes #11.

BetterEmailAttachment::getStoragePathAttribute() looked up filament-better-mails.logging.attachments.root, but the shipped config only defines that key under mails.. The lookup returned null, rtrim(null, '/') gave an empty string, and the prefix vanished:

before after
config(...) NULL "mails/attachments"
resulting path /1/report.docx mails/attachments/1/report.docx
writer's path mails/attachments/1/report.docx mails/attachments/1/report.docx

Because config() returns null for a missing key rather than raising, the mismatch was silent. Downloads threw UnableToRetrieveMetadata, and ResendMailAction failed the same way through the file_data accessor. On PHP 8.4 the null also raised a deprecation in rtrim().

Changes

BetterEmailAttachment.php — reads the same key as the writer, with the same 'mails/attachments' default so a published config missing the key falls back rather than resolving to null.

BeforeSendingMailListener.php — drops its inline path construction and uses the accessor. The record is created before the file is written, so $attachment->storage_path is already available. Two independent lookups of one key is what allowed the drift; there is now a single derivation. The disk comes off the stored record for the same reason, so the value written to the row cannot disagree with the disk actually used.

Existing files need no migration — they were always written to the correct path. Only the reader was wrong.

Tests

Five tests in tests/Feature/Core/Models/BetterEmailAttachmentTest.php drive the real listener and read back through the model:

  • round-trips an attachment and asserts it exists at the path the reader computes
  • pins the literal expected path against the configured root
  • reproduces the reported UnableToRetrieveMetadata via the download stream
  • covers a custom root on both write and read
  • covers the fallback when the config key is absent

All five fail on 5.x before the change. Full suite passes after (41 passed, 2 skipped — both pre-existing). Pint clean.

Note that once writer and reader share one derivation, the round-trip tests can no longer detect a wrong key on their own, since both sides move together. The three tests pinning literal path values are what guard the behaviour, so they are not redundant despite the overlap.

The storage path accessor looked up
`filament-better-mails.logging.attachments.root`, but the shipped config
only defines that key under `mails.`. The lookup returned null, so the
prefix vanished and every attachment resolved to `/{id}/{filename}`
instead of `mails/attachments/{id}/{filename}`.

Downloads threw UnableToRetrieveMetadata, and ResendMailAction failed the
same way through the file_data accessor. On PHP 8.4 the null also raised
a deprecation in rtrim().

The reader now uses the same key and default as the writer. The writer in
turn drops its own inline path construction and uses the accessor, since
the record is created before the file is written. Two independent lookups
of one key is what let the two sides drift; now there is a single
derivation. The disk comes off the stored record for the same reason.

Stored files need no migration; they were always written to the right
path.

Fixes basementdevs#11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Attachment downloads 500: storage path is read from logging.attachments.root but written to mails.logging.attachments.root

1 participant