Skip to content

Commit

Permalink
Fix images (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Oct 27, 2022
1 parent 2f9197d commit 6572f6b
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 250 deletions.
99 changes: 2 additions & 97 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ chrono = "0.4.22"
url = "2.3.1"
futures = { version = "0.3.25", default-features = false }
itertools = "0.10.5"
image = { version = "0.24.4", features = ["png", "jpeg"], default-features = false }
rand = "0.8.5"
deser-hjson = "1.0.2"
json-gettext = { version = "4.0.3", default-features = false }
Expand Down
80 changes: 80 additions & 0 deletions src/api/image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::{
api::{handle_response, CLIENT},
env::lemmy_backend,
error::ErrorPage,
site_fairing::SiteData,
utils::base_url,
};
use anyhow::{anyhow, Error};
use lemmy_api_common::sensitive::Sensitive;
use rand::distributions::{Alphanumeric, DistString};
use reqwest::multipart::{Form, Part};
use rocket::fs::TempFile;
use serde::Deserialize;
use std::{collections::HashMap, env::temp_dir, fs::File, io::Read};
use url::Url;

/// Pass image requests to Lemmy backend, which forwards it to pictrs
#[get("/pictrs/image/<file>?<params..>")]
pub async fn image(file: String, params: HashMap<String, String>) -> Result<Vec<u8>, ErrorPage> {
let url = format!("{}/pictrs/image/{}", lemmy_backend(), file);
let bytes: Vec<u8> = CLIENT
.get(url)
.query(&params)
.send()
.await?
.bytes()
.await?
.to_vec();
Ok(bytes)
}

#[derive(Deserialize)]
pub struct UploadImageResponse {
msg: String,
files: Vec<UploadImageFile>,
}

#[derive(Deserialize)]
struct UploadImageFile {
pub file: String,
#[allow(dead_code)]
pub delete_token: String,
}

pub async fn upload_image(
image: &mut TempFile<'_>,
auth: Sensitive<String>,
site_data: &SiteData,
) -> Result<Url, Error> {
// TODO: currently need to persist tempfile to be able to read bytes
// https://github.com/SergioBenitez/Rocket/issues/2148
let filename = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);
let tempfile = format!("{}/{}", temp_dir().as_path().display(), filename);
let file_name = image.raw_name().unwrap().as_str().unwrap().to_string();
let mime_str = image.content_type().unwrap().to_string();
image.persist_to(&tempfile).await?;
let mut file = File::open(&tempfile)?;
let mut data = Vec::new();
file.read_to_end(&mut data)?;

let part = Part::bytes(data).file_name(file_name).mime_str(&mime_str)?;
let form = Form::new().part("images[]", part);
let path = format!("{}/pictrs/image", lemmy_backend());
let res = CLIENT
.post(&path)
.header("cookie", format!("jwt={}", auth.into_inner()))
.multipart(form)
.send()
.await?;
let res: UploadImageResponse = handle_response(res, &path).await?;
if res.msg != "ok" {
return Err(anyhow!(res.msg));
}
let url = Url::parse(&format!(
"{}/pictrs/image/{}",
base_url(site_data),
res.files[0].file
))?;
Ok(url)
}
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod categories;
pub mod comment;
pub mod community;
pub mod extra;
pub mod image;
pub mod post;
pub mod private_message;
pub mod site;
Expand Down
Loading

0 comments on commit 6572f6b

Please sign in to comment.