Create downloader layer + manage library installation
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
use crate::app::Application;
|
||||
use crate::layer::Layer;
|
||||
use crate::types::{Playlist, Video};
|
||||
use eframe::egui::{Context, Ui};
|
||||
use eframe::{Frame, egui};
|
||||
use std::path::PathBuf;
|
||||
use tokio::runtime::Handle;
|
||||
use tracing::warn;
|
||||
use yt_dlp::client::LibraryInstaller;
|
||||
|
||||
pub(crate) struct Downloader {
|
||||
video_queue: Vec<Video>,
|
||||
playlists_queue: Vec<Playlist>,
|
||||
yt_downloader: Option<yt_dlp::Downloader>,
|
||||
|
||||
tokio_handle: Handle,
|
||||
|
||||
yt_dlp_installed: bool,
|
||||
rejected_yt_dlp_install: bool,
|
||||
|
||||
ffmpeg_installed: bool,
|
||||
rejected_ffmpeg_install: bool,
|
||||
}
|
||||
|
||||
impl Downloader {
|
||||
pub fn new() -> Self {
|
||||
let yt_downloader = None;
|
||||
Self {
|
||||
video_queue: Vec::new(),
|
||||
playlists_queue: Vec::new(),
|
||||
yt_downloader,
|
||||
tokio_handle: Handle::current(),
|
||||
yt_dlp_installed: false,
|
||||
rejected_yt_dlp_install: false,
|
||||
ffmpeg_installed: false,
|
||||
rejected_ffmpeg_install: false,
|
||||
}
|
||||
}
|
||||
|
||||
async fn install_yt_dlp(data_dir: String) -> Result<(), String> {
|
||||
let libraries_dir = PathBuf::from(format!("{}/libs", &data_dir));
|
||||
let installer = LibraryInstaller::new(libraries_dir);
|
||||
if let Err(e) = installer.install_youtube(None).await {
|
||||
warn!("Failed to install yt-dlp: {}", e);
|
||||
return Err(e.to_string());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn install_ffmpeg(data_dir: String) -> Result<(), String> {
|
||||
let libraries_dir = PathBuf::from(format!("{}/libs", &data_dir));
|
||||
let installer = LibraryInstaller::new(libraries_dir);
|
||||
if let Err(e) = installer.install_ffmpeg(None).await {
|
||||
warn!("Failed to install yt-dlp: {}", e);
|
||||
return Err(e.to_string());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn create_new_yt_downloader(application: &Application) -> Option<yt_dlp::Downloader> {
|
||||
Err("Not implemented".to_string()).expect("Soon")
|
||||
}
|
||||
|
||||
fn check_yt_dlp(data_dir: String) -> bool {
|
||||
let path = PathBuf::from(format!("{}/libs/yt-dlp", &data_dir));
|
||||
path.exists()
|
||||
}
|
||||
|
||||
fn check_ffmpeg(data_dir: String) -> bool {
|
||||
let path = PathBuf::from(format!("{}/libs/ffmpeg", &data_dir));
|
||||
path.exists()
|
||||
}
|
||||
}
|
||||
|
||||
impl Layer for Downloader {
|
||||
fn logic(&mut self, application: &mut Application, ctx: &Context, frame: &mut Frame) {
|
||||
if !self.yt_dlp_installed {
|
||||
if Downloader::check_yt_dlp(application.data_dir.clone()) {
|
||||
self.yt_dlp_installed = true;
|
||||
}
|
||||
}
|
||||
if !self.ffmpeg_installed {
|
||||
if Downloader::check_ffmpeg(application.data_dir.clone()) {
|
||||
self.ffmpeg_installed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn ui(&mut self, application: &mut Application, ui: &mut Ui, frame: &mut Frame) {
|
||||
if !self.yt_dlp_installed && !self.rejected_yt_dlp_install {
|
||||
egui::Window::new("Missing yt-dlp").show(ui, |ui| {
|
||||
ui.label("yt-dlp is not installed. Would you like to install it?");
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("Yes").clicked() {
|
||||
self.tokio_handle
|
||||
.spawn(Self::install_yt_dlp(application.data_dir.clone()));
|
||||
}
|
||||
if ui.button("No").clicked() {
|
||||
self.rejected_yt_dlp_install = true;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if !self.ffmpeg_installed && !self.rejected_ffmpeg_install {
|
||||
egui::Window::new("Missing ffmpeg").show(ui, |ui| {
|
||||
ui.label("ffmpeg is not installed. Would you like to install it?");
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("Yes").clicked() {
|
||||
self.tokio_handle
|
||||
.spawn(Self::install_ffmpeg(application.data_dir.clone()));
|
||||
}
|
||||
if ui.button("No").clicked() {
|
||||
self.rejected_ffmpeg_install = true;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user