Add file attachments to comments#108
Open
ItsMalikJones wants to merge 4 commits into
Open
Conversation
Lets users attach files when creating a comment. - New comment_attachments table (migration stub) and CommentAttachment model with a comment() relation, getUrl() and isImage() helpers. - Comment::attachments() relation; commentsQuery() eager-loads attachments. - The Comments component uses WithFileUploads — files are validated (size, count, optional MIME allowlist), stored via a StoreCommentAttachments action on the configured disk, and linked to the comment on save. - Opt-in: a `commentions.attachments` config block (disabled by default) plus CommentsEntry::make()->enableAttachments(), threaded through the entry and actions to the Comments component. - Attachments render below each comment (image previews / download links). - Deleting a comment deletes its attachment rows and files from disk.
- Ship a safe default accepted_mime_types allowlist (was empty/any), preventing browser-executable types (e.g. svg/html) being served from the public disk; add a regression test for rejected types. - Lock the attachmentsEnabled Livewire property so a closure-based gate (e.g. enableAttachments(fn () => $user->isAdmin())) cannot be flipped on by tampering with the request payload. - Log a warning when an upload fails to store instead of silently dropping. - Document the attachments feature and the public-disk MIME risk in the README.
1370d06 to
e7d3aa2
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #32
Summary
Adds optional file attachments to comments. Users can attach one or more files when writing a comment; images render as thumbnails and other file types render as downloadable chips beneath the comment body. The feature is opt-in and disabled by default, so existing installations are unaffected until they explicitly enable it and publish the new migration.
What's included
comment_attachmentstable with a publishable migration (create_commentions_attachments_table), wired into the package's migration tag and the test suite.CommentAttachmentmodel —belongsToa comment, resolves its table viaConfig, exposesgetUrl()andisImage(), and deletes its underlying file from the configured disk when the record is deleted.StoreCommentAttachmentsaction — persists uploaded files to the configured disk/directory and creates the attachment records, following the existingAction::run()convention. Logs a warning instead of silently dropping a file if a write fails.HasAttachmentsFilament concern:enableAttachments()/disableAttachments()(accepts a bool or closure) onCommentsEntry,CommentsAction, andCommentsTableAction, falling back to the global config value.Commentscomponent usingWithFileUploads: pending-file list with per-file removal, upload progress indicator, and validation errors. The attach control is gated on the editor'swasFocusedstate so it appears alongside the existing Comment/Cancel buttons rather than always being visible.attach_filesanduploadingstrings added across all bundled locales (ar, en, es, fr, nl, ro).attachmentsadded to the comment query to avoid N+1s in the list.Configuration
A new
attachmentsconfig block controls the feature:The README documents enabling globally vs. per-component, all options, and the deletion behavior.
Security considerations
accepted_mime_typesships with a safe allowlist (common images + documents) rather than allowing any type, and is validated against each file's actual contents (mimetypesrule). The README includes a warning that emptying the allowlist permits browser-executable types (e.g.image/svg+xml,text/html) to be served from a public disk.attachmentsEnabledLivewire property is marked#[Locked], so a client cannot flip a closure-based gate by tampering with the request payload.Testing
Adds
tests/Livewire/CommentAttachmentTest.phpcovering: attaching a file, the control's visibility (disabled, enabled, and focus-gated), file + record cleanup on comment deletion, oversized-file rejection, disallowed-MIME rejection, pending-file removal, the per-component toggle, and image detection. Run with./vendor/bin/pest.