pub fn download_chunks(content_length: u64,
                       mut out_file: OutputFileWriter,
                       nb_chunks: u64,
                       url: &str,
                       authorization_header_factory: Option<AuthorizationHeaderFactory>) {

    // let mut downloaded_chunks: Arc<Mutex<Chunks>> =
    //     Arc::new(Mutex::new(Chunks::with_capacity(nb_chunks as usize)));
    let global_chunk_length: u64 = (content_length / nb_chunks) + 1;
    let mut jobs = vec![];

    let mut mpb = MultiBar::new();
    mpb.println(&format!("Downloading {} chunks: ", nb_chunks));

    for chunk_index in 0..nb_chunks {

        let (mut http_header, RangeBytes(chunk_offset, chunk_length)) =
            get_header_from_index(chunk_index, content_length, global_chunk_length).unwrap();
        let ssl = NativeTlsClient::new().unwrap();
        let connector = HttpsConnector::new(ssl);
        let hyper_client = Client::with_connector(connector);
        let url_clone = String::from(url);
        if let Some(auth_header_factory) = authorization_header_factory.clone() {
            http_header.set(auth_header_factory.build_header());
        }

        // Progress bar customization
        let mut mp = mpb.create_bar(chunk_length);
        mp.tick_format("▏▎▍▌▋▊▉██▉▊▋▌▍▎▏");
        mp.format("|#--|");
        mp.show_tick = true;
        mp.show_speed = true;
        mp.show_percent = true;
        mp.show_counter = false;
        mp.show_time_left = true;
        mp.set_units(Units::Bytes);
        mp.message(&format!("Chunk {} ", chunk_index));


        let chunk_writer = out_file.get_chunk_writer(chunk_offset);
        jobs.push(thread::spawn(move || match download_a_chunk(&hyper_client,
                                                                    http_header,
                                                                    chunk_writer,
                                                                    &url_clone,
                                                                    &mut mp) {
            Ok(bytes_written) => {
                if bytes_written > 0 {
                    mp.finish();
                } else {
                    panic!("The downloaded chunk {} is empty", chunk_index);
                }
            }
            Err(error) => {
                panic!("Cannot download the chunk {}, due to error {}",
                       chunk_index,
                       error);
            }
        }));
    }

    mpb.listen();

    for child in jobs {
        let _ = child.join();
    }

}
/// Function to download each chunk of a remote content (given by its URL).
/// This function takes as parameters:
/// * the remote content length,
/// * a mutable reference to share between threads, which contains each chunk,
/// * the number of chunks that contains the remote content,
/// * the URL of the remote content server.
Function to download each chunk of a remote content (given by its URL).
This function takes as parameters:
the remote content length,
a mutable reference to share between threads, which contains each chunk,
the number of chunks that contains the remote content,
the URL of the remote content server.
Function to download each chunk of a remote content ( given by its URL ) . This function takes as parameters : the remote content length a mutable reference to share between threads which contains each chunk the number of chunks that contains the remote content the URL of the remote content server .
pub fn download_chunks(content_length: u64,
                       mut out_file: OutputFileWriter,
                       nb_chunks: u64,
                       url: &str,
                       authorization_header_factory: Option<AuthorizationHeaderFactory>) {
    // let mut downloaded_chunks: Arc<Mutex<Chunks>> =
    //     Arc::new(Mutex::new(Chunks::with_capacity(nb_chunks as usize)));
    let global_chunk_length: u64 = (content_length / nb_chunks) + 1;
    let mut jobs = vec![];
    let mut mpb = MultiBar::new();
    mpb.println(&format!("Downloading {} chunks: ", nb_chunks));
    for chunk_index in 0..nb_chunks {
        let (mut http_header, RangeBytes(chunk_offset, chunk_length)) =
            get_header_from_index(chunk_index, content_length, global_chunk_length).unwrap();
        let ssl = NativeTlsClient::new().unwrap();
        let connector = HttpsConnector::new(ssl);
        let hyper_client = Client::with_connector(connector);
        let url_clone = String::from(url);
        if let Some(auth_header_factory) = authorization_header_factory.clone() {
            http_header.set(auth_header_factory.build_header());
        }
        // Progress bar customization
        let mut mp = mpb.create_bar(chunk_length);
        mp.tick_format("▏▎▍▌▋▊▉██▉▊▋▌▍▎▏");
        mp.format("|#--|");
        mp.show_tick = true;
        mp.show_speed = true;
        mp.show_percent = true;
        mp.show_counter = false;
        mp.show_time_left = true;
        mp.set_units(Units::Bytes);
        mp.message(&format!("Chunk {} ", chunk_index));
        let chunk_writer = out_file.get_chunk_writer(chunk_offset);
        jobs.push(thread::spawn(move || match download_a_chunk(&hyper_client,
                                                                    http_header,
                                                                    chunk_writer,
                                                                    &url_clone,
                                                                    &mut mp) {
            Ok(bytes_written) => {
                if bytes_written > 0 {
                    mp.finish();
                } else {
                    panic!("The downloaded chunk {} is empty", chunk_index);
                }
            }
            Err(error) => {
                panic!("Cannot download the chunk {}, due to error {}",
                       chunk_index,
                       error);
            }
        }));
    }
    mpb.listen();
    for child in jobs {
        let _ = child.join();
    }
}
pub fn download_chunks ( content_length : u64 , mut out_file : OutputFileWriter , nb_chunks : u64 , url : & str , authorization_header_factory : Option < AuthorizationHeaderFactory > ) { // let mut downloaded_chunks: Arc<Mutex<Chunks>> = //     Arc::new(Mutex::new(Chunks::with_capacity(nb_chunks as usize))); let global_chunk_length : u64 = ( content_length / nb_chunks ) + 1 ; let mut jobs = vec ! [ ] ; let mut mpb = MultiBar :: new ( ) ; mpb . println ( & format ! ( " " nb_chunks ) ) ; for chunk_index in 0 .. nb_chunks { let ( mut http_header , RangeBytes ( chunk_offset , chunk_length ) ) = get_header_from_index ( chunk_index , content_length , global_chunk_length ) . unwrap ( ) ; let ssl = NativeTlsClient :: new ( ) . unwrap ( ) ; let connector = HttpsConnector :: new ( ssl ) ; let hyper_client = Client :: with_connector ( connector ) ; let url_clone = String :: from ( url ) ; if let Some ( auth_header_factory ) = authorization_header_factory . clone ( ) { http_header . set ( auth_header_factory . build_header ( ) ) ; } // Progress bar customization let mut mp = mpb . create_bar ( chunk_length ) ; mp . tick_format ( " mp . format ( " " ) ; mp . show_tick = true ; mp . show_speed = true ; mp . show_percent = true ; mp . show_counter = false ; mp . show_time_left = true ; mp . set_units ( Units :: Bytes ) ; mp . message ( & format ! ( " " chunk_index ) ) ; let chunk_writer = out_file . get_chunk_writer ( chunk_offset ) ; jobs . push ( thread :: spawn ( move | | match download_a_chunk ( & hyper_client , http_header , chunk_writer , & url_clone , & mut mp ) { Ok ( bytes_written ) => { if bytes_written > 0 { mp . finish ( ) ; } else { panic ! ( " " chunk_index ) ; } } Err ( error ) => { panic ! ( " " chunk_index error ) ; } } ) ) ; } mpb . listen ( ) ; for child in jobs { let _ = child . join ( ) ; } }
Function to download each chunk of a remote content (given by its URL).
Function to download each chunk of a remote content ( given by its URL ) .
content_length
u64
out_file
OutputFileWriter
nb_chunks
u64
url
&str
authorization_header_factory
Option<AuthorizationHeaderFactory>
content_length
u64
out_file
OutputFileWriter
nb_chunks
u64
url
&str
authorization_header_factory
Option<AuthorizationHeaderFactory>