mirror of
https://git.asonix.dog/asonix/pict-rs
synced 2024-11-10 06:25:00 +00:00
Use futures-util instead of hand-implemented types
This commit is contained in:
parent
eccb7da3e9
commit
ac79a9d5cd
7 changed files with 11 additions and 63 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -934,7 +934,7 @@ dependencies = [
|
||||||
"awc",
|
"awc",
|
||||||
"base64",
|
"base64",
|
||||||
"dashmap",
|
"dashmap",
|
||||||
"futures-core",
|
"futures-util",
|
||||||
"mime",
|
"mime",
|
||||||
"num_cpus",
|
"num_cpus",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
|
|
@ -18,7 +18,7 @@ anyhow = "1.0"
|
||||||
awc = { version = "3.0.0-beta.7", default-features = false }
|
awc = { version = "3.0.0-beta.7", default-features = false }
|
||||||
base64 = "0.13.0"
|
base64 = "0.13.0"
|
||||||
dashmap = "4.0.2"
|
dashmap = "4.0.2"
|
||||||
futures-core = "0.3.17"
|
futures-util = "0.3.17"
|
||||||
mime = "0.3.1"
|
mime = "0.3.1"
|
||||||
num_cpus = "1.13"
|
num_cpus = "1.13"
|
||||||
once_cell = "1.4.0"
|
once_cell = "1.4.0"
|
||||||
|
|
|
@ -7,7 +7,7 @@ use actix_web::{
|
||||||
};
|
};
|
||||||
use awc::Client;
|
use awc::Client;
|
||||||
use dashmap::{mapref::entry::Entry, DashMap};
|
use dashmap::{mapref::entry::Entry, DashMap};
|
||||||
use futures_core::stream::Stream;
|
use futures_util::{stream::{LocalBoxStream, once}, Stream};
|
||||||
use once_cell::sync::{Lazy, OnceCell};
|
use once_cell::sync::{Lazy, OnceCell};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashSet,
|
collections::HashSet,
|
||||||
|
@ -42,7 +42,6 @@ use self::{
|
||||||
config::{Config, Format},
|
config::{Config, Format},
|
||||||
error::UploadError,
|
error::UploadError,
|
||||||
middleware::{Internal, Tracing},
|
middleware::{Internal, Tracing},
|
||||||
stream::{once, LocalBoxStream},
|
|
||||||
upload_manager::{Details, UploadManager},
|
upload_manager::{Details, UploadManager},
|
||||||
validate::{image_webp, video_mp4},
|
validate::{image_webp, video_mp4},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use crate::stream::LocalBoxFuture;
|
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
dev::{Service, ServiceRequest, Transform},
|
dev::{Service, ServiceRequest, Transform},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
HttpResponse, ResponseError,
|
HttpResponse, ResponseError,
|
||||||
};
|
};
|
||||||
|
use futures_util::future::LocalBoxFuture;
|
||||||
use std::{
|
use std::{
|
||||||
future::{ready, Ready},
|
future::{ready, Ready},
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
use crate::{
|
use crate::{stream::bytes_stream, UploadError};
|
||||||
stream::{bytes_stream, LocalBoxStream},
|
|
||||||
UploadError,
|
|
||||||
};
|
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
dev::Payload,
|
dev::Payload,
|
||||||
http::{
|
http::{
|
||||||
|
@ -11,6 +8,7 @@ use actix_web::{
|
||||||
web::Bytes,
|
web::Bytes,
|
||||||
FromRequest, HttpRequest,
|
FromRequest, HttpRequest,
|
||||||
};
|
};
|
||||||
|
use futures_util::stream::LocalBoxStream;
|
||||||
use std::{future::ready, io};
|
use std::{future::ready, io};
|
||||||
use tokio::io::{AsyncReadExt, AsyncSeekExt};
|
use tokio::io::{AsyncReadExt, AsyncSeekExt};
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::error::UploadError;
|
use crate::error::UploadError;
|
||||||
use actix_web::web::{Bytes, BytesMut};
|
use actix_web::web::{Bytes, BytesMut};
|
||||||
use futures_core::stream::Stream;
|
use futures_util::Stream;
|
||||||
use std::{
|
use std::{
|
||||||
future::Future,
|
future::Future,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
|
@ -8,9 +8,6 @@ use std::{
|
||||||
};
|
};
|
||||||
use tokio::io::{AsyncRead, AsyncWriteExt, ReadBuf};
|
use tokio::io::{AsyncRead, AsyncWriteExt, ReadBuf};
|
||||||
|
|
||||||
pub(crate) type LocalBoxStream<'a, T> = Pin<Box<dyn Stream<Item = T> + 'a>>;
|
|
||||||
pub(crate) type LocalBoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
|
|
||||||
|
|
||||||
pub(crate) struct Process {
|
pub(crate) struct Process {
|
||||||
child: tokio::process::Child,
|
child: tokio::process::Child,
|
||||||
}
|
}
|
||||||
|
@ -23,12 +20,6 @@ pub(crate) struct ProcessRead<I> {
|
||||||
|
|
||||||
struct BytesFreezer<S>(S);
|
struct BytesFreezer<S>(S);
|
||||||
|
|
||||||
pub(crate) struct Once<T> {
|
|
||||||
inner: Option<T>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct Next<'a, S>(&'a mut S);
|
|
||||||
|
|
||||||
impl Process {
|
impl Process {
|
||||||
fn new(child: tokio::process::Child) -> Self {
|
fn new(child: tokio::process::Child) -> Self {
|
||||||
Process { child }
|
Process { child }
|
||||||
|
@ -92,14 +83,6 @@ pub(crate) fn bytes_stream(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn once<T>(input: T) -> Once<T> {
|
|
||||||
Once { inner: Some(input) }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn next<'a, S>(stream: &'a mut S) -> Next<'a, S> {
|
|
||||||
Next(stream)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<I> AsyncRead for ProcessRead<I>
|
impl<I> AsyncRead for ProcessRead<I>
|
||||||
where
|
where
|
||||||
I: AsyncRead + Unpin,
|
I: AsyncRead + Unpin,
|
||||||
|
@ -139,35 +122,3 @@ where
|
||||||
.map_err(UploadError::from)
|
.map_err(UploadError::from)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Stream for Once<T>
|
|
||||||
where
|
|
||||||
T: Future + Unpin,
|
|
||||||
{
|
|
||||||
type Item = <T as Future>::Output;
|
|
||||||
|
|
||||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
||||||
if let Some(mut fut) = self.inner.take() {
|
|
||||||
match Pin::new(&mut fut).poll(cx) {
|
|
||||||
Poll::Ready(item) => Poll::Ready(Some(item)),
|
|
||||||
Poll::Pending => {
|
|
||||||
self.inner = Some(fut);
|
|
||||||
Poll::Pending
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Poll::Ready(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, S> Future for Next<'a, S>
|
|
||||||
where
|
|
||||||
S: Stream + Unpin,
|
|
||||||
{
|
|
||||||
type Output = Option<<S as Stream>::Item>;
|
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
Pin::new(&mut self.0).poll_next(cx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,10 +2,10 @@ use crate::{
|
||||||
config::Format,
|
config::Format,
|
||||||
error::UploadError,
|
error::UploadError,
|
||||||
migrate::{alias_id_key, alias_key, alias_key_bounds, variant_key_bounds, LatestDb},
|
migrate::{alias_id_key, alias_key, alias_key_bounds, variant_key_bounds, LatestDb},
|
||||||
stream::{next, LocalBoxStream},
|
|
||||||
to_ext,
|
to_ext,
|
||||||
};
|
};
|
||||||
use actix_web::web;
|
use actix_web::web;
|
||||||
|
use futures_util::stream::{LocalBoxStream, StreamExt};
|
||||||
use sha2::Digest;
|
use sha2::Digest;
|
||||||
use std::{
|
use std::{
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
|
@ -547,7 +547,7 @@ impl UploadManager {
|
||||||
let mut bytes_mut = actix_web::web::BytesMut::new();
|
let mut bytes_mut = actix_web::web::BytesMut::new();
|
||||||
|
|
||||||
debug!("Reading stream to memory");
|
debug!("Reading stream to memory");
|
||||||
while let Some(res) = next(&mut stream).await {
|
while let Some(res) = stream.next().await {
|
||||||
let bytes = res?;
|
let bytes = res?;
|
||||||
bytes_mut.extend_from_slice(&bytes);
|
bytes_mut.extend_from_slice(&bytes);
|
||||||
}
|
}
|
||||||
|
@ -582,7 +582,7 @@ impl UploadManager {
|
||||||
let mut bytes_mut = actix_web::web::BytesMut::new();
|
let mut bytes_mut = actix_web::web::BytesMut::new();
|
||||||
|
|
||||||
debug!("Reading stream to memory");
|
debug!("Reading stream to memory");
|
||||||
while let Some(res) = next(&mut stream).await {
|
while let Some(res) = stream.next().await {
|
||||||
let bytes = res?;
|
let bytes = res?;
|
||||||
bytes_mut.extend_from_slice(&bytes);
|
bytes_mut.extend_from_slice(&bytes);
|
||||||
}
|
}
|
||||||
|
@ -954,7 +954,7 @@ where
|
||||||
let fut = async move {
|
let fut = async move {
|
||||||
let mut file = tokio::fs::File::create(to1).await?;
|
let mut file = tokio::fs::File::create(to1).await?;
|
||||||
|
|
||||||
while let Some(res) = next(&mut stream).await {
|
while let Some(res) = stream.next().await {
|
||||||
let mut bytes = res?;
|
let mut bytes = res?;
|
||||||
file.write_all_buf(&mut bytes).await?;
|
file.write_all_buf(&mut bytes).await?;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue