Add C2S media upload (uploadMedia endpoint)#3287
Draft
pfefferle wants to merge 17 commits into
Draft
Conversation
Registers two routes: POST uploadMedia (501 placeholder, Task 4) and
GET /media/{attachment_id} which returns the AP-JSON representation
of a WordPress attachment using the existing Base transformer shim.
Register Media_Controller through the existing activitypub_api option gate in rest_init(), and update the test set_up() to exercise the production registration path instead of calling register_routes() directly. Adds a gate test verifying the route is absent when the option is false.
Reuse the same resize-and-WebP optimization the Mastodon importer already applies, so uploaded images don't bypass it. - Promote Attachments::optimize_image() to public (was private). No callers outside the class change behavior; the only addition is one new caller below. - After wp_handle_upload in Media_Controller::upload_item, run the optimization on the saved file. Refresh the cached MIME so the AP response advertises the format that actually lives on disk.
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.
Fixes #3286
Proposed changes:
Adds an OAuth-protected ActivityPub Client-to-Server
uploadMediaendpoint so AP-native clients can push images, audio, and video into the WordPress media library and reference the returned object in a subsequent outboxCreate.POST /activitypub/1.0/actors/{user_id}/uploadMedia(multipart/form-data).GET /activitypub/1.0/media/{attachment_id}returning the AP-JSON representation of an attachment.upload(separate fromwrite, mirroring Mastodon'swrite:media).file+objectJSON-LD shell) and the Pleroma shape (fileonly + optionaldescriptionform field).201 Createdwith aLocationheader pointing at the new attachment's canonical AP id; always the bare media object (never auto-wraps inCreate).uploadscope, owner check, pluscurrent_user_can('upload_files')so subscribers cannot upload.UserandBlogactors viaendpoints.uploadMedia, gated behind the existingactivitypub_apisite option.FEDERATION.mddocuments the feature; changelog entry included.References: W3C SocialCG wiki spec, Pleroma
uploadMedia, SWICG activitypub-api#6, w3c/activitypub#445.Other information:
17 new tests on
Test_Media_Controllercovering route registration, GET image/audio/video and 404/415 paths, POST happy path with DB-state verification, missing-file 400, malformed-JSON 400, Pleromadescriptionfallback,object.nameprecedence, cross-actor 403, missing-cap 403, actor advertisement (User and Blog), and API-disabled gate enforcement. Plus extended coverage onScopeand the newget_attachment_ap_id()helper. Full PHP suite passes locally (2525 tests).Testing instructions:
Enable the experimental ActivityPub API in
Settings → ActivityPub → Advanced.Smoke-test the GET endpoint:
https://<your-site>/wp-json/activitypub/1.0/media/<attachment_id>.Imageobject withtype,mediaType,url, and anidmatching the request URL.Smoke-test the POST endpoint (curl):
Get an OAuth Bearer token via the existing OAuth flow, requesting the
uploadscope.Run:
Expect
201 Created, aLocation: https://.../media/<new_id>header, and a bare APImagebody. The file should appear in the WP media library.Pleroma-style: omit the
objectfield, senddescription=alt textinstead. Same response.Negative cases:
file→400 activitypub_missing_file.object={not json→400 activitypub_invalid_object.application/pdf→415 activitypub_unsupported_media_type.403 activitypub_cannot_upload.403.404;endpoints.uploadMediano longer appears in the actor JSON.Changelog entry
Entry already lives in
.github/changelog/c2s-media-upload(Significance: minor, Type: added). No checkbox needed.