Implement actix Responder for Label

This commit is contained in:
Simon Bruder 2024-07-12 17:47:41 +02:00
parent d46a165a1a
commit 5a247f823f
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
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()))
}

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
mod actix;
mod barcode;
mod preset;