use std::collections::BTreeMap;
use std::fs::create_dir;
use std::path::PathBuf;
use crate::imdl::ImdlCommand;
use crate::options::CacheOptions;
use crate::queue::QueueItem;
use crate::transcode::TranscodeStatus;
use crate::verify::VerifyStatus;
use di::{inject, injectable, Ref};
use flat_db::{Hash, Table};
use futures::stream::{iter, StreamExt};
use log::error;
use rogue_logging::Error;
#[injectable]
pub struct Queue {
table: Table<20, 1, QueueItem>,
}
#[allow(dead_code)]
impl Queue {
#[allow(dead_code)]
pub fn from_path(path: PathBuf) -> Self {
Self {
table: Table::new(path),
}
}
#[inject]
pub fn from_options(options: Ref<CacheOptions>) -> Self {
let path = options.cache.clone().expect("queue path should be set");
let path = path.join("queue");
if !path.exists() {
create_dir(&path)
.expect("should be able to create queue directory if it does not exist");
}
Self::from_path(path)
}
pub fn get(&self, hash: Hash<20>) -> Result<Option<QueueItem>, Error> {
self.table.get(hash)
}
pub async fn get_unprocessed(
&mut self,
indexer: String,
transcode_enabled: bool,
upload_enabled: bool,
retry_failed_transcodes: bool,
) -> Result<Vec<Hash<20>>, Error> {
let items = self.table.get_all().await?;
let mut items: Vec<&QueueItem> = items
.values()
.filter(|item| {
item.indexer == indexer
&& exclude_verified_if_transcode_disabled(item, transcode_enabled)
&& exclude_transcoded_if_upload_disabled(item, upload_enabled)
&& exclude_verify_failures(item)
&& exclude_transcode_failures(item, retry_failed_transcodes)
&& item.upload.is_none()
})
.collect();
items.sort_by_key(|x| &x.name);
let hashes = items.iter().map(|x| x.hash).collect();
Ok(hashes)
}
pub async fn get_all(&mut self) -> Result<BTreeMap<Hash<20>, QueueItem>, Error> {
self.table.get_all().await
}
pub async fn set(&mut self, item: QueueItem) -> Result<(), Error> {
self.table.set(item.hash, item).await
}
pub async fn set_many(
&self,
items: BTreeMap<Hash<20>, QueueItem>,
replace: bool,
) -> Result<usize, Error> {
self.table.set_many(items, replace).await
}
pub async fn insert_new_torrent_files(&mut self, paths: Vec<PathBuf>) -> Result<usize, Error> {
let stream = iter(paths.into_iter());
let items: BTreeMap<_, _> = stream
.filter_map(|path| async {
let torrent = match ImdlCommand::show(&path).await {
Ok(torrent) => Some(torrent),
Err(error) => {
error!("Failed to read torrent: {}\n{error}", path.display());
None
}
};
let item = QueueItem::from_torrent(path, torrent?);
Some((item.hash, item))
})
.collect()
.await;
self.table.set_many(items, false).await
}
}
fn exclude_verify_failures(item: &QueueItem) -> bool {
!matches!(
item.verify,
Some(VerifyStatus {
verified: false,
..
})
)
}
fn exclude_transcode_failures(item: &QueueItem, retry_failed_transcodes: bool) -> bool {
retry_failed_transcodes
|| !matches!(item.transcode, Some(TranscodeStatus { success: false, .. }))
}
fn exclude_verified_if_transcode_disabled(item: &QueueItem, transcode_enabled: bool) -> bool {
transcode_enabled || item.verify.is_none()
}
fn exclude_transcoded_if_upload_disabled(item: &QueueItem, upload_enabled: bool) -> bool {
upload_enabled || !matches!(item.transcode, Some(TranscodeStatus { success: true, .. }))
}