uls-download 0.2.2

FCC ULS file download and synchronization
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
//! FCC data download client.

use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::Instant;

use chrono::{NaiveDate, Utc};
use reqwest::header::{HeaderMap, CONTENT_LENGTH, ETAG, IF_NONE_MATCH, LAST_MODIFIED, USER_AGENT};
use reqwest::StatusCode;
use tokio::io::AsyncWriteExt;
use tracing::{debug, info, warn};

use crate::catalog::{DataFile, ServiceCatalog};
use crate::config::DownloadConfig;
use crate::error::{DownloadError, Result};
use crate::progress::{no_progress, DownloadProgress, ProgressCallback};

/// HTTP client for downloading FCC ULS data files.
pub struct FccClient {
    http: reqwest::Client,
    config: DownloadConfig,
}

impl FccClient {
    /// Create a new FCC download client.
    pub fn new(config: DownloadConfig) -> Result<Self> {
        // Ensure cache directory exists
        fs::create_dir_all(&config.cache_dir).map_err(|_| DownloadError::CacheDirectoryError {
            path: config.cache_dir.clone(),
        })?;

        let mut headers = HeaderMap::new();
        headers.insert(
            USER_AGENT,
            config
                .user_agent
                .parse()
                .unwrap_or_else(|_| crate::config::DEFAULT_USER_AGENT.parse().unwrap()),
        );

        let http = reqwest::Client::builder()
            .timeout(config.timeout)
            .default_headers(headers)
            .danger_accept_invalid_certs(!config.verify_ssl)
            .build()?;

        Ok(Self { http, config })
    }

    /// Create a client with default configuration.
    pub fn default_client() -> Result<Self> {
        Self::new(DownloadConfig::default())
    }

    /// Get the full URL for a data file.
    fn url(&self, file: &DataFile) -> String {
        format!("{}/{}", self.config.base_url, file.url_path())
    }

    /// Get the cache path for a data file.
    fn cache_path(&self, file: &DataFile) -> PathBuf {
        self.config.cache_dir.join(file.filename())
    }

    /// Download the complete (weekly) license file for a service.
    pub async fn download_complete(&self, service: &str) -> Result<PathBuf> {
        self.download_complete_with_progress(service, no_progress())
            .await
    }

    /// Download the complete license file with progress callback.
    pub async fn download_complete_with_progress(
        &self,
        service: &str,
        progress: ProgressCallback,
    ) -> Result<PathBuf> {
        let file = ServiceCatalog::complete_license(service)?;
        let (path, _) = self.download_file(&file, progress).await?;
        Ok(path)
    }

    /// Download the complete (weekly) application file for a service.
    pub async fn download_applications(&self, service: &str) -> Result<PathBuf> {
        let file = ServiceCatalog::complete_application(service)?;
        let (path, _) = self.download_file(&file, no_progress()).await?;
        Ok(path)
    }

    /// Download all daily license files for a service.
    pub async fn download_all_daily(&self, service: &str) -> Result<Vec<PathBuf>> {
        let files = ServiceCatalog::daily_licenses(service)?;
        let mut paths = Vec::new();

        for file in files {
            match self.download_file(&file, no_progress()).await {
                Ok((path, _)) => paths.push(path),
                Err(DownloadError::NotFound { .. }) => {
                    debug!("Daily file not available: {}", file.filename());
                }
                Err(e) => return Err(e),
            }
        }

        Ok(paths)
    }

    /// Download the daily license file for a specific date.
    pub async fn download_daily_for_date(&self, service: &str, date: NaiveDate) -> Result<PathBuf> {
        let file = ServiceCatalog::daily_license_for_date(service, date)?;
        let (path, _) = self.download_file(&file, no_progress()).await?;
        Ok(path)
    }

    /// Download a specific data file.
    /// Returns the path and whether the file was newly downloaded or from cache.
    pub async fn download_file(
        &self,
        file: &DataFile,
        progress: ProgressCallback,
    ) -> Result<(PathBuf, DownloadResult)> {
        let url = self.url(file);
        let dest_path = self.cache_path(file);

        // Check for existing cached file and get its ETag
        // Only use cached ETag if the data file actually exists
        let cache_exists = dest_path.exists();
        let cached_etag = if cache_exists {
            self.get_cached_etag(file)
        } else {
            None // Ignore stale ETag if data file is missing
        };

        // If we have a cached file with an ETag, do a HEAD request to check if it's still current
        if cache_exists {
            if let Some(ref local_etag) = cached_etag {
                info!("Checking if cached {} is current...", file.filename());

                // HEAD request to get remote ETag without downloading
                let head_response = self.http.head(&url).send().await?;

                if head_response.status().is_success() {
                    let remote_etag = head_response
                        .headers()
                        .get(ETAG)
                        .and_then(|v| v.to_str().ok())
                        .map(|s| s.to_string());

                    if let Some(ref remote) = remote_etag {
                        if remote == local_etag {
                            info!("Cache is current (ETag match): {}", file.filename());
                            return Ok((dest_path, DownloadResult::NotModified));
                        } else {
                            info!("Remote ETag differs, downloading new version");
                        }
                    }
                }
            }
        }

        info!("Downloading {} to {}", url, dest_path.display());

        for attempt in 0..=self.config.max_retries {
            if attempt > 0 {
                warn!("Retry attempt {} for {}", attempt, file.filename());
                tokio::time::sleep(self.config.retry_delay).await;
            }

            match self
                .do_download(&url, &dest_path, cached_etag.as_deref(), progress.clone())
                .await
            {
                Ok(DownloadResult::Downloaded) => {
                    info!("Downloaded: {}", file.filename());
                    return Ok((dest_path, DownloadResult::Downloaded));
                }
                Ok(DownloadResult::NotModified) => {
                    info!("Not modified (using cache): {}", file.filename());
                    return Ok((dest_path, DownloadResult::NotModified));
                }
                Err(DownloadError::NotFound { .. }) => {
                    return Err(DownloadError::NotFound { url });
                }
                Err(e) if attempt == self.config.max_retries => {
                    return Err(e);
                }
                Err(e) => {
                    warn!("Download attempt failed: {}", e);
                }
            }
        }

        unreachable!()
    }

    /// Perform the actual download.
    async fn do_download(
        &self,
        url: &str,
        dest_path: &Path,
        etag: Option<&str>,
        progress: ProgressCallback,
    ) -> Result<DownloadResult> {
        let mut request = self.http.get(url);

        // Add If-None-Match header if we have a cached ETag
        if let Some(etag) = etag {
            request = request.header(IF_NONE_MATCH, etag);
        }

        let response = request.send().await?;
        let status = response.status();

        match status {
            StatusCode::OK => {
                let total_bytes = response
                    .headers()
                    .get(CONTENT_LENGTH)
                    .and_then(|v| v.to_str().ok())
                    .and_then(|s| s.parse().ok());

                let new_etag = response
                    .headers()
                    .get(ETAG)
                    .and_then(|v| v.to_str().ok())
                    .map(|s| s.to_string());

                let last_modified = response
                    .headers()
                    .get(LAST_MODIFIED)
                    .and_then(|v| v.to_str().ok())
                    .map(|s| s.to_string());

                // Create parent directory if needed
                if let Some(parent) = dest_path.parent() {
                    fs::create_dir_all(parent)?;
                }

                // Download with progress tracking
                let mut file = tokio::fs::File::create(dest_path).await?;
                let filename = dest_path
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("unknown")
                    .to_string();

                let mut prog = DownloadProgress::new(&filename, total_bytes);
                let mut stream = response.bytes_stream();
                let start_time = Instant::now();

                use futures_util::StreamExt;
                while let Some(chunk_result) = stream.next().await {
                    let chunk = chunk_result?;
                    file.write_all(&chunk).await?;

                    prog.downloaded_bytes += chunk.len() as u64;

                    // Calculate speed
                    let elapsed = start_time.elapsed().as_secs_f64();
                    if elapsed > 0.0 {
                        prog.speed_bps = (prog.downloaded_bytes as f64 / elapsed) as u64;

                        // Calculate ETA
                        if let Some(total) = total_bytes {
                            let remaining = total.saturating_sub(prog.downloaded_bytes);
                            prog.eta_seconds =
                                Some((remaining as f64 / prog.speed_bps.max(1) as f64) as u64);
                        }
                    }

                    progress(&prog);
                }

                file.flush().await?;

                // Verify download size
                if let Some(expected) = total_bytes {
                    if prog.downloaded_bytes != expected {
                        return Err(DownloadError::IncompleteDownload {
                            expected,
                            actual: prog.downloaded_bytes,
                        });
                    }
                }

                // Save metadata (ETag for future conditional requests)
                if let Some(etag) = new_etag {
                    self.save_etag(dest_path, &etag)?;
                }

                if let Some(modified) = last_modified {
                    debug!("Last-Modified: {}", modified);
                }

                Ok(DownloadResult::Downloaded)
            }
            StatusCode::NOT_MODIFIED => Ok(DownloadResult::NotModified),
            StatusCode::NOT_FOUND => Err(DownloadError::NotFound {
                url: url.to_string(),
            }),
            _ => Err(DownloadError::ServerError {
                status: status.as_u16(),
                url: url.to_string(),
            }),
        }
    }

