From 4cf16f89444b79e9c457af5d4676b820dd0d90c6 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Fri, 12 Jul 2024 17:47:41 +0200 Subject: [PATCH] Implement actix Responder for Label --- src/api/v1/label.rs | 15 ++------------- src/label/actix.rs | 27 +++++++++++++++++++++++++++ src/label/mod.rs | 1 + 3 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 src/label/actix.rs diff --git a/src/api/v1/label.rs b/src/api/v1/label.rs index f7c6f55..35a4bc6 100644 --- a/src/api/v1/label.rs +++ b/src/api/v1/label.rs @@ -2,8 +2,7 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later -use actix_web::http::header::{ContentDisposition, ContentType, DispositionParam, DispositionType}; -use actix_web::{error, get, web, HttpResponse, Responder}; +use actix_web::{error, get, web, Responder}; use serde::Deserialize; use sqlx::PgPool; use uuid::Uuid; @@ -39,15 +38,5 @@ async fn items( .await .map_err(error::ErrorInternalServerError)?; - let label = Label::for_items(&items, params.preset.clone().into()); - - Ok(HttpResponse::Ok() - .insert_header(ContentType(mime::APPLICATION_PDF)) - .insert_header(ContentDisposition { - disposition: DispositionType::Inline, - parameters: vec![DispositionParam::Filename( - "li7y-item-labels.pdf".to_string(), - )], - }) - .body(label.generate().map_err(error::ErrorInternalServerError)?)) + Ok(Label::for_items(&items, params.preset.clone().into())) } diff --git a/src/label/actix.rs b/src/label/actix.rs new file mode 100644 index 0000000..6d8c482 --- /dev/null +++ b/src/label/actix.rs @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: 2024 Simon Bruder +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use actix_web::http::header::{ContentDisposition, ContentType, DispositionParam, DispositionType}; +use actix_web::{error, HttpResponse, Responder}; + +use super::Label; + +impl Responder for Label { + type Body = actix_web::body::BoxBody; + + fn respond_to(self, _req: &actix_web::HttpRequest) -> actix_web::HttpResponse { + match self.generate() { + Ok(buf) => HttpResponse::Ok() + .insert_header(ContentType(mime::APPLICATION_PDF)) + .insert_header(ContentDisposition { + disposition: DispositionType::Inline, + parameters: vec![DispositionParam::Filename( + "li7y-item-labels.pdf".to_string(), + )], + }) + .body(buf), + Err(e) => error::ErrorInternalServerError(e).error_response(), + } + } +} diff --git a/src/label/mod.rs b/src/label/mod.rs index 97874f1..0aa3677 100644 --- a/src/label/mod.rs +++ b/src/label/mod.rs @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later +mod actix; mod barcode; mod preset;