Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image api rework #5260

Merged
merged 42 commits into from
Jan 13, 2025
Merged

Image api rework #5260

merged 42 commits into from
Jan 13, 2025

Conversation

Nutomic
Copy link
Member

@Nutomic Nutomic commented Dec 13, 2024

  • Move all image related endpoints under /api/v4/image/
  • Image parameters and responses are now in crates/api_common/src/image.rs, no more exposing of pictrs data structures
  • Reorganize code in crates/routes/images
  • Only use adapt_request() for image upload, not for simple things like delete or health check
  • New endpoints POST /api/v4/account/avatar which directly takes image upload, instead of setting avatar url via /api/v4/account/settings/save
  • Also POST /api/v4/account/banner, POST /api/v4/community/icon, POST /api/v4/community/banner, POST /api/v4/site/icon and POST /api/v4/site/banner

use lemmy_utils::rate_limit::RateLimitCell;

// Deprecated, use api v4 instead.
// When removing api v3, we also need to rewrite all links in database with
// `/api/v3/image_proxy` to use `/api/v4/image_proxy` instead.
// `/api/v3/image_proxy` to use `/api/v4/image/proxy` instead.
pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation was changed in this file, use "hide whitespace" in github to review it.

@Nutomic Nutomic marked this pull request as ready for review December 18, 2024 10:58
@Nutomic
Copy link
Member Author

Nutomic commented Dec 18, 2024

Ready to review.

Copy link
Member

@dessalines dessalines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job, adding these as API actions is going to be much better.

  • We still need a way to clear images. I'd add some POST or DELETE endpoints, that just delete the images and return that item. IE POST /site/icon/delete, /community/banner/delete...
    • Speaking of which, I see that this PR is the first one that adds HTTP DELETE. Maybe we should use that instead of POST for all our delete API actions.
  • Banners can't be square. So whether that means adding both a max height and max width, or just unfortunately not being able to resize them.
  • We're gonna need a DB migration to fix the history of all proxied images. So unfortunately that means doing a DB replace for comment.content, and post.body, where local = true. I believe the regular image endpoints stayed the same, so no need to correct those.

config/defaults.hjson Show resolved Hide resolved
crates/routes/src/images/upload.rs Show resolved Hide resolved
crates/api/src/local_user/save_settings.rs Show resolved Hide resolved
crates/routes/src/images/utils.rs Outdated Show resolved Hide resolved
crates/utils/src/settings/structs.rs Outdated Show resolved Hide resolved
crates/routes/src/images/upload.rs Show resolved Hide resolved
src/api_routes_v4.rs Outdated Show resolved Hide resolved
@Nothing4You Nothing4You self-requested a review December 19, 2024 22:31
@Nothing4You
Copy link
Collaborator

is there a reason to expose the pict-rs delete token to the user?

i guess this was historically exposed to the user during upload before it got used in the media listing, but there isn't really any need to keep that, now that we have the media listing.
instead, the deletion api should probably just accept the image url itself and fetch the delete token from the user upload table, which can be combined with verifying that either the user is an admin or the user is the uploader.
for older uploads, in the unlikely case that someone recorded deletion tokens prior to lemmy recording which user the upload is associated with, a compatibility api could exist that allows deletion when the token is provided and the upload does not exist in the table.

@Nutomic
Copy link
Member Author

Nutomic commented Jan 10, 2025

Addressed all the review comments. However the api tests are consistently failing and Im not sure why. Seems to be a problem with deleteAllImages() but I cant reproduce it locally.

Copy link
Member

@dessalines dessalines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the deleteImage is working fine, but look at https://github.com/LemmyNet/lemmy/blob/image-api-rework/api_tests/src/image.spec.ts#L61 , and also L92 .

Its weird because everything's passing locally for me too when I run the federation tests, but in a dockerized environment, this fetch might be timing out.

.woodpecker.yml Outdated
@@ -95,15 +95,6 @@ steps:
when:
- event: pull_request

cargo_clippy:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we get the federation tests figured out, these all need to get re-added back in.

@@ -11,7 +11,7 @@ killall -s1 lemmy_server || true
popd

pnpm i
pnpm api-test || true
pnpm api-test-image || true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also will need to get changed back.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

crates/routes/src/images/mod.rs Outdated Show resolved Hide resolved
@Nutomic
Copy link
Member Author

Nutomic commented Jan 13, 2025

Found the problem, purges would delete the image from pictrs but not the row from LocalImage table. So it would try to delete the image a second time in afterAll hook using deleteAllImages. Previously this worked fine, but now there is a stricter success check. Not sure why it was passing locally though.

@dessalines dessalines merged commit a91a03a into main Jan 13, 2025
2 checks passed
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.

4 participants