Skip to content

Conversation

@obenland
Copy link
Member

Summary

Implements a dual storage strategy where Mastodon imports continue using WordPress attachment posts while ActivityPub inbox items use direct file storage in uploads/activitypub/{post_id}/.

Changes

New methods added:

  • import_files(): Public method for direct file import (used by ap_posts)
  • save_file(): Downloads and saves files to uploads/activitypub/{post_id}/
    • Returns array with url, mime_type, and alt keys
  • import_inline_files(): Processes inline images for file storage
  • append_files_to_content(): Appends file-based media to content
  • generate_files_markup(): Creates block markup from file data
  • get_files_gallery_block(): Gallery block for file-based attachments
  • delete_files_directory(): Public helper to delete post's file directory

Existing methods preserved:

  • import(): Still used for Mastodon imports (creates attachment posts)
  • All attachment-related helper methods remain unchanged

Key improvements

  • Uses WP_Filesystem API exclusively for all file operations
  • Only handles remote URLs (no local file logic needed for inbox items)
  • Reusable delete_files_directory() helper used in multiple places
  • Removed unused function parameters (author_id, path key)
  • Well-documented return types with hash notation for array keys
  • Comprehensive parameter documentation for file data arrays
  • Simple folder-per-post structure for easy cleanup

Benefits

  • ✅ No attachment posts for inbox items (cleaner database)
  • ✅ Clear separation between import types
  • ✅ Consistent use of WordPress filesystem abstractions
  • ✅ Mastodon imports unaffected (backward compatible)

Test plan

  • All 1212 tests passing
  • Tests updated to verify file creation instead of attachment posts
  • Test cleanup properly removes activitypub directories
  • Mastodon import tests still pass (no regression)

Technical details

The implementation uses a simple folder-per-post structure where each ap_post gets its own directory at uploads/activitypub/{post_id}/. When a post is deleted, the entire directory is recursively removed using WP_Filesystem::delete().

Files are tracked by the directory structure itself rather than post meta, keeping the implementation simple and avoiding the need for complex deduplication logic.

Implements a dual storage strategy where Mastodon imports continue using
WordPress attachment posts while ActivityPub inbox items use direct file
storage in uploads/activitypub/{post_id}/.

New methods added:
- import_files(): Public method for direct file import (used by ap_posts)
- save_file(): Downloads and saves files to uploads/activitypub/{post_id}/
  Returns array with 'url', 'mime_type', and 'alt' keys
- import_inline_files(): Processes inline images for file storage
- append_files_to_content(): Appends file-based media to content
- generate_files_markup(): Creates block markup from file data
- get_files_gallery_block(): Gallery block for file-based attachments
- delete_files_directory(): Public helper to delete post's file directory

Existing methods preserved:
- import(): Still used for Mastodon imports (creates attachment posts)
- All attachment-related helper methods remain unchanged

Key improvements:
- Uses WP_Filesystem API exclusively for all file operations
- Only handles remote URLs (no local file logic needed for inbox items)
- Reusable delete_files_directory() helper used in multiple places
- Removed unused function parameters (author_id, path key)
- Well-documented return types with hash notation for array keys
- Comprehensive parameter documentation for file data arrays
- Simple folder-per-post structure for easy cleanup

Benefits:
- No attachment posts for inbox items (cleaner database)
- Clear separation between import types
- Consistent use of WordPress filesystem abstractions
- Mastodon imports unaffected (backward compatible)

Tests updated to verify file creation and cleanup.
All 1212 tests passing.
Refactored the logic for assigning the separator before updating post content by using a ternary operator instead of an if statement. This improves code readability and conciseness.
Refactored Attachments class to rename the delete_files_directory method to delete_directory for clarity and consistency. Updated all references to the method in related files.
Changed the directory for storing ap_post media files from 'uploads/activitypub/{post_id}/' to 'uploads/activitypub/ap_posts/{post_id}/'. Introduced a static property for the directory path and updated all relevant references to improve organization and clarity.
@obenland obenland merged commit 6ee806e into attachment-handling Oct 27, 2025
5 of 13 checks passed
@obenland obenland deleted the attachment-direct-storage branch October 27, 2025 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants