+ if response.msg == "ok" {
+ Ok(response)
+ } else {
+ Err(LemmyErrorType::PictrsResponseError(response.msg))?
+ }
+}
+
+/// Purges an image from pictrs
+/// Note: This should often be coerced from a Result to .ok() in order to fail softly, because:
+/// - It might fail due to image being not local
+/// - It might not be an image
+/// - Pictrs might not be set up
+pub async fn purge_image_from_pictrs(
+ client: &ClientWithMiddleware,
+ settings: &Settings,
+ image_url: &Url,
+) -> Result<(), LemmyError> {
+ let pictrs_config = settings.pictrs_config()?;
+ is_image_content_type(client, image_url).await?;
+
+ let alias = image_url
+ .path_segments()
+ .ok_or(LemmyErrorType::ImageUrlMissingPathSegments)?
+ .next_back()
+ .ok_or(LemmyErrorType::ImageUrlMissingLastPathSegment)?;
+
+ let purge_url = format!("{}/internal/purge?alias={}", pictrs_config.url, alias);
+
+ let pictrs_api_key = pictrs_config
+ .api_key
+ .ok_or(LemmyErrorType::PictrsApiKeyNotProvided)?;
+ let response = client
+ .post(&purge_url)
+ .timeout(REQWEST_TIMEOUT)
+ .header("x-api-token", pictrs_api_key)
+ .send()
+ .await?;
+
+ let response: PictrsPurgeResponse = response.json().await.map_err(LemmyError::from)?;
+
+ if response.msg == "ok" {
+ Ok(())