halldyll-media 0.1.0

Media extraction (images, videos, links) for halldyll scraper
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
//! Async media downloader for halldyll-media
//!
//! Download media files with support for:
//! - Concurrent downloads with rate limiting
//! - SHA256 hashing
//! - Base64 encoding
//! - Progress tracking
//! - Retry logic

use bytes::Bytes;
use futures::stream::{self, StreamExt};
use reqwest::{Client, Response};
use sha2::{Sha256, Digest};
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use tokio::fs;
use tokio::io::AsyncWriteExt;
use tokio::sync::Semaphore;
use url::Url;

use crate::types::{
    DownloadConfig, DownloadResult, MediaError, MediaResult, MediaType,
};

// ============================================================================
// DOWNLOADER
// ============================================================================

/// Media downloader with configurable options
#[derive(Debug, Clone)]
pub struct MediaDownloader {
    client: Client,
    config: DownloadConfig,
    semaphore: Arc<Semaphore>,
}

impl Default for MediaDownloader {
    fn default() -> Self {
        Self::new(DownloadConfig::default())
    }
}

impl MediaDownloader {
    /// Create new downloader with configuration
    pub fn new(config: DownloadConfig) -> Self {
        let client = Client::builder()
            .timeout(Duration::from_secs(config.timeout_secs))
            .user_agent(&config.user_agent)
            .build()
            .unwrap_or_default();
        
        let semaphore = Arc::new(Semaphore::new(config.max_concurrent));
        
        Self { client, config, semaphore }
    }
    
    /// Create with custom HTTP client
    pub fn with_client(client: Client, config: DownloadConfig) -> Self {
        let semaphore = Arc::new(Semaphore::new(config.max_concurrent));
        Self { client, config, semaphore }
    }
    
    /// Download single URL to bytes
    pub async fn download(&self, url: &str) -> MediaResult<DownloadResult> {
        let _permit = self.semaphore.acquire().await
            .map_err(|e| MediaError::Download(e.to_string()))?;
        
        self.download_with_retry(url).await
    }
    
    /// Download with retry logic
    async fn download_with_retry(&self, url: &str) -> MediaResult<DownloadResult> {
        let mut last_error = None;
        
        for attempt in 0..=self.config.max_retries {
            if attempt > 0 {
                let delay = Duration::from_millis(self.config.retry_delay_ms * (1 << (attempt - 1)));
                tokio::time::sleep(delay).await;
            }
            
            match self.do_download(url).await {
                Ok(result) => return Ok(result),
                Err(e) => {
                    last_error = Some(e);
                }
            }
        }
        
        Err(last_error.unwrap_or_else(|| MediaError::Download("Unknown error".to_string())))
    }
    
    /// Perform actual download
    async fn do_download(&self, url: &str) -> MediaResult<DownloadResult> {
        let response = self.client.get(url)
            .send()
            .await
            .map_err(|e| MediaError::Network(e.to_string()))?;
        
        if !response.status().is_success() {
            return Err(MediaError::Http(response.status().as_u16(), response.status().to_string()));
        }
        
        // Extract metadata from response
        let content_type = response.headers()
            .get("content-type")
            .and_then(|v| v.to_str().ok())
            .map(|s| s.split(';').next().unwrap_or(s).to_string());
        
        let content_length = response.headers()
            .get("content-length")
            .and_then(|v| v.to_str().ok())
            .and_then(|s| s.parse().ok());
        
        // Validate size
        if let Some(max_size) = self.config.max_file_size {
            if let Some(size) = content_length {
                if size > max_size {
                    return Err(MediaError::FileTooLarge(size, max_size));
                }
            }
        }
        
        // Download content
        let bytes = self.download_bytes(response).await?;
        
        // Validate actual size
        if let Some(max_size) = self.config.max_file_size {
            if bytes.len() as u64 > max_size {
                return Err(MediaError::FileTooLarge(bytes.len() as u64, max_size));
            }
        }
        
        // Calculate hash
        let hash = compute_sha256(&bytes);
        
        // Detect media type
        let media_type = detect_media_type(&content_type, url);
        
        // Base64 encode if configured
        let base64 = if self.config.encode_base64 {
            use base64::Engine;
            Some(base64::engine::general_purpose::STANDARD.encode(&bytes))
        } else {
            None
        };
        
        Ok(DownloadResult {
            url: url.to_string(),
            bytes,
            content_type,
            size: content_length.unwrap_or(0),
            hash,
            media_type,
            base64,
        })
    }
    
    /// Download response bytes
    async fn download_bytes(&self, response: Response) -> MediaResult<Bytes> {
        response.bytes()
            .await
            .map_err(|e| MediaError::Download(e.to_string()))
    }
    
    /// Download multiple URLs concurrently
    pub async fn download_many(&self, urls: &[String]) -> Vec<MediaResult<DownloadResult>> {
        stream::iter(urls)
            .map(|url| {
                let downloader = self.clone();
                async move {
                    downloader.download(url).await
                }
            })
            .buffer_unordered(self.config.max_concurrent)
            .collect()
            .await
    }
    
