feat: return IPFS header for CDN
All checks were successful
ci/woodpecker/push/central-override Pipeline was successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Timo Ley 2023-07-26 17:36:04 +02:00
parent a031352ef9
commit b42e35da45
3 changed files with 12 additions and 6 deletions

View file

@ -4,6 +4,7 @@ use axum::{
body::{Bytes, Empty},
response::IntoResponse,
};
use hyper::header::InvalidHeaderValue;
use reqwest::StatusCode;
use thiserror::Error;
@ -17,6 +18,8 @@ pub enum CDNError {
Service(#[from] ServiceError),
#[error("Decode error: {0}")]
Decode(#[from] FromUtf8Error),
#[error("Header error: {0}")]
Header(#[from] InvalidHeaderValue),
#[error("Internal server error")]
Internal,
}

View file

@ -7,7 +7,7 @@ use axum::{
routing::BoxRoute,
Router,
};
use headers::{ContentType, HeaderMapExt};
use headers::{ContentType, HeaderMapExt, HeaderValue};
use reqwest::{
header::{HeaderName, CONTENT_LENGTH},
StatusCode,
@ -38,6 +38,7 @@ async fn image(
) -> Result<impl IntoResponse, CDNError> {
let filename = urlencoding::decode(&filename)?.into_owned();
let cid = sql::get_cid(user, filename.clone(), &service.db_pool).await?;
let ipfs_path = format!("/ipfs/{}", cid);
let res = service.ipfs_cat(cid).await?;
let clength = res
.headers()
@ -48,6 +49,7 @@ async fn image(
let ctype = ContentType::from(new_mime_guess::from_path(filename).first_or_octet_stream());
headers.typed_insert(ctype);
headers.insert(CONTENT_LENGTH, clength.clone());
headers.insert("X-Ipfs-Path", HeaderValue::from_str(ipfs_path.as_str())?);
Ok((
StatusCode::OK,

View file

@ -12,11 +12,12 @@ pub async fn get_cid(user: String, filename: String, pool: &PgPool) -> Result<St
}
pub async fn get_memes(user: String, pool: &PgPool) -> Result<Vec<String>> {
let q: Vec<String> = sqlx::query("SELECT filename FROM memes WHERE userid = $1 ORDER BY filename")
.bind(user)
.map(|row: PgRow| row.get("filename"))
.fetch_all(pool)
.await?;
let q: Vec<String> =
sqlx::query("SELECT filename FROM memes WHERE userid = $1 ORDER BY filename")
.bind(user)
.map(|row: PgRow| row.get("filename"))
.fetch_all(pool)
.await?;
Ok(q)
}