smugmug-cli 0.5.1

A command-line tool for uploading photos to SmugMug with deduplication
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
//! Adding photos that are already on SmugMug to the target album
//! ("collecting" them) instead of uploading a second copy.
//!
//! The cache maps each uploaded file to its SmugMug image, and the upload
//! workers skip any file it knows. So a file uploaded to album A and then
//! uploaded to album B would be skipped and never appear in B. SmugMug
//! doesn't deduplicate uploads (uploading it again would store a second,
//! separate image), but an image can be collected into any number of
//! albums. Before the workers start, `plan_collects` collects files the
//! cache knows are in another album into the target album, from the cached
//! image URI, in batches of `COLLECT_BATCH` per album.
//!
//! Collected images take room in the album series like uploads do. Files
//! SmugMug refuses (the image was deleted) go on to the upload workers.

use anyhow::Result;
use chrono::Utc;
use colored::Colorize;
use rayon::prelude::*;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;

use crate::api::SmugMugClient;
use crate::api::albums::Album;
use crate::api::images::CollectResult;
use crate::cache::hash_store::{HashStore, UploadedFile};
use crate::uploader::album_series::{AlbumSeries, AlbumSeriesBackend};
use crate::uploader::worker::calculate_file_hash;

/// Image URIs per `!collectimages` request.
pub(crate) const COLLECT_BATCH: usize = 100;

/// How collects reach SmugMug.
// Only implemented and used inside this crate with concrete types, so the
// Send-bound caveat of async fns in public traits doesn't matter here.
#[allow(async_fn_in_trait)]
pub trait CollectBackend {
    async fn collect(&self, album_key: &str, uris: &[String]) -> Result<CollectResult>;
}

impl CollectBackend for SmugMugClient {
    async fn collect(&self, album_key: &str, uris: &[String]) -> Result<CollectResult> {
        self.collect_images(album_key, uris).await
    }
}

/// What the target albums already hold, as listed before the upload.
pub struct TargetContents<'a> {
    /// Keys of the series' albums.
    pub album_keys: HashSet<String>,
    /// File names of images in them.
    pub file_names: HashSet<&'a str>,
}

#[derive(Debug, Default)]
pub struct CollectOutcome {
    /// Files left for the upload workers.
    pub to_upload: Vec<PathBuf>,
    /// SHA-256 of files the planner hashed, so the workers needn't again.
    pub hashes: HashMap<PathBuf, String>,
    /// Files collected (on a dry run: that would be).
    pub collected: usize,
    /// Files that couldn't be collected because a request failed. They're
    /// neither collected nor uploaded (that would duplicate them); their
    /// cache entries stay, so the next run tries again.
    pub failed: usize,
}

/// A file to collect and the image it's already on SmugMug as.
pub(crate) struct Pending {
    pub(crate) path: PathBuf,
    pub(crate) hash: String,
    pub(crate) file_size: u64,
    pub(crate) image_uri: String,
    pub(crate) image_key: String,
}

/// Collect the files in `files` that the cache knows are in another album
/// into the series' albums, and return the rest for uploading.
pub async fn plan_collects<C: CollectBackend, S: AlbumSeriesBackend>(
    files: Vec<PathBuf>,
    target: &TargetContents<'_>,
    store: &HashStore,
    dry_run: bool,
    backend: &C,
    series: &AlbumSeries<S>,
) -> Result<CollectOutcome> {
    let mut outcome = CollectOutcome::default();

    // Hash and look up every file, in parallel and without requests.
    let classified: Vec<(PathBuf, Option<String>, Option<Pending>)> = files
        .into_par_iter()
        .map(|path| {
            let (hash, pending) = classify(&path, target, store);
            (path, hash, pending)
        })
        .collect();

    let mut pending = Vec::new();
    for (path, hash, collect) in classified {
        if let Some(hash) = hash {
            outcome.hashes.insert(path.clone(), hash);
        }
        match collect {
            Some(p) => pending.push(p),
            None => outcome.to_upload.push(path),
        }
    }

    if !pending.is_empty() {
        let collect = collect_pending(pending, store, dry_run, backend, series).await?;
        outcome.collected = collect.collected;
        outcome.failed = collect.failed;
        outcome.to_upload.extend(collect.refused);
    }

    Ok(outcome)
}

