Repository navigation
feat: custom user avatar upload and delete - #13949
Conversation
🟢 Tier S · Ready to merge
The PR adds custom avatar upload ( Latest changes: Update and Delete now stop retrying after 5 optimistic-concurrency conflicts and throw DOCUMENT_UPDATE_CONFLICT. Update also removes the newly uploaded file before giving up.
Note @imtia33 does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve. 📂 Walkthrough · 16
Reviewed the commits since |
|
- Store the photo ID in users.avatar instead of the full path - Rename avatars collection to photos and drop its path attribute - Rename Avatar response model to Photo - Lock uploads per upload ID instead of per user; drop the lock from delete - Generate the chunked test payload instead of shipping a 17 MB fixture Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
- Lock per user around chunks, activation and deletion via the lock pool - Validate Content-Range shape and actual chunk size against the range - Accept only PNG, JPEG, GIF and WebP by content - Delete storage before records so failed deletes stay retriable - Retry activation when a completed upload was never made active - Audit photo deletion against the session user - Store photos under _photos/ so bucket deletion can't remove them - Test the served image instead of upload metadata Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Replace the users.avatar pointer with a users.photos subquery attribute that returns the newest completed photo, cached with the user document. Photo rows are the only state; Update and Delete purge the user cache after changing them. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Rename users.photos to users.photo and return the live photo with findOne instead of a one-item list. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Drop chunked uploads: a photo is one request of up to one upload chunk (5MB), validated by content before it is stored. Every photos row is now a finished photo, so the chunk counters, upload metadata and the per-user lock go away; each upload removes only older photos, which lets concurrent uploads settle on the newest. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Replace the photos collection and its subquery with photoId and photoSize attributes on users, drop the stored mime type, and accept PNG and JPEG only. Files live in a per-user folder that user deletion removes whole, which also clears anything a racing request left behind. Update now returns the user. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Move photo file deletion out of the provider and into the endpoints and the Deletes worker. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Replace the provider's getPath() with APP_STORAGE_PHOTOS and build the path where it is needed. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
- Serialise photo updates and deletes per user, so the replaced photo is always the one whose file is removed - Return MODEL_ACCOUNT instead of MODEL_USER, which exposed the password hash - Accept WebP alongside PNG and JPEG - Check that JPEG and WebP uploads are served Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Only remove the uploaded file when the user was not switched to it yet; a failure after the switch is logged and the request succeeds. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The lock added more code than the race it prevents is worth. A file left behind by concurrent requests is removed with the user's photo folder when the user is deleted. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Swoole drops a multipart request without a boundary before it reaches the app, so the test got a bare 400 instead of the missing-file error. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Photo updates and deletes are guarded by the user's update timestamp, so a concurrent upload is never cleared by a delete and a replaced photo is never left behind. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
What does this PR do?
Adds custom user avatar upload and delete to the Avatars service.
New endpoints:
PUT /v1/avatars/photo— Upload a custom profile photo (PNG, JPG, GIF, WebP). Supports chunked uploads for files over 5 MB viaContent-Range. Concurrent uploads for the same user are serialised with a per-user distributed lock. The new photo is only activated after the file is fully assembled — no downtime between replacements.DELETE /v1/avatars/photo— Remove the custom photo and revert to the existing fallback chain (OAuth2 identity photo → Gravatar → Libravatar → Initials → static placeholder). Idempotent.GET /v1/avatars/photoupdated: Custom uploaded photo is now tried first before any existing provider.Storage: A new
avatarscollection tracks uploads per-project. Storage is metered via aStatsResourcesgauge (avatars.storage) — the same pattern asfiles.storage. Avatar files are cleaned up when a user is deleted.Migration:
avatarscollection andusers.avatarPathattribute are created inside the existing V25 migration switch (same pattern aspushLedger) so existing projects are covered without a separate migration version.Test Plan
E2E tests added to
AvatarsCustomClientTestandAvatarsCustomServerTest:chunksTotal,chunksUploaded,mimeType,sizeActualGET /v1/avatars/photoafter upload — custom photo wins the provider chainDELETE+ verify fallback chain resumes + idempotent second deleteContent-Range(400)PUT/DELETE— assert 401Run with:
All 39 tests pass, 773 assertions.
Related PRs and Issues
Checklist
avatars.updatePhoto,avatars.deletePhoto) are generated automatically from theMethodlabels on the new actions. No manual spec changes needed —app/config/sdks.phpis untouched.