Skip to content

Commit

Permalink
Remove PictrsImageMode::StoreLinkPreviews
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Nov 20, 2024
1 parent 99f23f7 commit ada012e
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 70 deletions.
2 changes: 1 addition & 1 deletion api_tests/run-federation-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ killall -s1 lemmy_server || true
popd

pnpm i
pnpm api-test || true
pnpm api-test-image || true

killall -s1 lemmy_server || true
killall -s1 pict-rs || true
Expand Down
9 changes: 0 additions & 9 deletions config/defaults.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@

# or

# Generate thumbnails for external post urls and store them persistently in pict-rs. This
# ensures that they can be reliably retrieved and can be resized using pict-rs APIs. However
# it also increases storage usage.
#
# This behaviour matches Lemmy 0.18.
"StoreLinkPreviews"

# or

# If enabled, all images from remote domains are rewritten to pass through
# `/api/v3/image_proxy`, including embedded images in markdown. Images are stored temporarily
# in pict-rs for caching. This improves privacy as users don't expose their IP to untrusted
Expand Down
50 changes: 4 additions & 46 deletions crates/api_common/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use futures::StreamExt;
use lemmy_db_schema::{
newtypes::DbUrl,
source::{
images::{ImageDetailsForm, LocalImage, LocalImageForm},
images::{ImageDetailsForm, LocalImage},
post::{Post, PostUpdateForm},
site::Site,
},
Expand Down Expand Up @@ -346,53 +346,11 @@ async fn generate_pictrs_thumbnail(image_url: &Url, context: &LemmyContext) -> L
let pictrs_config = context.settings().pictrs_config()?;

match pictrs_config.image_mode {
PictrsImageMode::None => return Ok(image_url.clone()),
PictrsImageMode::None => Ok(image_url.clone()),
PictrsImageMode::ProxyAllImages => {
return Ok(proxy_image_link(image_url.clone(), context).await?.into())
Ok(proxy_image_link(image_url.clone(), context).await?.into())
}
_ => {}
};

// fetch remote non-pictrs images for persistent thumbnail link
// TODO: should limit size once supported by pictrs
let fetch_url = format!(
"{}image/download?url={}&resize={}",
pictrs_config.url,
encode(image_url.as_str()),
context.settings().pictrs_config()?.max_thumbnail_size
);

let res = context
.client()
.get(&fetch_url)
.timeout(REQWEST_TIMEOUT)
.send()
.await?
.error_for_status()?
.json::<PictrsResponse>()
.await?;

let files = res.files.unwrap_or_default();

let image = files
.first()
.ok_or(LemmyErrorType::PictrsResponseError(res.msg))?;

let form = LocalImageForm {
// This is none because its an internal request.
// IE, a local user shouldn't get to delete the thumbnails for their link posts
local_user_id: None,
pictrs_alias: image.file.clone(),
pictrs_delete_token: image.delete_token.clone(),
};
let protocol_and_hostname = context.settings().get_protocol_and_hostname();
let thumbnail_url = image.thumbnail_url(&protocol_and_hostname)?;

// Also store the details for the image
let details_form = image.details.build_image_details_form(&thumbnail_url);
LocalImage::create(&mut context.pool(), &form, &details_form).await?;

Ok(thumbnail_url)
}
}

/// Fetches the image details for pictrs proxied images
Expand Down
6 changes: 2 additions & 4 deletions crates/utils/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ pub enum LemmyErrorType {
CouldntUpdateComment,
CouldntUpdatePrivateMessage,
CannotLeaveAdmin,
// TODO: also remove the translations of unused errors
PictrsResponseError(String),
PictrsPurgeResponseError(String),
ImageUrlMissingPathSegments,
ImageUrlMissingLastPathSegment,
Expand Down Expand Up @@ -335,12 +333,12 @@ cfg_if! {

#[test]
fn deserializes_with_message() -> LemmyResult<()> {
let reg_banned = LemmyErrorType::PictrsResponseError(String::from("reason"));
let reg_banned = LemmyErrorType::Unknown(String::from("reason"));
let err = LemmyError::from(reg_banned).error_response();
let json = String::from_utf8(err.into_body().try_into_bytes().unwrap_or_default().to_vec())?;
assert_eq!(
&json,
"{\"error\":\"pictrs_response_error\",\"message\":\"reason\"}"
"{\"error\":\"unknown\",\"message\":\"reason\"}"
);

Ok(())
Expand Down
6 changes: 0 additions & 6 deletions crates/utils/src/settings/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,6 @@ pub enum PictrsImageMode {
/// Leave images unchanged, don't generate any local thumbnails for post urls. Instead the
/// Opengraph image is directly returned as thumbnail
None,
/// Generate thumbnails for external post urls and store them persistently in pict-rs. This
/// ensures that they can be reliably retrieved and can be resized using pict-rs APIs. However
/// it also increases storage usage.
///
/// This behaviour matches Lemmy 0.18.
StoreLinkPreviews,
/// If enabled, all images from remote domains are rewritten to pass through
/// `/api/v3/image_proxy`, including embedded images in markdown. Images are stored temporarily
/// in pict-rs for caching. This improves privacy as users don't expose their IP to untrusted
Expand Down
1 change: 0 additions & 1 deletion docker/federation/lemmy_alpha.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
}
pictrs: {
api_key: "my-pictrs-key"
image_mode: StoreLinkPreviews
}
}
1 change: 0 additions & 1 deletion docker/federation/lemmy_beta.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
}
pictrs: {
api_key: "my-pictrs-key"
image_mode: StoreLinkPreviews
}
}
1 change: 0 additions & 1 deletion docker/federation/lemmy_epsilon.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
}
pictrs: {
api_key: "my-pictrs-key"
image_mode: ProxyAllImages
}
}
1 change: 0 additions & 1 deletion docker/federation/lemmy_gamma.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
}
pictrs: {
api_key: "my-pictrs-key"
image_mode: ProxyAllImages
}
}

0 comments on commit ada012e

Please sign in to comment.