Implement actix Responder for Label

This commit is contained in:
Simon Bruder 2024-07-12 17:47:41 +02:00
parent ce171225be
commit 4cf16f8944
Signed by: simon
GPG key ID: 347FF8699CDA0776
3 changed files with 30 additions and 13 deletions

View file

@ -2,8 +2,7 @@
// //
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
use actix_web::http::header::{ContentDisposition, ContentType, DispositionParam, DispositionType}; use actix_web::{error, get, web, Responder};
use actix_web::{error, get, web, HttpResponse, Responder};
use serde::Deserialize; use serde::Deserialize;
use sqlx::PgPool; use sqlx::PgPool;
use uuid::Uuid; use uuid::Uuid;
@ -39,15 +38,5 @@ async fn items(
.await .await
.map_err(error::ErrorInternalServerError)?; .map_err(error::ErrorInternalServerError)?;
let label = Label::for_items(&items, params.preset.clone().into()); Ok(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)?))
} }

27
src/label/actix.rs Normal file
View file

@ -0,0 +1,27 @@
// SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
//
// 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<Self::Body> {
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(),
}
}
}

View file

@ -2,6 +2,7 @@
// //
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
mod actix;
mod barcode; mod barcode;
mod preset; mod preset;