2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 19:31:35 +00:00
This commit is contained in:
John Doe 2021-01-14 11:26:17 -05:00
parent c663337bcd
commit da237eacae

View file

@ -1,8 +1,23 @@
use actix_form_data::{Field, Form, Value}; use actix_form_data::{Field, Form, Value};
use actix_fs::file; use actix_fs::file;
use actix_web::{App, HttpRequest, HttpResponse, HttpServer, client::Client, guard, http::{HeaderValue, header::{ACCEPT_RANGES, CONTENT_LENGTH, CacheControl, CacheDirective, ContentRange, ContentRangeSpec, Header, LastModified}}, middleware::{Compress, Logger}, web}; use actix_web::{
client::Client,
guard,
http::{
header::{
CacheControl, CacheDirective, ContentRange, ContentRangeSpec, Header, LastModified,
ACCEPT_RANGES, CONTENT_LENGTH,
},
HeaderValue,
},
middleware::{Compress, Logger},
web, App, HttpRequest, HttpResponse, HttpServer,
};
use bytes::Bytes; use bytes::Bytes;
use futures::{StreamExt, stream::{Stream, TryStreamExt}}; use futures::{
stream::{Stream, TryStreamExt},
StreamExt,
};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::{ use std::{
collections::HashSet, convert::TryInto, io, path::PathBuf, sync::Once, time::SystemTime, collections::HashSet, convert::TryInto, io, path::PathBuf, sync::Once, time::SystemTime,
@ -428,7 +443,6 @@ async fn process(
drop(entered); drop(entered);
}); });
match req.headers().get("Range") { match req.headers().get("Range") {
Some(range_head) => { Some(range_head) => {
let range = parse_range_header(range_head)?; let range = parse_range_header(range_head)?;
@ -445,7 +459,8 @@ async fn process(
7 * DAYS, 7 * DAYS,
details.system_time(), details.system_time(),
Some((range[0], range[1])), Some((range[0], range[1])),
Some(img_bytes.len() as u64))); Some(img_bytes.len() as u64),
));
} }
None => { None => {
return Ok(srv_response( return Ok(srv_response(
@ -549,7 +564,11 @@ fn parse_range_header(range_head: &HeaderValue) -> Result<Vec<u64>, UploadError>
Ok(range) Ok(range)
} }
async fn ranged_file_resp(path: PathBuf, req: HttpRequest, details: Details) -> Result<HttpResponse, UploadError> { async fn ranged_file_resp(
path: PathBuf,
req: HttpRequest,
details: Details,
) -> Result<HttpResponse, UploadError> {
match req.headers().get("Range") { match req.headers().get("Range") {
//Range header exists - return as ranged //Range header exists - return as ranged
Some(range_head) => { Some(range_head) => {
@ -581,16 +600,14 @@ async fn ranged_file_resp(path: PathBuf, req: HttpRequest, details: Details) ->
.await? .await?
.take(whole_to + 1) .take(whole_to + 1)
.enumerate() .enumerate()
.map(move |bytes_res| { .map(move |bytes_res| match bytes_res.1 {
match bytes_res.1 { Ok(mut bytes) => {
Ok(mut bytes) => { if bytes_res.0 == whole_to && partial_len <= bytes.len() {
if bytes_res.0 == whole_to && partial_len <= bytes.len() { return Ok(bytes.split_to(partial_len));
return Ok(bytes.split_to(partial_len));
}
return Ok(bytes);
} }
Err(e) => Err(e), return Ok(bytes);
} }
Err(e) => Err(e),
}); });
return Ok(srv_ranged_response( return Ok(srv_ranged_response(