2022-04-03 01:56:29 +00:00
|
|
|
use crate::{
|
|
|
|
error::Error,
|
|
|
|
repo::{FullRepo, UploadId, UploadRepo},
|
|
|
|
store::Store,
|
|
|
|
};
|
|
|
|
use actix_web::web::Bytes;
|
|
|
|
use futures_util::{Stream, TryStreamExt};
|
2023-07-14 19:53:37 +00:00
|
|
|
use mime::APPLICATION_OCTET_STREAM;
|
2022-04-08 17:51:33 +00:00
|
|
|
use tracing::{Instrument, Span};
|
2022-04-03 01:56:29 +00:00
|
|
|
|
|
|
|
pub(crate) struct Backgrounded<R, S>
|
|
|
|
where
|
|
|
|
R: FullRepo + 'static,
|
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
repo: R,
|
|
|
|
identifier: Option<S::Identifier>,
|
|
|
|
upload_id: Option<UploadId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R, S> Backgrounded<R, S>
|
|
|
|
where
|
|
|
|
R: FullRepo + 'static,
|
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
pub(crate) fn disarm(mut self) {
|
|
|
|
let _ = self.identifier.take();
|
|
|
|
let _ = self.upload_id.take();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn upload_id(&self) -> Option<UploadId> {
|
|
|
|
self.upload_id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn identifier(&self) -> Option<&S::Identifier> {
|
|
|
|
self.identifier.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn proxy<P>(repo: R, store: S, stream: P) -> Result<Self, Error>
|
|
|
|
where
|
2022-09-24 22:18:53 +00:00
|
|
|
P: Stream<Item = Result<Bytes, Error>> + Unpin + 'static,
|
2022-04-03 01:56:29 +00:00
|
|
|
{
|
|
|
|
let mut this = Self {
|
|
|
|
repo,
|
|
|
|
identifier: None,
|
|
|
|
upload_id: Some(UploadId::generate()),
|
|
|
|
};
|
|
|
|
|
|
|
|
this.do_proxy(store, stream).await?;
|
|
|
|
|
|
|
|
Ok(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn do_proxy<P>(&mut self, store: S, stream: P) -> Result<(), Error>
|
|
|
|
where
|
2022-09-24 22:18:53 +00:00
|
|
|
P: Stream<Item = Result<Bytes, Error>> + Unpin + 'static,
|
2022-04-03 01:56:29 +00:00
|
|
|
{
|
|
|
|
UploadRepo::create(&self.repo, self.upload_id.expect("Upload id exists")).await?;
|
|
|
|
|
|
|
|
let stream = stream.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e));
|
|
|
|
|
2023-07-14 19:53:37 +00:00
|
|
|
// use octet-stream, we don't know the upload's real type yet
|
|
|
|
let identifier = store.save_stream(stream, APPLICATION_OCTET_STREAM).await?;
|
2022-04-03 01:56:29 +00:00
|
|
|
|
2022-04-03 02:15:39 +00:00
|
|
|
self.identifier = Some(identifier);
|
2022-04-03 01:56:29 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R, S> Drop for Backgrounded<R, S>
|
|
|
|
where
|
|
|
|
R: FullRepo + 'static,
|
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
fn drop(&mut self) {
|
2022-10-02 03:47:52 +00:00
|
|
|
if self.identifier.is_some() || self.upload_id.is_some() {
|
|
|
|
let cleanup_parent_span =
|
|
|
|
tracing::info_span!(parent: None, "Dropped backgrounded cleanup");
|
|
|
|
cleanup_parent_span.follows_from(Span::current());
|
|
|
|
|
|
|
|
if let Some(identifier) = self.identifier.take() {
|
|
|
|
let repo = self.repo.clone();
|
|
|
|
|
2022-12-08 04:43:12 +00:00
|
|
|
let cleanup_span = tracing::info_span!(parent: &cleanup_parent_span, "Backgrounded cleanup Identifier", identifier = ?identifier);
|
2022-10-02 03:47:52 +00:00
|
|
|
|
|
|
|
tracing::trace_span!(parent: None, "Spawn task").in_scope(|| {
|
|
|
|
actix_rt::spawn(
|
|
|
|
async move {
|
|
|
|
let _ = crate::queue::cleanup_identifier(&repo, identifier).await;
|
|
|
|
}
|
|
|
|
.instrument(cleanup_span),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(upload_id) = self.upload_id {
|
|
|
|
let repo = self.repo.clone();
|
|
|
|
|
2022-12-08 04:43:12 +00:00
|
|
|
let cleanup_span = tracing::info_span!(parent: &cleanup_parent_span, "Backgrounded cleanup Upload ID", upload_id = ?upload_id);
|
2022-10-02 03:47:52 +00:00
|
|
|
|
|
|
|
tracing::trace_span!(parent: None, "Spawn task").in_scope(|| {
|
|
|
|
actix_rt::spawn(
|
|
|
|
async move {
|
|
|
|
let _ = repo.claim(upload_id).await;
|
|
|
|
}
|
|
|
|
.instrument(cleanup_span),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
2022-04-03 01:56:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|