    /// Download and save to file
    pub async fn download_to_file(&self, url: &str, path: &Path) -> MediaResult<DownloadResult> {
        let result = self.download(url).await?;
        
        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)
                .await
                .map_err(|e| MediaError::Io(e.to_string()))?;
        }
        
        // Write to file
        let mut file = fs::File::create(path)
            .await
            .map_err(|e| MediaError::Io(e.to_string()))?;
        
        file.write_all(&result.bytes)
            .await
            .map_err(|e| MediaError::Io(e.to_string()))?;
        
        Ok(result)
    }
    
    /// Download many and save to directory
    pub async fn download_many_to_dir(
        &self,
        urls: &[String],
        dir: &Path,
    ) -> Vec<MediaResult<(String, std::path::PathBuf)>> {
        stream::iter(urls)
            .map(|url| {
                let downloader = self.clone();
                let dir = dir.to_path_buf();
                async move {
                    let filename = url_to_filename(url);
                    let path = dir.join(&filename);
                    
                    downloader.download_to_file(url, &path)
                        .await
                        .map(|_| (url.clone(), path))
                }
            })
            .buffer_unordered(self.config.max_concurrent)
            .collect()
            .await
    }
}

// ============================================================================
// HELPER FUNCTIONS
// ============================================================================

/// Compute SHA256 hash of bytes
pub fn compute_sha256(data: &[u8]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(data);
    format!("{:x}", hasher.finalize())
}

/// Detect media type from content type and URL
pub fn detect_media_type(content_type: &Option<String>, url: &str) -> MediaType {
    if let Some(ct) = content_type {
        if ct.starts_with("image/") { return MediaType::Image; }
        if ct.starts_with("video/") { return MediaType::Video; }
        if ct.starts_with("audio/") { return MediaType::Audio; }
        if ct.contains("pdf") { return MediaType::Document; }
        if ct.contains("document") || ct.contains("spreadsheet") || ct.contains("presentation") {
            return MediaType::Document;
        }
    }
    
    // Fallback to URL extension
    let url_lower = url.to_lowercase();
    
    // Images
    if url_lower.ends_with(".jpg") || url_lower.ends_with(".jpeg") ||
       url_lower.ends_with(".png") || url_lower.ends_with(".gif") ||
       url_lower.ends_with(".webp") || url_lower.ends_with(".svg") ||
       url_lower.ends_with(".avif") {
        return MediaType::Image;
    }
    
    // Videos
    if url_lower.ends_with(".mp4") || url_lower.ends_with(".webm") ||
       url_lower.ends_with(".avi") || url_lower.ends_with(".mov") ||
       url_lower.ends_with(".mkv") {
        return MediaType::Video;
    }
    
    // Audio
    if url_lower.ends_with(".mp3") || url_lower.ends_with(".wav") ||
       url_lower.ends_with(".ogg") || url_lower.ends_with(".flac") ||
       url_lower.ends_with(".aac") {
        return MediaType::Audio;
    }
    
    // Documents
    if url_lower.ends_with(".pdf") || url_lower.ends_with(".doc") ||
       url_lower.ends_with(".docx") || url_lower.ends_with(".xls") ||
       url_lower.ends_with(".xlsx") || url_lower.ends_with(".ppt") ||
       url_lower.ends_with(".pptx") {
        return MediaType::Document;
    }
    
    MediaType::Other
}

/// Generate filename from URL
pub fn url_to_filename(url: &str) -> String {
    if let Ok(parsed) = Url::parse(url) {
        let path = parsed.path();
        let filename = path.rsplit('/').next().unwrap_or("download");
        
        if filename.is_empty() || filename == "/" {
            let hash = &compute_sha256(url.as_bytes())[..12];
            return format!("download_{}", hash);
        }
        
        // Sanitize filename
        sanitize_filename(filename)
    } else {
        let hash = &compute_sha256(url.as_bytes())[..12];
        format!("download_{}", hash)
    }
}

/// Sanitize filename for filesystem
fn sanitize_filename(name: &str) -> String {
    let decoded = urlencoding::decode(name).unwrap_or_else(|_| name.into());
    
    decoded.chars()
        .map(|c| {
            if c.is_alphanumeric() || c == '.' || c == '-' || c == '_' {
                c
            } else {
                '_'
            }
        })
        .collect()
}

/// Check if URL is likely downloadable
pub fn is_downloadable(url: &str) -> bool {
    let url_lower = url.to_lowercase();
    
    // Skip data URLs
    if url_lower.starts_with("data:") {
        return false;
    }
    
    // Skip javascript
    if url_lower.starts_with("javascript:") {
        return false;
    }
    
    // Must be http(s)
    url_lower.starts_with("http://") || url_lower.starts_with("https://")
}

/// Estimate file size from URL (heuristic)
pub fn estimate_size_from_url(url: &str) -> Option<u64> {
    // Look for size hints in URL query params
    if let Ok(parsed) = Url::parse(url) {
        for (key, value) in parsed.query_pairs() {
            if key == "size" || key == "s" || key == "bytes" {
                if let Ok(size) = value.parse::<u64>() {
                    return Some(size);
                }
            }
        }
    }
    None
}

