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

View file

@ -7,7 +7,7 @@ use axum::{
routing::BoxRoute, routing::BoxRoute,
Router, Router,
}; };
use headers::{ContentType, HeaderMapExt}; use headers::{ContentType, HeaderMapExt, HeaderValue};
use reqwest::{ use reqwest::{
header::{HeaderName, CONTENT_LENGTH}, header::{HeaderName, CONTENT_LENGTH},
StatusCode, StatusCode,
@ -38,6 +38,7 @@ async fn image(
) -> Result<impl IntoResponse, CDNError> { ) -> Result<impl IntoResponse, CDNError> {
let filename = urlencoding::decode(&filename)?.into_owned(); let filename = urlencoding::decode(&filename)?.into_owned();
let cid = sql::get_cid(user, filename.clone(), &service.db_pool).await?; 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 res = service.ipfs_cat(cid).await?;
let clength = res let clength = res
.headers() .headers()
@ -48,6 +49,7 @@ async fn image(
let ctype = ContentType::from(new_mime_guess::from_path(filename).first_or_octet_stream()); let ctype = ContentType::from(new_mime_guess::from_path(filename).first_or_octet_stream());
headers.typed_insert(ctype); headers.typed_insert(ctype);
headers.insert(CONTENT_LENGTH, clength.clone()); headers.insert(CONTENT_LENGTH, clength.clone());
headers.insert("X-Ipfs-Path", HeaderValue::from_str(ipfs_path.as_str())?);
Ok(( Ok((
StatusCode::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>> { 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") let q: Vec<String> =
.bind(user) sqlx::query("SELECT filename FROM memes WHERE userid = $1 ORDER BY filename")
.map(|row: PgRow| row.get("filename")) .bind(user)
.fetch_all(pool) .map(|row: PgRow| row.get("filename"))
.await?; .fetch_all(pool)
.await?;
Ok(q) Ok(q)
} }