2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-10 06:25:00 +00:00

Add back range header support for thumbnail on generation

This commit is contained in:
Aode (Lion) 2021-09-09 16:35:07 -05:00
parent 6436f48712
commit c1b2c7706a
2 changed files with 50 additions and 8 deletions

View file

@ -541,13 +541,40 @@ async fn process(
let (details, bytes) =
CancelSafeProcessor::new(thumbnail_path.clone(), Box::pin(process_fut)).await?;
return Ok(srv_response(
HttpResponse::Ok(),
once(ready(Ok(bytes) as Result<_, UploadError>)),
details.content_type(),
7 * DAYS,
details.system_time(),
));
return match range {
Some(range_header) => {
if !range_header.is_bytes() {
return Err(UploadError::Range);
}
if range_header.is_empty() {
Err(UploadError::Range)
} else if range_header.len() == 1 {
let range = range_header.ranges().next().unwrap();
let content_range = range.to_content_range(bytes.len() as u64);
let stream = range.chop_bytes(bytes);
let mut builder = HttpResponse::PartialContent();
builder.insert_header(content_range);
Ok(srv_response(
builder,
stream,
details.content_type(),
7 * DAYS,
details.system_time(),
))
} else {
Err(UploadError::Range)
}
}
None => Ok(srv_response(
HttpResponse::Ok(),
once(ready(Ok(bytes) as Result<_, UploadError>)),
details.content_type(),
7 * DAYS,
details.system_time(),
)),
};
}
let details = if let Some(details) = details {

View file

@ -8,7 +8,7 @@ use actix_web::{
web::Bytes,
FromRequest, HttpRequest,
};
use futures_util::stream::LocalBoxStream;
use futures_util::stream::{once, LocalBoxStream, Stream};
use std::{future::ready, io};
use tokio::io::{AsyncReadExt, AsyncSeekExt};
@ -43,6 +43,21 @@ impl Range {
}
}
pub(crate) fn chop_bytes(
&self,
bytes: Bytes,
) -> impl Stream<Item = Result<Bytes, UploadError>> + Unpin {
match self {
Range::RangeStart(start) => once(ready(Ok(bytes.slice(*start as usize..)))),
Range::SuffixLength(from_start) => {
once(ready(Ok(bytes.slice(..*from_start as usize))))
}
Range::Segment(start, end) => {
once(ready(Ok(bytes.slice(*start as usize..*end as usize))))
}
}
}
pub(crate) async fn chop_file(
&self,
mut file: tokio::fs::File,