// ============================================================================
// CONVENIENCE FUNCTIONS
// ============================================================================

/// Quick download to bytes
pub async fn download_bytes(url: &str) -> MediaResult<Bytes> {
    let downloader = MediaDownloader::default();
    let result = downloader.download(url).await?;
    Ok(result.bytes)
}

/// Quick download with hash
pub async fn download_with_hash(url: &str) -> MediaResult<(Bytes, String)> {
    let downloader = MediaDownloader::default();
    let result = downloader.download(url).await?;
    Ok((result.bytes, result.hash))
}

/// Quick download to base64
pub async fn download_to_base64(url: &str) -> MediaResult<String> {
    let config = DownloadConfig {
        encode_base64: true,
        ..Default::default()
    };
    let downloader = MediaDownloader::new(config);
    let result = downloader.download(url).await?;
    result.base64.ok_or_else(|| MediaError::Download("Base64 encoding failed".to_string()))
}

/// Download and save to file
pub async fn save_to_file(url: &str, path: &Path) -> MediaResult<()> {
    let downloader = MediaDownloader::default();
    downloader.download_to_file(url, path).await?;
    Ok(())
}

// ============================================================================
// TESTS
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compute_sha256() {
        let data = b"Hello, World!";
        let hash = compute_sha256(data);
        assert!(!hash.is_empty());
        assert_eq!(hash.len(), 64); // SHA256 hex is 64 chars
    }

    #[test]
    fn test_detect_media_type_from_content_type() {
        assert_eq!(detect_media_type(&Some("image/png".to_string()), ""), MediaType::Image);
        assert_eq!(detect_media_type(&Some("video/mp4".to_string()), ""), MediaType::Video);
        assert_eq!(detect_media_type(&Some("audio/mpeg".to_string()), ""), MediaType::Audio);
        assert_eq!(detect_media_type(&Some("application/pdf".to_string()), ""), MediaType::Document);
    }

    #[test]
    fn test_detect_media_type_from_url() {
        assert_eq!(detect_media_type(&None, "https://example.com/image.png"), MediaType::Image);
        assert_eq!(detect_media_type(&None, "https://example.com/video.mp4"), MediaType::Video);
        assert_eq!(detect_media_type(&None, "https://example.com/audio.mp3"), MediaType::Audio);
        assert_eq!(detect_media_type(&None, "https://example.com/doc.pdf"), MediaType::Document);
        assert_eq!(detect_media_type(&None, "https://example.com/unknown"), MediaType::Other);
    }

    #[test]
    fn test_url_to_filename() {
        assert_eq!(url_to_filename("https://example.com/images/photo.jpg"), "photo.jpg");
        assert_eq!(url_to_filename("https://example.com/file%20name.pdf"), "file_name.pdf");
        assert!(url_to_filename("https://example.com/").starts_with("download_"));
    }

    #[test]
    fn test_sanitize_filename() {
        assert_eq!(sanitize_filename("file.txt"), "file.txt");
        assert_eq!(sanitize_filename("file name.txt"), "file_name.txt");
        assert_eq!(sanitize_filename("file<>:\"/\\|?*.txt"), "file_________.txt");
    }

    #[test]
    fn test_is_downloadable() {
        assert!(is_downloadable("https://example.com/file.jpg"));
        assert!(is_downloadable("http://example.com/file.pdf"));
        assert!(!is_downloadable("data:image/png;base64,abc"));
        assert!(!is_downloadable("javascript:void(0)"));
        assert!(!is_downloadable("/relative/path"));
    }

    #[test]
    fn test_download_config_default() {
        let config = DownloadConfig::default();
        assert!(config.max_concurrent > 0);
        assert!(config.timeout_secs > 0);
    }

    #[test]
    fn test_downloader_creation() {
        let downloader = MediaDownloader::default();
        assert!(downloader.config.max_concurrent > 0);
    }

    #[test]
    fn test_downloader_with_config() {
        let config = DownloadConfig {
            max_concurrent: 10,
            timeout_secs: 60,
            max_retries: 5,
            ..Default::default()
        };
        let downloader = MediaDownloader::new(config.clone());
        assert_eq!(downloader.config.max_concurrent, 10);
        assert_eq!(downloader.config.timeout_secs, 60);
    }

    #[test]
    fn test_estimate_size_from_url() {
        assert_eq!(estimate_size_from_url("https://example.com/file?size=1024"), Some(1024));
        assert_eq!(estimate_size_from_url("https://example.com/file"), None);
    }

    #[tokio::test]
    async fn test_download_invalid_url() {
        let downloader = MediaDownloader::default();
        let result = downloader.download("not-a-valid-url").await;
        assert!(result.is_err());
    }

    #[test]
    fn test_media_type_detection_priority() {
        // Content-type should take priority over URL
        assert_eq!(
            detect_media_type(&Some("video/mp4".to_string()), "https://example.com/image.png"),
            MediaType::Video
        );
    }
}