    /// Check if FCC has updates available for a service.
    pub async fn check_for_updates(&self, service: &str) -> Result<UpdateInfo> {
        let file = ServiceCatalog::complete_license(service)?;
        let url = self.url(&file);

        let response = self.http.head(&url).send().await?;

        if !response.status().is_success() {
            return Err(DownloadError::ServerError {
                status: response.status().as_u16(),
                url,
            });
        }

        let content_length = response
            .headers()
            .get(CONTENT_LENGTH)
            .and_then(|v| v.to_str().ok())
            .and_then(|s| s.parse().ok());

        let last_modified = response
            .headers()
            .get(LAST_MODIFIED)
            .and_then(|v| v.to_str().ok())
            .map(|s| s.to_string());

        let etag = response
            .headers()
            .get(ETAG)
            .and_then(|v| v.to_str().ok())
            .map(|s| s.to_string());

        let cached_etag = self.get_cached_etag(&file);
        let has_update = match (&etag, &cached_etag) {
            (Some(remote), Some(local)) => remote != local,
            _ => true, // Assume update needed if we can't compare
        };

        Ok(UpdateInfo {
            file,
            size_bytes: content_length,
            last_modified,
            etag,
            has_update,
            checked_at: Utc::now(),
        })
    }

    /// Get the cached ETag for a file.
    pub fn get_cached_etag(&self, file: &DataFile) -> Option<String> {
        let etag_path = self
            .config
            .cache_dir
            .join(format!("{}.etag", file.filename()));
        fs::read_to_string(etag_path).ok()
    }

    /// Save the ETag for a downloaded file.
    fn save_etag(&self, file_path: &Path, etag: &str) -> Result<()> {
        let etag_path = file_path.with_extension("zip.etag");
        let mut file = File::create(etag_path)?;
        file.write_all(etag.as_bytes())?;
        Ok(())
    }

    /// Get the modification time of a cached file.
    ///
    /// Returns the file's modification time (which should match the FCC server's
    /// Last-Modified header if we preserved it), or None if the file doesn't exist.
    pub fn get_cached_file_date(&self, file: &DataFile) -> Option<chrono::DateTime<Utc>> {
        let path = self.cache_path(file);
        std::fs::metadata(&path)
            .ok()
            .and_then(|m| m.modified().ok())
            .map(chrono::DateTime::<Utc>::from)
    }
}

/// Result of a download operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DownloadResult {
    /// File was newly downloaded.
    Downloaded,
    /// File was not modified (HTTP 304) - cache is valid.
    NotModified,
}

/// Information about available updates.
#[derive(Debug, Clone)]
pub struct UpdateInfo {
    /// The data file.
    pub file: DataFile,
    /// Size of the file in bytes.
    pub size_bytes: Option<u64>,
    /// Last-Modified header value.
    pub last_modified: Option<String>,
    /// ETag header value.
    pub etag: Option<String>,
    /// Whether an update is available.
    pub has_update: bool,
    /// When this check was performed.
    pub checked_at: chrono::DateTime<Utc>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::catalog::Weekday;
    use tempfile::TempDir;

    #[test]
    fn test_url_generation() {
        let temp_dir = TempDir::new().unwrap();
        let config = DownloadConfig::with_cache_dir(temp_dir.path().to_path_buf());
        let client = FccClient::new(config).unwrap();

        let file = DataFile::complete_license("amat");
        assert!(client.url(&file).ends_with("/complete/l_amat.zip"));

        let daily = DataFile::daily_license("amat", Weekday::Monday);
        assert!(client.url(&daily).ends_with("/daily/l_am_mon.zip"));
    }

    #[test]
    fn test_cache_path() {
        let temp_dir = TempDir::new().unwrap();
        let config = DownloadConfig::with_cache_dir(temp_dir.path().to_path_buf());
        let client = FccClient::new(config).unwrap();

        let file = DataFile::complete_license("amat");
        assert_eq!(client.cache_path(&file), temp_dir.path().join("l_amat.zip"));
    }
}