Skip to content

Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.

11.8 KB
Raw Blame
use std::path::Path;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;

use anyhow::anyhow;
use tauri::Emitter;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

use libreget_core::core::filename;

use super::api::{self, AfyaCourse, AfyaSession};

#[derive(Debug, Clone, serde::Serialize)]
pub struct AfyaCourseDownloadProgress {
    pub course_id: String,
    pub course_name: String,
    pub percent: f64,
    pub current_module: String,
    #[serde(rename = "current_page")]
    pub current_lesson: String,
    pub downloaded_bytes: u64,
    #[serde(rename = "total_pages")]
    pub total_lessons: u32,
    #[serde(rename = "completed_pages")]
    pub completed_lessons: u32,
    pub total_modules: u32,
    pub current_module_index: u32,
}

pub async fn download_full_course(
    app: &tauri::AppHandle,
    session: &AfyaSession,
    course: &AfyaCourse,
    output_dir: &str,
    cancel_token: CancellationToken,
) -> anyhow::Result<()> {
    let modules = api::get_modules(session, &course.product_id).await?;

    if modules.is_empty() {
        return Err(anyhow!(
            "'{}' has no modules available for download",
            course.name
        ));
    }

    let course_dir = format!(
        "{}/{}",
        output_dir,
        filename::sanitize_path_component(&course.name)
    );
    tokio::fs::create_dir_all(&course_dir).await?;

    if libreget_core::core::course_utils::is_course_complete(&course_dir) {
        tracing::info!("[afya] course already complete, skipping");
        return Ok(());
    }

    let total_lessons: usize = modules.iter().map(|m| m.lessons.len()).sum();
    let total_modules = modules.len();
    let total_bytes = Arc::new(AtomicU64::new(0));
    let completed = Arc::new(AtomicUsize::new(0));

    let _ = app.emit(
        "download-progress",
        &AfyaCourseDownloadProgress {
            course_id: course.id.clone(),
            course_name: course.name.clone(),
            percent: 0.0,
            current_module: "Starting...".to_string(),
            current_lesson: String::new(),
            downloaded_bytes: 0,
            total_lessons: total_lessons as u32,
            completed_lessons: 0,
            total_modules: total_modules as u32,
            current_module_index: 0,
        },
    );

    for (mi, module) in modules.iter().enumerate() {
        if cancel_token.is_cancelled() {
            return Err(anyhow!("Download cancelled by user"));
        }

        let mod_name = filename::sanitize_path_component(&module.name);
        let mod_dir = format!("{}/{}. {}", course_dir, mi + 1, mod_name);
        tokio::fs::create_dir_all(&mod_dir).await?;

        for (li, lesson) in module.lessons.iter().enumerate() {
            if cancel_token.is_cancelled() {
                return Err(anyhow!("Download cancelled by user"));
            }

            let lesson_name = filename::sanitize_path_component(&lesson.name);
            let lesson_dir = format!("{}/{}. {}", mod_dir, li + 1, lesson_name);
            tokio::fs::create_dir_all(&lesson_dir).await?;

            if let Some(ref desc) = lesson.description {
                libreget_core::core::course_utils::save_description(&lesson_dir, desc, "txt")
                    .await
                    .ok();
            }

            if lesson.type_id == "videoClass" || lesson.type_id.contains("video") {
                match api::get_video_url(session, &lesson.id, &lesson.product_id).await {
                    Ok(video_url) => {
                        let video_path = format!("{}/{}. {}.mp4", lesson_dir, li + 1, lesson_name);

                        if tokio::fs::try_exists(&video_path).await.unwrap_or(false) {
                            let meta = tokio::fs::metadata(&video_path).await;
                            if meta.map(|m| m.len() > 0).unwrap_or(false) {
                                tracing::info!("[afya] Skipping existing: {}", video_path);
                                let done = completed.fetch_add(1, Ordering::Relaxed) + 1;
                                let _ = app.emit(
                                    "download-progress",
                                    &AfyaCourseDownloadProgress {
                                        course_id: course.id.clone(),
                                        course_name: course.name.clone(),
                                        percent: done as f64 / total_lessons as f64 * 100.0,
                                        current_module: module.name.clone(),
                                        current_lesson: lesson.name.clone(),
                                        downloaded_bytes: total_bytes.load(Ordering::Relaxed),
                                        total_lessons: total_lessons as u32,
                                        completed_lessons: done as u32,
                                        total_modules: total_modules as u32,
                                        current_module_index: (mi + 1) as u32,
                                    },
                                );
                                continue;
                            }
                        }

                        if video_url.contains(".m3u8") {
                            match download_hls_video(&video_url, &video_path, &cancel_token).await {
                                Ok(size) => {
                                    total_bytes.fetch_add(size, Ordering::Relaxed);
                                }
                                Err(e) => {
                                    tracing::error!(
                                        "[afya] HLS download failed for '{}': {}",
                                        lesson.name,
                                        e
                                    );
                                    let _ = tokio::fs::remove_file(&video_path).await;
                                }
                            }
                        } else {
                            match download_with_ytdlp(&video_url, &lesson_dir, &cancel_token).await
                            {
                                Ok(size) => {
                                    total_bytes.fetch_add(size, Ordering::Relaxed);
                                }
                                Err(e) => {
                                    tracing::error!(
                                        "[afya] yt-dlp download failed for '{}': {}, trying direct download",
                                        lesson.name,
                                        e
                                    );
                                    match download_file_direct(
                                        &session.client,
                                        &video_url,
                                        &video_path,
                                        &cancel_token,
                                    )
                                    .await
                                    {
                                        Ok(size) => {
                                            total_bytes.fetch_add(size, Ordering::Relaxed);
                                        }
                                        Err(e2) => {
                                            tracing::error!(
                                                "[afya] Direct download also failed for '{}': {}",
                                                lesson.name,
                                                e2
                                            );
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Err(e) => {
                        tracing::error!(
                            "[afya] Failed to get video URL for '{}': {}",
                            lesson.name,
                            e
                        );
                    }
                }
            }

            for material in &lesson.materials {
                if cancel_token.is_cancelled() {
                    return Err(anyhow!("Download cancelled by user"));
                }

                let ext = material.url.rsplit('.').next().unwrap_or("pdf");
                let ext = ext.split('?').next().unwrap_or(ext);
                let mat_name = filename::sanitize_path_component(&material.title);
                let mat_path = format!("{}/{}.{}", lesson_dir, mat_name, ext);

                if tokio::fs::try_exists(&mat_path).await.unwrap_or(false) {
                    let meta = tokio::fs::metadata(&mat_path).await;
                    if meta.map(|m| m.len() > 0).unwrap_or(false) {
                        continue;
                    }
                }

                match download_file_direct(&session.client, &material.url, &mat_path, &cancel_token)
                    .await
                {
                    Ok(size) => {
                        total_bytes.fetch_add(size, Ordering::Relaxed);
                    }
                    Err(e) => {
                        tracing::error!(
                            "[afya] Material download failed for '{}': {}",
                            material.title,
                            e
                        );
                    }
                }
            }

            let done = completed.fetch_add(1, Ordering::Relaxed) + 1;
            let _ = app.emit(
                "download-progress",
                &AfyaCourseDownloadProgress {
                    course_id: course.id.clone(),
                    course_name: course.name.clone(),
                    percent: done as f64 / total_lessons as f64 * 100.0,
                    current_module: module.name.clone(),
                    current_lesson: lesson.name.clone(),
                    downloaded_bytes: total_bytes.load(Ordering::Relaxed),
                    total_lessons: total_lessons as u32,
                    completed_lessons: done as u32,
                    total_modules: total_modules as u32,
                    current_module_index: (mi + 1) as u32,
                },
            );
        }
    }

    if cancel_token.is_cancelled() {
        return Err(anyhow!("Download cancelled by user"));
    }

    libreget_core::core::course_utils::mark_course_complete(&course_dir)
        .await
        .ok();

    Ok(())
}

async fn download_hls_video(
    m3u8_url: &str,
    output_path: &str,
    cancel_token: &CancellationToken,
) -> anyhow::Result<u64> {
    use libreget_core::core::media_processor::MediaProcessor;

    let _result = MediaProcessor::download_hls(
        m3u8_url,
        output_path,
        "https://alunos-internato.afya.com.br/",
        None,
        cancel_token.clone(),
        20,
        3,
        None,
    )
    .await?;

    let meta = tokio::fs::metadata(output_path).await?;
    Ok(meta.len())
}

async fn download_with_ytdlp(
    url: &str,
    output_dir: &str,
    cancel_token: &CancellationToken,
) -> anyhow::Result<u64> {
    let ytdlp_path = libreget_core::core::ytdlp::ensure_ytdlp().await?;
    let out_dir = Path::new(output_dir);
    let (tx, _rx) = mpsc::channel(8);

    let result = libreget_core::core::ytdlp::download_video(
        &ytdlp_path,
        url,
        out_dir,
        None,
        tx,
        None,
        None,
        None,
        Some("https://alunos-internato.afya.com.br/"),
        cancel_token.clone(),
        None,
        8,
        false,
        &[],
        None,
    )
    .await?;

    Ok(result.file_size_bytes)
}

async fn download_file_direct(
    client: &reqwest::Client,
    url: &str,
    output_path: &str,
    cancel_token: &CancellationToken,
) -> anyhow::Result<u64> {
    let out = Path::new(output_path);
    let (tx, _rx) = mpsc::channel(8);

    libreget_core::core::direct_downloader::download_direct(
        client,
        url,
        out,
        tx,
        Some(cancel_token),
    )
    .await
}