Add view original functionality

This commit is contained in:
2025-04-19 12:33:11 -07:00
parent 122e949072
commit 7d9376d607
3 changed files with 57 additions and 15 deletions

View File

@@ -29,7 +29,7 @@ use serde::Deserialize;
use sqlx::postgres::PgPool;
use tokio::{net::TcpListener, sync::Mutex};
use tower_http::trace::{DefaultMakeSpan, TraceLayer};
use tracing::info;
use tracing::{info, warn};
// Make our own error that wraps `anyhow::Error`.
struct AppError(letterbox_server::ServerError);
@@ -142,6 +142,25 @@ async fn view_cid(
Ok(inline_attachment_response(attachment))
}
async fn view_original(
State(AppState { nm, .. }): State<AppState>,
extract::Path(id): extract::Path<String>,
) -> Result<impl IntoResponse, AppError> {
info!("view_original {id}");
let mid = if id.starts_with("id:") {
id.to_string()
} else {
format!("id:{}", id)
};
let files = nm.files(&mid)?;
let Some(path) = files.first() else {
warn!("failed to find files for message {mid}");
return Ok((StatusCode::NOT_FOUND, mid).into_response());
};
let str = std::fs::read_to_string(&path)?;
Ok(str.into_response())
}
async fn graphiql() -> impl IntoResponse {
response::Html(
GraphiQLSource::build()
@@ -260,6 +279,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
get(download_attachment),
)
.route("/view/attachment/{id}/{idx}/{*rest}", get(view_attachment))
.route("/original/{id}", get(view_original))
.route("/cid/{id}/{cid}", get(view_cid))
.route("/ws", any(start_ws))
.route_service("/graphql/ws", GraphQLSubscription::new(schema.clone()))