use serde::Deserialize;
use std::future::Future;
use thiserror::Error;
+use url::Url;
#[derive(Clone, Debug, Error)]
#[error("Error sending request, {0}")]
pub(crate) struct IframelyResponse {
title: Option<String>,
description: Option<String>,
- thumbnail_url: Option<String>,
+ thumbnail_url: Option<Url>,
html: Option<String>,
}
pub(crate) async fn fetch_iframely(
client: &Client,
- url: &str,
+ url: &Url,
) -> Result<IframelyResponse, LemmyError> {
let fetch_url = format!("{}/oembed?url={}", Settings::get().iframely_url(), url);
pub(crate) async fn fetch_pictrs(
client: &Client,
- image_url: &str,
+ image_url: &Url,
) -> Result<PictrsResponse, LemmyError> {
is_image_content_type(client, image_url).await?;
let fetch_url = format!(
"{}/image/download?url={}",
Settings::get().pictrs_url(),
- utf8_percent_encode(image_url, NON_ALPHANUMERIC) // TODO this might not be needed
+ utf8_percent_encode(image_url.as_str(), NON_ALPHANUMERIC) // TODO this might not be needed
);
let response = retry(|| client.get(&fetch_url).send()).await?;
pub async fn fetch_iframely_and_pictrs_data(
client: &Client,
- url: Option<String>,
-) -> (
- Option<String>,
- Option<String>,
- Option<String>,
- Option<String>,
-) {
+ url: Option<&Url>,
+) -> (Option<String>, Option<String>, Option<String>, Option<Url>) {
match &url {
Some(url) => {
// Fetch iframely data
// The full urls are necessary for federation
let pictrs_thumbnail = if let Some(pictrs_hash) = pictrs_hash {
- Some(format!(
+ let url = Url::parse(&format!(
"{}/pictrs/image/{}",
Settings::get().get_protocol_and_hostname(),
pictrs_hash
- ))
+ ));
+ match url {
+ Ok(parsed_url) => Some(parsed_url),
+ Err(e) => {
+ // This really shouldn't happen unless the settings or hash are malformed
+ error!("Unexpected error constructing pictrs thumbnail URL: {}", e);
+ None
+ }
+ }
} else {
None
};
}
}
-async fn is_image_content_type(client: &Client, test: &str) -> Result<(), LemmyError> {
- let response = retry(|| client.get(test).send()).await?;
-
+async fn is_image_content_type(client: &Client, test: &Url) -> Result<(), LemmyError> {
+ let response = retry(|| client.get(test.to_owned()).send()).await?;
if response
.headers()
.get("Content-Type")