partialzip 6.0.0

Download single files from online zip archives or list the content
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
use chrono::NaiveDate;
use chrono::NaiveDateTime;
use chrono::NaiveTime;
use conv::{NoError, ValueFrom};
use curl::easy::Easy;
use num_traits::ToPrimitive;
use serde::Deserialize;
use serde::Serialize;
use std::cell::RefCell;
use std::fs::File;
use std::io;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::ErrorKind;
use std::path::Path;
use std::time::Duration;
use thiserror::Error;
use zip::result::ZipError;

use super::utils;

use zip::ZipArchive;

/// Enum for errors thrown by the partialzip crate
#[derive(Error, Debug)]
pub enum PartialZipError {
    /// The URL is invalid
    #[error("Invalid URL")]
    InvalidUrl,
    /// The file is not found
    #[error("File Not Found")]
    FileNotFound,
    /// Range request not supported
    #[error("Range request not supported")]
    RangeNotSupported,
    /// The compression scheme is currently not supported
    #[error("{0} is a Unsupported Compression")]
    UnsupportedCompression(u16),
    /// Error for the underlying zip crate
    #[error("zip error: {0}")]
    ZipRsError(#[from] ZipError),
    /// `std::io::Error` wrapper
    #[error("io error: {0}")]
    IOError(#[from] io::Error),
    /// Error for CURL
    #[error("CURL error: {0}")]
    CURLError(#[from] curl::Error),
    /// `NoError` error
    #[error("NoError error: {0}")]
    NoError(#[from] NoError),
    /// Conversion Error
    #[error("Conversion error: {0}")]
    ConvError(#[from] conv::PosOverflow<u64>),
}

/// Default maximum number of HTTP redirects to follow
pub const DEFAULT_MAX_REDIRECTS: u32 = 10;

/// Default connection timeout in seconds
pub const DEFAULT_CONNECT_TIMEOUT_SECS: u64 = 30;

/// Default TCP keep-alive idle time in seconds
pub const DEFAULT_TCP_KEEPIDLE_SECS: u64 = 120;

/// Default TCP keep-alive interval in seconds
pub const DEFAULT_TCP_KEEPINTVL_SECS: u64 = 60;

/// Options for configuring [`PartialZip`] and [`PartialReader`] behavior
#[derive(Debug, Clone)]
pub struct PartialZipOptions {
    /// Whether to verify that the server supports HTTP Range requests
    pub check_range: bool,
    /// Maximum number of HTTP redirects to follow (prevents redirect loops and SSRF attacks)
    pub max_redirects: u32,
    /// Connection timeout (None = no timeout)
    pub connect_timeout: Option<Duration>,
    /// TCP keep-alive idle time before sending probes
    pub tcp_keepidle: Duration,
    /// TCP keep-alive interval between probes
    pub tcp_keepintvl: Duration,
    /// Basic authentication credentials (username, password)
    pub basic_auth: Option<(String, String)>,
    /// Proxy URL (e.g., `http://proxy:8080`, `socks5://proxy:1080`)
    pub proxy: Option<String>,
    /// Proxy authentication credentials (username, password)
    pub proxy_auth: Option<(String, String)>,
}

impl Default for PartialZipOptions {
    fn default() -> Self {
        Self {
            check_range: false,
            max_redirects: DEFAULT_MAX_REDIRECTS,
            connect_timeout: Some(Duration::from_secs(DEFAULT_CONNECT_TIMEOUT_SECS)),
            tcp_keepidle: Duration::from_secs(DEFAULT_TCP_KEEPIDLE_SECS),
            tcp_keepintvl: Duration::from_secs(DEFAULT_TCP_KEEPINTVL_SECS),
            basic_auth: None,
            proxy: None,
            proxy_auth: None,
        }
    }
}

impl PartialZipOptions {
    /// Create new options with default values
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set whether to check for range request support
    #[must_use]
    pub const fn check_range(mut self, check: bool) -> Self {
        self.check_range = check;
        self
    }

    /// Set the maximum number of redirects to follow
    #[must_use]
    pub const fn max_redirects(mut self, max: u32) -> Self {
        self.max_redirects = max;
        self
    }

    /// Set the connection timeout (None = no timeout)
    #[must_use]
    pub const fn connect_timeout(mut self, timeout: Option<Duration>) -> Self {
        self.connect_timeout = timeout;
        self
    }

    /// Set the TCP keep-alive idle time before sending probes
    #[must_use]
    pub const fn tcp_keepidle(mut self, duration: Duration) -> Self {
        self.tcp_keepidle = duration;
        self
    }

    /// Set the TCP keep-alive interval between probes
    #[must_use]
    pub const fn tcp_keepintvl(mut self, duration: Duration) -> Self {
        self.tcp_keepintvl = duration;
        self
    }

    /// Set basic authentication credentials
    #[must_use]
    pub fn basic_auth(mut self, username: &str, password: &str) -> Self {
        self.basic_auth = Some((username.to_string(), password.to_string()));
        self
    }

    /// Set proxy URL (e.g., `http://proxy:8080`, `socks5://proxy:1080`)
    #[must_use]
    pub fn proxy(mut self, url: &str) -> Self {
        self.proxy = Some(url.to_string());
        self
    }

    /// Set proxy authentication credentials
    #[must_use]
    pub fn proxy_auth(mut self, username: &str, password: &str) -> Self {
        self.proxy_auth = Some((username.to_string(), password.to_string()));
        self
    }
}

/// Core struct of the crate representing a zip file we want to access partially
#[derive(Debug)]
pub struct PartialZip {
    /// URL of the zip archive
    url: String,
    /// The archive object
    archive: RefCell<ZipArchive<BufReader<PartialReader>>>,
    /// The archive size
    file_size: u64,
}

/// Compression methods for the files inside the archive. Redefined structure to make it serializable.
/// Maps directly to the zip crate `zip::CompressionMethod` enum.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum PartialZipCompressionMethod {
    /// Stored (no compression)
    Stored,
    /// Deflated compression
    Deflated,
    /// bzip2 compression
    Bzip2,
    /// zstd compression
    Zstd,
    /// unsupported compression
    Unsupported,
}

impl From<zip::CompressionMethod> for PartialZipCompressionMethod {
    fn from(value: zip::CompressionMethod) -> Self {
        match value {
            zip::CompressionMethod::Stored => Self::Stored,
            zip::CompressionMethod::Deflated => Self::Deflated,
            zip::CompressionMethod::Bzip2 => Self::Bzip2,
            zip::CompressionMethod::Zstd => Self::Zstd,
            _ => Self::Unsupported,
        }
    }
}

/// Struct for a file in the zip file with some attributes
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct PartialZipFileDetailed {
    /// Filename
    pub name: String,
    /// Compressed size of the file
    pub compressed_size: u64,
    /// How it has been compressed (compression method, like bzip2, deflate, etc.)
    pub compression_method: PartialZipCompressionMethod,
    /// Is the compression supported or not by this crate?
    pub supported: bool,
    /// The date the file was last modified
    pub last_modified: Option<NaiveDateTime>,
}

impl PartialZip {
    /// Create a new [`PartialZip`] with default options
    /// # Errors
    ///
    /// Will return a [`PartialZipError`] enum depending on what error happened
    pub fn new(url: &str) -> Result<Self, PartialZipError> {
        Self::new_with_options(url, &PartialZipOptions::default())
    }

    /// Create a new [`PartialZip`] with range checking option
    /// # Errors
    ///
    /// Will return a [`PartialZipError`] enum depending on what error happened
    pub fn new_check_range(url: &str, check_range: bool) -> Result<Self, PartialZipError> {
        Self::new_with_options(url, &PartialZipOptions::default().check_range(check_range))
    }

    /// Create a new [`PartialZip`] with custom options
    /// # Errors
    ///
    /// Will return a [`PartialZipError`] enum depending on what error happened
    pub fn new_with_options(
        url: &str,
        options: &PartialZipOptions,
    ) -> Result<Self, PartialZipError> {
        let reader = PartialReader::new_with_options(url, options)?;
        let file_size = reader.file_size;
        // higher capacity BufReader has better performances
        let bufreader = BufReader::with_capacity(0x0010_0000, reader);
        let archive = ZipArchive::new(bufreader)?;
        Ok(Self {
            url: url.to_owned(),
            archive: RefCell::new(archive),
            file_size,
        })
    }

    /// Returns the url for the [`PartialZip`]
    #[must_use]
    pub fn url(&self) -> &str {
        &self.url
    }

    /// Returns the zip size for the entire archive of the [`PartialZip`]
    pub const fn file_size(&self) -> u64 {
        self.file_size
    }

    /// Get a list of the filenames in the archive
    pub fn list_names(&self) -> Vec<String> {
        self.archive
            .borrow()
            .file_names()
            .map(std::borrow::ToOwned::to_owned)
            .collect()
    }

    /// Get a list of the files in the archive with details (much slower than just listing names because it fetches much more data around with more requests)
    pub fn list_detailed(&self) -> Vec<PartialZipFileDetailed> {
        let mut file_list = Vec::new();
        let num_files = self.archive.borrow().len();
        for i in 0..num_files {
            match self.archive.borrow_mut().by_index(i) {
                Ok(file) => {
                    let compression_method = file.compression();
                    // we only support some compressions
                    let supported = matches!(
                        compression_method,
                        zip::CompressionMethod::Stored
                            | zip::CompressionMethod::Deflated
                            | zip::CompressionMethod::Bzip2
                            | zip::CompressionMethod::Zstd
                    );
                    let (date, time) = file.last_modified().map_or((None, None), |datetime| {
                        (
                            NaiveDate::from_ymd_opt(
                                datetime.year().into(),
                                datetime.month().into(),
                                datetime.day().into(),
                            ),
                            NaiveTime::from_hms_opt(
                                datetime.hour().into(),
                                datetime.minute().into(),
                                datetime.second().into(),
                            ),
                        )
                    });
                    let last_modified = if let (Some(d), Some(t)) = (date, time) {
                        Some(NaiveDateTime::new(d, t))
                    } else {
                        None
                    };
                    let pzf = PartialZipFileDetailed {
                        name: file.name().to_string(),
                        compressed_size: file.compressed_size(),
                        compression_method: compression_method.into(),
                        supported,
                        last_modified,
                    };
                    file_list.push(pzf);
                }
                Err(e) => {
                    // We are unable to get a file, let's try to continue,
                    // and at least return the files we can
                    log::warn!("list: error while matching file by index: {i} - {e}");
                }
            };
        }
        file_list
    }
    /// Download a single file from the archive into memory.
    ///
    /// **Note:** This loads the entire decompressed file into RAM. For large files,
    /// consider using [`download_to_file`](Self::download_to_file) or
    /// [`download_to_write`](Self::download_to_write) which stream directly to disk
    /// without buffering the entire file in memory.
    ///
    /// # Errors
    /// Will return a [`PartialZipError`] depending on what happened
    pub fn download(&self, filename: &str) -> Result<Vec<u8>, PartialZipError> {
        let mut content: Vec<u8> = Vec::new();
        self.download_to_write(filename, &mut content)?;
        Ok(content)
    }

    /// Download a single file from the archive directly to a file path.
    ///
    /// This is the recommended method for large files as it streams the decompressed
    /// content directly to disk without loading the entire file into memory.
    ///
    /// Returns the number of bytes written.
    ///
    /// # Errors
    /// Will return a [`PartialZipError`] depending on what happened
    pub fn download_to_file(
        &self,
        filename: &str,
        output_path: &Path,
    ) -> Result<u64, PartialZipError> {
        let file = File::create(output_path)?;
        let mut writer = BufWriter::new(file);
        self.download_to_write(filename, &mut writer)
    }

    /// Download a single file from the archive and write it to any [`std::io::Write`] implementor.
    ///
    /// This method streams the decompressed content directly to the writer without
    /// buffering the entire file in memory. Use this for large files or when you need
    /// custom output handling.
    ///
    /// Returns the number of bytes written.
    ///
    /// # Example
    /// ```no_run
    /// use partialzip::PartialZip;
    /// use std::fs::File;
    ///
    /// let pz = PartialZip::new(&"https://example.com/archive.zip")?;
    /// let mut file = File::create("output.bin")?;
    /// pz.download_to_write("large_file.bin", &mut file)?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// # Errors
    /// Will return a [`PartialZipError`] depending on what happened
    pub fn download_to_write(
        &self,
        filename: &str,
        writer: &mut dyn std::io::Write,
    ) -> Result<u64, PartialZipError> {
        let mut archive = self.archive.borrow_mut();
        let mut file = archive.by_name(filename)?;
        let bytes_written = io::copy(&mut file, writer)?;
        Ok(bytes_written)
    }

    /// Download a single file from the archive into memory, showing a progress bar.
    ///
    /// **Note:** This loads the entire decompressed file into RAM. For large files,
    /// consider using [`download_to_file_with_progressbar`](Self::download_to_file_with_progressbar)
    /// or [`download_to_write_with_progressbar`](Self::download_to_write_with_progressbar).
    ///
    /// # Errors
    /// Will return a [`PartialZipError`] depending on what happened
    #[cfg(feature = "progressbar")]
    pub fn download_with_progressbar(&self, filename: &str) -> Result<Vec<u8>, PartialZipError> {
        let mut content: Vec<u8> = Vec::new();
        self.download_to_write_with_progressbar(filename, &mut content)?;
        Ok(content)
    }

    /// Download a single file from the archive directly to a file path, showing a progress bar.
    ///
    /// This is the recommended method for large files as it streams the decompressed
    /// content directly to disk without loading the entire file into memory.
    ///
    /// Returns the number of bytes written.
    ///
    /// # Errors
    /// Will return a [`PartialZipError`] depending on what happened
    #[cfg(feature = "progressbar")]
    pub fn download_to_file_with_progressbar(
        &self,
        filename: &str,
        output_path: &Path,
    ) -> Result<u64, PartialZipError> {
        let file = File::create(output_path)?;
        let mut writer = BufWriter::new(file);
        self.download_to_write_with_progressbar(filename, &mut writer)
    }

    /// Download a single file from the archive to any [`std::io::Write`], showing a progress bar.
    ///
    /// This method streams the decompressed content directly to the writer without
    /// buffering the entire file in memory.
    ///
    /// Returns the number of bytes written.
    ///
    /// # Errors
    /// Will return a [`PartialZipError`] depending on what happened
    #[cfg(feature = "progressbar")]
    pub fn download_to_write_with_progressbar(
        &self,
        filename: &str,
        writer: &mut dyn std::io::Write,
    ) -> Result<u64, PartialZipError> {
        use indicatif::ProgressBar;

        let mut archive = self.archive.borrow_mut();
        let file = archive.by_name(filename)?;
        let pb = ProgressBar::new(file.compressed_size());
        let bytes_written = io::copy(&mut pb.wrap_read(file), writer)?;
        Ok(bytes_written)
    }

    /// Download multiple files from the archive into memory.
    ///
    /// This method efficiently reuses the underlying connection for all downloads.
    /// Returns a vector of tuples containing the filename and its content.
    ///
    /// **Note:** All file contents are loaded into RAM. For large files, consider using
    /// [`download_multiple_to_dir`](Self::download_multiple_to_dir) instead.
    ///
    /// # Errors
    /// Will return a [`PartialZipError`] on the first file that fails to download.
    /// Successfully downloaded files before the error are not returned.
    pub fn download_multiple(
        &self,
        filenames: &[&str],
    ) -> Result<Vec<(String, Vec<u8>)>, PartialZipError> {
        filenames
            .iter()
            .map(|filename| {
                let content = self.download(filename)?;
                Ok(((*filename).to_string(), content))
            })
            .collect()
    }

    /// Download multiple files from the archive to a directory, streaming to disk.
    ///
    /// This method efficiently reuses the underlying connection for all downloads
    /// and streams each file directly to disk without loading entire contents into memory.
    ///
    /// Files are saved with their original names from the archive. If a file in the archive
    /// contains path separators, only the final component (filename) is used.
    ///
    /// Returns the total number of bytes written across all files.
    ///
    /// # Errors
    /// Will return a [`PartialZipError`] on the first file that fails to download.
    pub fn download_multiple_to_dir(
        &self,
        filenames: &[&str],
        output_dir: &Path,
    ) -> Result<u64, PartialZipError> {
        let mut total_bytes = 0u64;
        for filename in filenames {
            // Extract just the filename component (handle paths in archive)
            let output_name = Path::new(filename)
                .file_name()
                .unwrap_or_else(|| std::ffi::OsStr::new(filename));
            let output_path = output_dir.join(output_name);
            total_bytes += self.download_to_file(filename, &output_path)?;
        }
        Ok(total_bytes)
    }

    /// Download multiple files from the archive to a directory with progress bars.
    ///
    /// This method efficiently reuses the underlying connection for all downloads
    /// and streams each file directly to disk without loading entire contents into memory.
    ///
    /// Returns the total number of bytes written across all files.
    ///
    /// # Errors
    /// Will return a [`PartialZipError`] on the first file that fails to download.
    #[cfg(feature = "progressbar")]
    pub fn download_multiple_to_dir_with_progressbar(
        &self,
        filenames: &[&str],
        output_dir: &Path,
    ) -> Result<u64, PartialZipError> {
        let mut total_bytes = 0u64;
        for filename in filenames {
            let output_name = Path::new(filename)
                .file_name()
                .unwrap_or_else(|| std::ffi::OsStr::new(filename));
            let output_path = output_dir.join(output_name);
            total_bytes += self.download_to_file_with_progressbar(filename, &output_path)?;
        }
        Ok(total_bytes)
    }
}

/// Reader for the partialzip doing only the partial read instead of downloading everything
#[derive(Debug)]
pub struct PartialReader {
    /// URL at which we read the file
    url: String,
    file_size: u64,
    easy: Easy,
    pos: u64,
}

const HTTP_PARTIAL_CONTENT: u32 = 206;

impl PartialReader {
    /// Creates a new [`PartialReader`] with default options
    ///
    /// # Errors
    /// Will return a [`PartialZipError`] enum depending on what happened
    pub fn new(url: &str) -> Result<Self, PartialZipError> {
        Self::new_with_options(url, &PartialZipOptions::default())
    }

    /// Creates a new [`PartialReader`] with range checking option
    ///
    /// # Errors
    /// Will return a [`PartialZipError`] enum depending on what happened
    pub fn new_check_range(url: &str, check_range: bool) -> Result<Self, PartialZipError> {
        Self::new_with_options(url, &PartialZipOptions::default().check_range(check_range))
    }

    /// Creates a new [`PartialReader`] with custom options
    ///
    /// # Errors
    /// Will return a [`PartialZipError`] enum depending on what happened
    pub fn new_with_options(
        url: &str,
        options: &PartialZipOptions,
    ) -> Result<Self, PartialZipError> {
        if !utils::url_is_valid(url) {
            return Err(PartialZipError::InvalidUrl);
        }

        let mut easy = Easy::new();
        easy.url(url)?;
        easy.follow_location(true)?;
        easy.max_redirections(options.max_redirects)?;
        if let Some(timeout) = options.connect_timeout {
            easy.connect_timeout(timeout)?;
        }
        easy.tcp_keepalive(true)?;
        easy.tcp_keepidle(options.tcp_keepidle)?;
        easy.tcp_keepintvl(options.tcp_keepintvl)?;

        // Authentication
        if let Some((username, password)) = &options.basic_auth {
            easy.username(username)?;
            easy.password(password)?;
        }

        // Proxy configuration
        if let Some(proxy_url) = &options.proxy {
            easy.proxy(proxy_url)?;
        }
        if let Some((username, password)) = &options.proxy_auth {
            easy.proxy_username(username)?;
            easy.proxy_password(password)?;
        }

        easy.nobody(true)?;
        easy.write_function(|data| Ok(data.len()))?;
        easy.perform()?;
        let file_size = easy
            .content_length_download()?
            .to_u64()
            .ok_or_else(|| std::io::Error::new(ErrorKind::InvalidData, "invalid content length"))?;

        if options.check_range {
            // check if range-request is possible by request 1 byte. if 206 Partial Content (HTTP_PARTIAL_CONTENT) is returned, we can make future request.
            easy.range("0-0")?;
            easy.nobody(true)?;
            easy.perform()?;
            let head_size = easy.content_length_download()?.to_u64().ok_or_else(|| {
                std::io::Error::new(ErrorKind::InvalidData, "can not perform range request")
            })?;
            if head_size != 1 {
                return Err(PartialZipError::RangeNotSupported);
            }
            // 206 Partial Content (HTTP_PARTIAL_CONTENT)
            if easy.response_code()? != HTTP_PARTIAL_CONTENT {
                return Err(PartialZipError::RangeNotSupported);
            }
            easy.range("")?;
            easy.nobody(false)?;
        }
        Ok(Self {
            url: url.to_owned(),
            file_size,
            easy,
            pos: 0,
        })
    }

    /// Returns the url for the [`PartialReader`]
    #[must_use]
    pub fn url(&self) -> &str {
        &self.url
    }
}

impl io::Read for PartialReader {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        log::trace!(
            "read self.pos = {:x} self.file_size = {:x}",
            self.pos,
            self.file_size
        );
        if self.pos >= self.file_size {
            return Ok(0);
        }
        // start = current position
        let start = self.pos;
        // end candidate = start + buf.len() - 1;
        let maybe_end = start
            .checked_add(buf.len().to_u64().ok_or_else(|| {
                std::io::Error::new(
                    ErrorKind::InvalidData,
                    format!("The buf len is invalid {}", buf.len()),
                )
            })?)
            .ok_or_else(|| {
                std::io::Error::new(
                    ErrorKind::InvalidData,
                    format!("start + buf.len() overflow {start} {}", buf.len()),
                )
            })?
            .checked_sub(1)
            .ok_or_else(|| {
                std::io::Error::new(
                    ErrorKind::InvalidData,
                    format!("start + buf.len() - 1 underflow {start} {}", buf.len()),
                )
            })?;
        log::trace!("maybe_end = {maybe_end:x}");
        // end = min(end candidate, file_size - 1)
        let end = std::cmp::min(
            maybe_end,
            self.file_size.checked_sub(1).ok_or_else(|| {
                std::io::Error::new(
                    ErrorKind::InvalidData,
                    format!("file_size - 1 underflow {}", self.file_size),
                )
            })?,
        );
        log::trace!("end = {end:x} start = {start:x}");
        // check if the end and start are valid ( end >= start )
        if end < start {
            return Err(std::io::Error::new(
                ErrorKind::InvalidData,
                format!("end < start: {end} < {start}"),
            ));
        }
        let range = format!("{start}-{end}");
        log::trace!("range = {range}");

        self.easy.range(&range)?;
        self.easy.get(true)?;

        let mut content: Vec<u8> = Vec::new();
        {
            let mut transfer = self.easy.transfer();
            transfer.write_function(|data| {
                log::trace!("transfered {:x} bytes", data.len());
                content.extend_from_slice(data);
                Ok(data.len())
            })?;

            transfer.perform()?;
        };

        let n = io::Read::read(&mut content.as_slice(), buf)?;
        // new position = position + read amount;
        self.pos = self
            .pos
            .checked_add(n.to_u64().ok_or_else(|| {
                std::io::Error::new(ErrorKind::InvalidData, format!("invalid read amount {n}"))
            })?)
            .ok_or_else(|| {
                std::io::Error::new(
                    ErrorKind::InvalidData,
                    format!("adding {n} overflows the reader position {}", self.pos),
                )
            })?;
        log::trace!("new self.pos = {:x}", self.pos);
        Ok(n)
    }
}

impl io::Seek for PartialReader {
    fn seek(&mut self, style: io::SeekFrom) -> io::Result<u64> {
        // we can seek both from start, end, or current position
        let (base_pos, offset) = match style {
            io::SeekFrom::Start(n) => {
                self.pos = n;
                return Ok(n);
            }
            io::SeekFrom::End(n) => (self.file_size, n),
            io::SeekFrom::Current(n) => (self.pos, n),
        };
        log::trace!("seek base_pos = {base_pos:x} offset = {offset:x}");
        let new_pos = if offset >= 0 {
            // position = base position + offset
            base_pos.checked_add(
                u64::value_from(offset)
                    .map_err(|e| std::io::Error::new(ErrorKind::InvalidData, e.to_string()))?,
            )
        } else {
            // position = base position - offset
            base_pos.checked_sub(
                u64::value_from(offset.wrapping_neg())
                    .map_err(|e| std::io::Error::new(ErrorKind::InvalidData, e.to_string()))?,
            )
        };
        // check if new position is valid
        match new_pos {
            Some(n) => {
                self.pos = n;
                log::trace!("new self.pos = {n:x}");
                Ok(self.pos)
            }
            None => Err(std::io::Error::new(
                ErrorKind::InvalidInput,
                "invalid seek to a negative or overflowing position",
            )),
        }
    }
}