2022-04-01 21:51:12 +00:00
|
|
|
use crate::{
|
|
|
|
error::Error,
|
2023-07-13 03:12:21 +00:00
|
|
|
formats::InputProcessableFormat,
|
2022-04-02 21:44:03 +00:00
|
|
|
ingest::Session,
|
2022-10-02 03:47:52 +00:00
|
|
|
queue::{Base64Bytes, LocalBoxFuture, Process},
|
2022-04-02 21:44:03 +00:00
|
|
|
repo::{Alias, DeleteToken, FullRepo, UploadId, UploadResult},
|
2022-04-01 21:51:12 +00:00
|
|
|
serde_str::Serde,
|
2022-04-02 21:44:03 +00:00
|
|
|
store::{Identifier, Store},
|
2023-07-13 22:42:21 +00:00
|
|
|
CONFIG,
|
2022-04-01 21:51:12 +00:00
|
|
|
};
|
2022-04-02 21:44:03 +00:00
|
|
|
use futures_util::TryStreamExt;
|
2022-04-01 21:51:12 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
pub(super) fn perform<'a, R, S>(
|
|
|
|
repo: &'a R,
|
|
|
|
store: &'a S,
|
|
|
|
job: &'a [u8],
|
|
|
|
) -> LocalBoxFuture<'a, Result<(), Error>>
|
|
|
|
where
|
2022-04-02 21:44:03 +00:00
|
|
|
R: FullRepo + 'static,
|
2022-04-02 22:40:04 +00:00
|
|
|
S: Store + 'static,
|
2022-04-01 21:51:12 +00:00
|
|
|
{
|
|
|
|
Box::pin(async move {
|
|
|
|
match serde_json::from_slice(job) {
|
|
|
|
Ok(job) => match job {
|
|
|
|
Process::Ingest {
|
2022-10-02 03:47:52 +00:00
|
|
|
identifier: Base64Bytes(identifier),
|
2022-04-01 21:51:12 +00:00
|
|
|
upload_id,
|
|
|
|
declared_alias,
|
|
|
|
} => {
|
2022-04-02 21:44:03 +00:00
|
|
|
process_ingest(
|
2022-04-01 21:51:12 +00:00
|
|
|
repo,
|
|
|
|
store,
|
|
|
|
identifier,
|
2022-04-03 01:56:29 +00:00
|
|
|
Serde::into_inner(upload_id),
|
2022-04-01 21:51:12 +00:00
|
|
|
declared_alias.map(Serde::into_inner),
|
2023-07-13 22:42:21 +00:00
|
|
|
&CONFIG.media,
|
2022-04-01 21:51:12 +00:00
|
|
|
)
|
|
|
|
.await?
|
|
|
|
}
|
|
|
|
Process::Generate {
|
|
|
|
target_format,
|
|
|
|
source,
|
|
|
|
process_path,
|
|
|
|
process_args,
|
|
|
|
} => {
|
|
|
|
generate(
|
|
|
|
repo,
|
|
|
|
store,
|
|
|
|
target_format,
|
|
|
|
Serde::into_inner(source),
|
|
|
|
process_path,
|
|
|
|
process_args,
|
|
|
|
)
|
|
|
|
.await?
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
2023-01-29 17:57:59 +00:00
|
|
|
tracing::warn!("Invalid job: {}", format!("{e}"));
|
2022-04-01 21:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-02 03:47:52 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2022-04-02 21:44:03 +00:00
|
|
|
async fn process_ingest<R, S>(
|
2022-04-01 21:51:12 +00:00
|
|
|
repo: &R,
|
|
|
|
store: &S,
|
2022-04-02 21:44:03 +00:00
|
|
|
unprocessed_identifier: Vec<u8>,
|
|
|
|
upload_id: UploadId,
|
2022-04-01 21:51:12 +00:00
|
|
|
declared_alias: Option<Alias>,
|
2023-07-13 22:42:21 +00:00
|
|
|
media: &crate::config::Media,
|
2022-04-02 21:44:03 +00:00
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
R: FullRepo + 'static,
|
|
|
|
S: Store,
|
|
|
|
{
|
|
|
|
let fut = async {
|
|
|
|
let unprocessed_identifier = S::Identifier::from_bytes(unprocessed_identifier)?;
|
|
|
|
|
|
|
|
let stream = store
|
|
|
|
.to_stream(&unprocessed_identifier, None, None)
|
|
|
|
.await?
|
|
|
|
.map_err(Error::from);
|
|
|
|
|
2023-07-13 22:42:21 +00:00
|
|
|
let session = crate::ingest::ingest(repo, store, stream, declared_alias, media).await?;
|
2022-04-02 21:44:03 +00:00
|
|
|
|
|
|
|
let token = session.delete_token().await?;
|
|
|
|
|
2022-04-03 02:15:39 +00:00
|
|
|
store.remove(&unprocessed_identifier).await?;
|
|
|
|
|
2022-04-02 21:44:03 +00:00
|
|
|
Ok((session, token)) as Result<(Session<R, S>, DeleteToken), Error>
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = match fut.await {
|
|
|
|
Ok((mut session, token)) => {
|
|
|
|
let alias = session.alias().take().expect("Alias should exist").clone();
|
|
|
|
let result = UploadResult::Success { alias, token };
|
|
|
|
session.disarm();
|
|
|
|
result
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2023-07-10 22:15:43 +00:00
|
|
|
tracing::warn!("Failed to ingest\n{}\n{}", format!("{e}"), format!("{e:?}"));
|
2022-04-02 21:44:03 +00:00
|
|
|
|
|
|
|
UploadResult::Failure {
|
|
|
|
message: e.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
repo.complete(upload_id, result).await?;
|
|
|
|
|
|
|
|
Ok(())
|
2022-04-01 21:51:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-02 03:47:52 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2022-04-02 22:40:04 +00:00
|
|
|
async fn generate<R: FullRepo, S: Store + 'static>(
|
2022-04-01 21:51:12 +00:00
|
|
|
repo: &R,
|
|
|
|
store: &S,
|
2023-07-13 03:12:21 +00:00
|
|
|
target_format: InputProcessableFormat,
|
2022-04-01 21:51:12 +00:00
|
|
|
source: Alias,
|
|
|
|
process_path: PathBuf,
|
|
|
|
process_args: Vec<String>,
|
|
|
|
) -> Result<(), Error> {
|
2023-07-05 21:46:44 +00:00
|
|
|
let Some(hash) = repo.hash(&source).await? else {
|
|
|
|
// Nothing to do
|
|
|
|
return Ok(());
|
|
|
|
};
|
2022-04-02 22:40:04 +00:00
|
|
|
|
|
|
|
let path_string = process_path.to_string_lossy().to_string();
|
|
|
|
let identifier_opt = repo
|
|
|
|
.variant_identifier::<S::Identifier>(hash.clone(), path_string)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
if identifier_opt.is_some() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-10-01 00:38:11 +00:00
|
|
|
let original_details = crate::ensure_details(repo, store, &source).await?;
|
|
|
|
|
2022-04-02 22:40:04 +00:00
|
|
|
crate::generate::generate(
|
|
|
|
repo,
|
|
|
|
store,
|
|
|
|
target_format,
|
|
|
|
source,
|
|
|
|
process_path,
|
|
|
|
process_args,
|
2023-07-13 18:48:59 +00:00
|
|
|
original_details.video_format(),
|
2022-10-01 00:38:11 +00:00
|
|
|
None,
|
2022-04-02 22:40:04 +00:00
|
|
|
hash,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
2022-04-01 21:51:12 +00:00
|
|
|
}
|