diff options
Diffstat (limited to 'import')
-rw-r--r-- | import/src/lib.rs | 71 |
1 files changed, 46 insertions, 25 deletions
diff --git a/import/src/lib.rs b/import/src/lib.rs index 6639789..e69e203 100644 --- a/import/src/lib.rs +++ b/import/src/lib.rs @@ -11,11 +11,15 @@ use anyhow::{anyhow, Context, Ok}; use async_recursion::async_recursion; use futures::{stream::FuturesUnordered, StreamExt}; use jellybase::{ - cache::async_cache_file, database::Database, federation::Federation, AssetLocationExt, CONF, + cache::{async_cache_file, cache_file}, + database::Database, + federation::Federation, + AssetLocationExt, CONF, }; use jellyclient::Session; use jellycommon::{ - AssetLocation, AssetRole, ImportOptions, ImportSource, Node, NodePrivate, NodePublic, + AssetLocation, AssetRole, ImportOptions, ImportSource, MediaInfo, Node, NodePrivate, + NodePublic, TrackSource, }; use jellymatroska::read::EbmlReader; use jellyremuxer::import::import_metadata; @@ -24,12 +28,12 @@ use std::{ cmp::Ordering, ffi::OsStr, fs::File, - io::BufReader, + io::{BufReader, Write}, os::unix::prelude::OsStrExt, path::{Path, PathBuf}, sync::{Arc, LazyLock}, }; -use tokio::{sync::Semaphore, task::spawn_blocking}; +use tokio::{io::AsyncWriteExt, sync::Semaphore, task::spawn_blocking}; static IMPORT_SEM: LazyLock<Semaphore> = LazyLock::new(|| Semaphore::new(1)); @@ -179,45 +183,62 @@ async fn process_source( }; match s { ImportSource::Override(n) => insert_node(&id, n)?, - ImportSource::Tmdb(_) => todo!(), + ImportSource::Tmdb { id } => { + todo!() + } ImportSource::Media { location, .. } => { // TODO use ignore options let media_path = location.path(); let metadata = spawn_blocking(move || { - let input = BufReader::new(File::open(&media_path).unwrap()); + let input = + BufReader::new(File::open(&location.path()).context("opening media file")?); let mut input = EbmlReader::new(input); import_metadata(&mut input) }) .await??; - // if let Some(cover) = metadata.cover { - // let pu = path.join(format!( - // "cover.{}", - // match mime.as_str() { - // "image/webp" => "webp", - // "image/jpeg" => "jpeg", - // "image/png" => "png", - // _ => { - // warn!("unknown mime, just using webp"); - // "webp" - // } - // } - // )); - // if !pu.exists() { - // let mut f = tokio::fs::File::create(&pu).await?; - // f.write_all(&data).await?; - // } - // } + let poster = if let Some((filename, data)) = metadata.cover { + Some( + async_cache_file( + &[media_path.to_str().unwrap(), &filename], + |mut f| async move { + f.write_all(&data).await?; + Ok(()) + }, + ) + .await?, + ) + } else { + None + }; let node = Node { public: NodePublic { title: metadata.title, description: metadata.description, tagline: metadata.tagline, + media: Some(MediaInfo { + chapters: metadata.chapters, + duration: metadata.duration, + tracks: metadata.tracks, + }), + ..Default::default() + }, + private: NodePrivate { + poster, + source: Some( + metadata + .track_sources + .into_iter() + .map(|mut ts| { + ts.path = media_path.to_owned(); + TrackSource::Local(ts) + }) + .collect(), + ), ..Default::default() }, - private: NodePrivate::default(), }; insert_node(&id, node)?; } |