/// The file's hash, and what to collect if the cache knows the file is in
/// another album. Everything else is left to the worker.
fn classify(
    path: &PathBuf,
    target: &TargetContents<'_>,
    store: &HashStore,
) -> (Option<String>, Option<Pending>) {
    // Same name already in the album: the worker skips or replaces it.
    let file_name = path.file_name().map(|n| n.to_string_lossy());
    if file_name.is_some_and(|n| target.file_names.contains(n.as_ref())) {
        return (None, None);
    }
    // Errors here resurface (and are reported) in the worker.
    let Ok(hash) = calculate_file_hash(path) else {
        return (None, None);
    };
    let Ok(Some(cached)) = store.get(&hash) else {
        return (Some(hash), None);
    };
    // Already in the series: the worker skips it.
    if target.album_keys.contains(&cached.album_key) {
        return (Some(hash), None);
    }
    let pending = Pending {
        path: path.clone(),
        hash: hash.clone(),
        file_size: std::fs::metadata(path).map(|m| m.len()).unwrap_or(0),
        image_uri: cached.smugmug_uri,
        image_key: cached.image_key,
    };
    (Some(hash), Some(pending))
}

/// How collecting went.
#[derive(Default)]
struct Collected {
    collected: usize,
    /// Files SmugMug refused to collect (the image is gone or can't be
    /// collected), to upload instead.
    refused: Vec<PathBuf>,
    /// Files not collected because a request failed.
    failed: usize,
}

/// Claim room for each file in the series and collect them album by album.
async fn collect_pending<C: CollectBackend, S: AlbumSeriesBackend>(
    pending: Vec<Pending>,
    store: &HashStore,
    dry_run: bool,
    backend: &C,
    series: &AlbumSeries<S>,
) -> Result<Collected> {
    // Group by album, keeping the series' order.
    let mut groups: Vec<(Album, Vec<Pending>)> = Vec::new();
    for p in pending {
        let album = series.claim().await?;
        match groups.iter_mut().find(|(a, _)| a.name == album.name) {
            Some((_, members)) => members.push(p),
            None => groups.push((album, vec![p])),
        }
    }

    let total: usize = groups.iter().map(|(_, m)| m.len()).sum();
    if dry_run {
        return Ok(Collected {
            collected: total,
            ..Default::default()
        });
    }

    println!("Adding {} files already on SmugMug to the album...", total);
    let mut outcome = Collected::default();
    for (album, members) in groups {
        let mut members = members.into_iter().peekable();
        while members.peek().is_some() {
            let batch: Vec<Pending> = members.by_ref().take(COLLECT_BATCH).collect();
            let batch = collect_batch(batch, &album, store, backend).await;
            outcome.collected += batch.collected;
            for _ in 0..batch.refused.len() + batch.failed.len() {
                series.release(&album).await;
            }
            outcome
                .refused
                .extend(batch.refused.into_iter().map(|p| p.path));
            outcome.failed += batch.failed.len();
        }
    }

    if !outcome.refused.is_empty() {
        println!(
            "{}",
            format!(
                "âš  SmugMug refused to add {} files (no longer there?); uploading them instead",
                outcome.refused.len()
            )
            .yellow()
        );
    }
    if outcome.failed > 0 {
        println!(
            "{}",
            format!(
                "âš  {} files couldn't be added from SmugMug; run the upload again to retry",
                outcome.failed
            )
            .yellow()
        );
    }
    Ok(outcome)
}

/// How one batch went.
pub(crate) struct BatchOutcome {
    pub(crate) collected: usize,
    pub(crate) refused: Vec<Pending>,
    pub(crate) failed: Vec<Pending>,
}

