🚑️ files
All checks were successful
ci/woodpecker/push/test Pipeline was successful

This commit is contained in:
JMARyA 2025-03-04 19:19:21 +01:00
parent 0c17944215
commit f3c3892774
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -5,7 +5,6 @@ use rocket::http::Header;
use rocket::http::Status; use rocket::http::Status;
use rocket::response::Responder; use rocket::response::Responder;
use std::io::Cursor; use std::io::Cursor;
use std::io::Read;
use std::os::unix::fs::FileExt; use std::os::unix::fs::FileExt;
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
@ -54,16 +53,13 @@ impl Data {
} }
} }
pub fn full(self) -> Vec<u8> { pub fn full(self) -> Result<Vec<u8>, tokio::fs::File> {
if let Some(raw) = self.raw { if let Some(raw) = self.raw {
raw Ok(raw)
} else { } else {
let mut buf = Vec::with_capacity(self.len()); let file = std::fs::File::open(self.file.unwrap()).unwrap();
std::fs::File::open(self.file.unwrap()) let file = tokio::fs::File::from_std(file);
.unwrap() return Err(file);
.read_to_end(&mut buf)
.unwrap();
return buf;
} }
} }
} }
@ -166,12 +162,23 @@ impl<'r> Responder<'r, 'static> for DataResponse {
|duration| Header::new("Cache-Control", format!("public, max-age={duration}")), |duration| Header::new("Cache-Control", format!("public, max-age={duration}")),
); );
Ok(Response::build() let data_full = self.data.full();
.header(cache_control_header)
.header(Header::new("Accept-Ranges", "bytes")) if let Ok(raw) = data_full {
.header(Header::new("Content-Type", self.content_type)) Ok(Response::build()
.streamed_body(Cursor::new(self.data.full())) .header(cache_control_header)
.finalize()) .header(Header::new("Accept-Ranges", "bytes"))
.header(Header::new("Content-Type", self.content_type))
.streamed_body(Cursor::new(raw))
.finalize())
} else {
Ok(Response::build()
.header(cache_control_header)
.header(Header::new("Accept-Ranges", "bytes"))
.header(Header::new("Content-Type", self.content_type))
.streamed_body(data_full.unwrap_err())
.finalize())
}
} }
} }