pict-rs/src/range.rs

54 lines
1.3 KiB
Rust
Raw Permalink Normal View History

use std::sync::Arc;
2021-10-23 04:48:56 +00:00
use crate::{
error::{Error, UploadError},
store::Store,
};
use actix_web::{
2022-03-01 17:23:15 +00:00
http::header::{ByteRangeSpec, ContentRange, ContentRangeSpec, Range},
web::Bytes,
};
2023-08-23 16:59:42 +00:00
use futures_core::Stream;
2022-03-01 17:23:15 +00:00
pub(crate) async fn chop_store<S: Store>(
byte_range: &ByteRangeSpec,
store: &S,
identifier: &Arc<str>,
2022-03-01 17:23:15 +00:00
length: u64,
) -> Result<impl Stream<Item = std::io::Result<Bytes>>, Error> {
2022-03-01 17:23:15 +00:00
if let Some((start, end)) = byte_range.to_satisfiable_range(length) {
// END IS INCLUSIVE
let end = end + 1;
return store
2022-03-01 17:23:15 +00:00
.to_stream(identifier, Some(start), Some(end.saturating_sub(start)))
.await
.map_err(Error::from);
}
2022-03-01 17:23:15 +00:00
Err(UploadError::Range.into())
}
2022-03-01 17:23:15 +00:00
pub(crate) fn single_bytes_range(range: &Range) -> Option<&ByteRangeSpec> {
if let Range::Bytes(ranges) = range {
if ranges.len() == 1 {
2024-01-04 16:42:34 +00:00
return ranges.first();
}
}
2022-03-01 17:23:15 +00:00
None
}
2022-03-01 17:23:15 +00:00
pub(crate) fn to_content_range(
byte_range: &ByteRangeSpec,
instance_length: u64,
) -> Option<ContentRange> {
byte_range
.to_satisfiable_range(instance_length)
.map(|range| {
ContentRange(ContentRangeSpec::Bytes {
range: Some(range),
instance_length: Some(instance_length),
})
})
}