/// Collect one batch into `album`, recording each collected file in the
/// cache.
pub(crate) async fn collect_batch<C: CollectBackend>(
    batch: Vec<Pending>,
    album: &Album,
    store: &HashStore,
    backend: &C,
) -> BatchOutcome {
    let uris: Vec<String> = batch.iter().map(|p| p.image_uri.clone()).collect();
    let (mut good, refused) = match backend.collect(&album.album_key, &uris).await {
        Ok(result) => batch
            .into_iter()
            .partition::<Vec<Pending>, _>(|p| !result.rejected.contains_key(&p.image_uri)),
        Err(e) => {
            println!("Warning: Couldn't add files to '{}': {}", album.name, e);
            return BatchOutcome {
                collected: 0,
                refused: Vec::new(),
                failed: batch,
            };
        }
    };

    // The cache points at an image that's gone or unusable: forget it, so
    // the worker uploads the file instead of skipping it as a duplicate.
    for p in &refused {
        let _ = store.remove(&p.hash);
    }

    // A refused URI fails the whole request, though SmugMug may have
    // collected the others; collecting them again settles it.
    let mut failed = Vec::new();
    if !refused.is_empty() && !good.is_empty() {
        let uris: Vec<String> = good.iter().map(|p| p.image_uri.clone()).collect();
        match backend.collect(&album.album_key, &uris).await {
            Ok(result) if result.rejected.is_empty() => {}
            outcome => {
                if let Err(e) = outcome {
                    println!("Warning: Couldn't add files to '{}': {}", album.name, e);
                }
                failed.append(&mut good);
            }
        }
    }

    for p in &good {
        let _ = store.insert(
            &p.hash,
            UploadedFile {
                smugmug_uri: p.image_uri.clone(),
                album_key: album.album_key.clone(),
                image_key: p.image_key.clone(),
                uploaded_at: Utc::now(),
                file_size: p.file_size,
                original_path: p.path.to_string_lossy().to_string(),
            },
        );
    }
    BatchOutcome {
        collected: good.len(),
        refused,
        failed,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::uploader::album_series::MAX_ALBUM_IMAGES;
    use std::sync::Mutex as StdMutex;

    /// In-memory SmugMug collects.
    #[derive(Default)]
    struct FakeSmugMug {
        /// URIs `collect` refuses ("Does not exist").
        missing: HashSet<String>,
        fail_collect: bool,
        /// Fail every collect after the first.
        fail_on_retry: bool,
        /// (album key, URIs) per collect call.
        collects: StdMutex<Vec<(String, Vec<String>)>>,
    }

    impl CollectBackend for FakeSmugMug {
        async fn collect(&self, album_key: &str, uris: &[String]) -> Result<CollectResult> {
            self.collects
                .lock()
                .unwrap()
                .push((album_key.to_string(), uris.to_vec()));
            let calls = self.collects.lock().unwrap().len();
            if self.fail_collect || (self.fail_on_retry && calls > 1) {
                anyhow::bail!("503 Service Unavailable");
            }
            let rejected = uris
                .iter()
                .filter(|u| self.missing.contains(*u))
                .map(|u| (u.clone(), vec!["Does not exist".to_string()]))
                .collect();
            Ok(CollectResult { rejected })
        }
    }

    /// Albums of a series: name -> image count.
    struct FakeSeries(StdMutex<HashMap<String, u64>>);

    impl AlbumSeriesBackend for FakeSeries {
        async fn find_album(&self, name: &str) -> Result<Option<Album>> {
            Ok(self
                .0
                .lock()
                .unwrap()
                .contains_key(name)
                .then(|| album(name)))
        }
        async fn image_count(&self, album: &Album) -> Result<u64> {
            Ok(self.0.lock().unwrap()[&album.name])
        }
        async fn create_album(&self, name: &str) -> Result<Album> {
            self.0.lock().unwrap().insert(name.to_string(), 0);
            Ok(album(name))
        }
    }

    fn album(name: &str) -> Album {
        Album {
            album_key: format!("key-{}", name),
            name: name.to_string(),
            url_name: String::new(),
            node_id: String::new(),
            uri: format!("/api/v2/album/key-{}", name),
            web_uri: None,
            uris: None,
            image_count: None,
        }
    }

    async fn series(albums: &[(&str, u64)], capacity: u64) -> AlbumSeries<FakeSeries> {
        let backend = FakeSeries(StdMutex::new(
            albums.iter().map(|(n, c)| (n.to_string(), *c)).collect(),
        ));
        AlbumSeries::load(backend, "Trip", capacity, false)
            .await
            .unwrap()
    }

    fn photo(dir: &std::path::Path, name: &str) -> PathBuf {
        let path = dir.join(name);
        std::fs::write(&path, format!("photo {}", name)).unwrap();
        path
    }

    fn cached(store: &HashStore, path: &std::path::Path, album_key: &str, image_key: &str) {
        store
            .insert(
                &calculate_file_hash(path).unwrap(),
                UploadedFile {
                    smugmug_uri: format!("/api/v2/image/{}-0", image_key),
                    album_key: album_key.to_string(),
                    image_key: image_key.to_string(),
                    uploaded_at: Utc::now(),
                    file_size: 1,
                    original_path: path.to_string_lossy().to_string(),
                },
            )
            .unwrap();
    }

    fn empty_target() -> TargetContents<'static> {
        TargetContents {
            album_keys: HashSet::from(["key-Trip".to_string()]),
            file_names: HashSet::new(),
        }
    }

    struct Setup {
        dir: tempfile::TempDir,
        store: HashStore,
    }

    fn setup() -> Setup {
        let dir = tempfile::tempdir().unwrap();
        let store = HashStore::new(&dir.path().join("cache").to_string_lossy()).unwrap();
        Setup { dir, store }
    }

    #[tokio::test]
    async fn test_cache_hit_elsewhere_is_collected() {
        let s = setup();
        let a = photo(s.dir.path(), "a.jpg");
        let b = photo(s.dir.path(), "b.jpg");
        cached(&s.store, &a, "key-Other", "AAA");
        let smugmug = FakeSmugMug::default();
        let series = series(&[("Trip", 0)], MAX_ALBUM_IMAGES).await;

        let outcome = plan_collects(
            vec![a.clone(), b.clone()],
            &empty_target(),
            &s.store,
            false,
            &smugmug,
            &series,
        )
        .await
        .unwrap();

        assert_eq!(outcome.collected, 1);
        assert_eq!(outcome.to_upload, vec![b.clone()]);
        assert_eq!(
            *smugmug.collects.lock().unwrap(),
            vec![(
                "key-Trip".to_string(),
                vec!["/api/v2/image/AAA-0".to_string()]
            )]
        );
        // The cache now knows the file is in this album.
        let entry = s.store.get(&outcome.hashes[&a]).unwrap().unwrap();
        assert_eq!(entry.album_key, "key-Trip");
        // Both files were hashed for the workers.
        assert!(outcome.hashes.contains_key(&b));
        assert_eq!(series.albums().await[0].claimed, 1);
    }

    #[tokio::test]
    async fn test_cache_hit_in_target_or_same_name_is_left_to_worker() {
        let s = setup();
        let a = photo(s.dir.path(), "a.jpg");
        let b = photo(s.dir.path(), "b.jpg");
        cached(&s.store, &a, "key-Trip", "AAA");
        cached(&s.store, &b, "key-Other", "BBB");
        let smugmug = FakeSmugMug::default();
        let series = series(&[("Trip", 0)], MAX_ALBUM_IMAGES).await;
        let target = TargetContents {
            file_names: HashSet::from(["b.jpg"]),
            ..empty_target()
        };

        let outcome = plan_collects(
            vec![a.clone(), b.clone()],
            &target,
            &s.store,
            false,
            &smugmug,
            &series,
        )
        .await
        .unwrap();

        assert_eq!(outcome.collected, 0);
        assert_eq!(outcome.to_upload.len(), 2);
        assert!(smugmug.collects.lock().unwrap().is_empty());
    }

    #[tokio::test]
    async fn test_stale_cache_entry_is_forgotten_and_uploaded() {
        let s = setup();
        let a = photo(s.dir.path(), "a.jpg");
        let b = photo(s.dir.path(), "b.jpg");
        cached(&s.store, &a, "key-Other", "GONE");
        cached(&s.store, &b, "key-Other", "BBB");
        let smugmug = FakeSmugMug {
            missing: HashSet::from(["/api/v2/image/GONE-0".to_string()]),
            ..Default::default()
        };
        let series = series(&[("Trip", 0)], MAX_ALBUM_IMAGES).await;

        let outcome = plan_collects(
            vec![a.clone(), b.clone()],
            &empty_target(),
            &s.store,
            false,
            &smugmug,
            &series,
        )
        .await
        .unwrap();

        assert_eq!(outcome.collected, 1);
        assert_eq!(outcome.failed, 0);
        assert_eq!(outcome.to_upload, vec![a.clone()]);
        assert!(s.store.get(&outcome.hashes[&a]).unwrap().is_none());
        // The refused file's room went back.
        assert_eq!(series.albums().await[0].claimed, 1);
        // The good one is collected again after the refused batch.
        let collects = smugmug.collects.lock().unwrap();
        assert_eq!(collects.len(), 2);
        assert_eq!(collects[1].1, vec!["/api/v2/image/BBB-0".to_string()]);
    }

    #[tokio::test]
    async fn test_failed_collect_fails_without_uploading_or_forgetting() {
        let s = setup();
        let a = photo(s.dir.path(), "a.jpg");
        cached(&s.store, &a, "key-Other", "AAA");
        let smugmug = FakeSmugMug {
            fail_collect: true,
            ..Default::default()
        };
        let series = series(&[("Trip", 0)], MAX_ALBUM_IMAGES).await;

        let outcome = plan_collects(
            vec![a.clone()],
            &empty_target(),
            &s.store,
            false,
            &smugmug,
            &series,
        )
        .await
        .unwrap();

        // Not uploaded (it's on SmugMug; that would duplicate it) and not
        // forgotten, so the next run tries again; reported as failed.
        assert_eq!(outcome.collected, 0);
        assert_eq!(outcome.failed, 1);
        assert!(outcome.to_upload.is_empty());
        assert!(s.store.get(&outcome.hashes[&a]).unwrap().is_some());
        assert_eq!(series.albums().await[0].claimed, 0);
    }

    #[tokio::test]
    async fn test_failed_retry_after_refusal_counts_as_failed() {
        let s = setup();
        let a = photo(s.dir.path(), "a.jpg");
        let b = photo(s.dir.path(), "b.jpg");
        cached(&s.store, &a, "key-Other", "GONE");
        cached(&s.store, &b, "key-Other", "BBB");
        let smugmug = FakeSmugMug {
            missing: HashSet::from(["/api/v2/image/GONE-0".to_string()]),
            fail_on_retry: true,
            ..Default::default()
        };
        let series = series(&[("Trip", 0)], MAX_ALBUM_IMAGES).await;

        let outcome = plan_collects(
            vec![a.clone(), b.clone()],
            &empty_target(),
            &s.store,
            false,
            &smugmug,
            &series,
        )
        .await
        .unwrap();

        // a was refused: uploaded, and its stale entry forgotten. b's retry
        // failed: kept for the next run.
        assert_eq!(outcome.collected, 0);
        assert_eq!(outcome.to_upload, vec![a.clone()]);
        assert_eq!(outcome.failed, 1);
        assert!(s.store.get(&outcome.hashes[&a]).unwrap().is_none());
        assert!(s.store.get(&outcome.hashes[&b]).unwrap().is_some());
        assert_eq!(series.albums().await[0].claimed, 0);
    }

    #[tokio::test]
    async fn test_collects_roll_over_into_the_next_album() {
        let s = setup();
        let files: Vec<PathBuf> = (0..3)
            .map(|i| {
                let p = photo(s.dir.path(), &format!("{i}.jpg"));
                cached(&s.store, &p, "key-Other", &format!("K{i}"));
                p
            })
            .collect();
        let smugmug = FakeSmugMug::default();
        let series = series(&[("Trip", 4)], 5).await;

        let outcome = plan_collects(files, &empty_target(), &s.store, false, &smugmug, &series)
            .await
            .unwrap();

        assert_eq!(outcome.collected, 3);
        let collects = smugmug.collects.lock().unwrap();
        let per_album: Vec<(String, usize)> =
            collects.iter().map(|(k, u)| (k.clone(), u.len())).collect();
        assert_eq!(
            per_album,
            vec![("key-Trip".to_string(), 1), ("key-Trip (2)".to_string(), 2)]
        );
    }

    #[tokio::test]
    async fn test_dry_run_collects_nothing() {
        let s = setup();
        let a = photo(s.dir.path(), "a.jpg");
        cached(&s.store, &a, "key-Other", "AAA");
        let smugmug = FakeSmugMug::default();
        let series = series(&[("Trip", 0)], MAX_ALBUM_IMAGES).await;

        let outcome = plan_collects(vec![a], &empty_target(), &s.store, true, &smugmug, &series)
            .await
            .unwrap();

        assert_eq!(outcome.collected, 1);
        assert!(outcome.to_upload.is_empty());
        assert!(smugmug.collects.lock().unwrap().is_empty());
    }
}