🚑️ 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::response::Responder;
use std::io::Cursor;
use std::io::Read;
use std::os::unix::fs::FileExt;
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 {
raw
Ok(raw)
} else {
let mut buf = Vec::with_capacity(self.len());
std::fs::File::open(self.file.unwrap())
.unwrap()
.read_to_end(&mut buf)
.unwrap();
return buf;
let file = std::fs::File::open(self.file.unwrap()).unwrap();
let file = tokio::fs::File::from_std(file);
return Err(file);
}
}
}
@ -166,12 +162,23 @@ impl<'r> Responder<'r, 'static> for DataResponse {
|duration| Header::new("Cache-Control", format!("public, max-age={duration}")),
);
Ok(Response::build()
.header(cache_control_header)
.header(Header::new("Accept-Ranges", "bytes"))
.header(Header::new("Content-Type", self.content_type))
.streamed_body(Cursor::new(self.data.full()))
.finalize())
let data_full = self.data.full();
if let Ok(raw) = data_full {
Ok(Response::build()
.header(cache_control_header)
.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())
}
}
}