smugmug-cli 0.5.0

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
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
//! One pass of a dated upload (see `crate::dated`).
//!
//! For each file, cheapest check first:
//! 1. Same size and modification time as the index records → done, unread.
//! 2. Read it (SHA-256 + MD5 in one pass; a RAW file whole). Same content
//!    as the index records → re-record it (touched or copied), done.
//! 3. Content already on SmugMug (the hash store knows it) → link it if it's
//!    in its day's albums, else collect it there. A RAW file isn't rendered.
//! 4. A RAW file whose rendered JPEG is what this path uploaded last time
//!    (metadata-only edit) → re-record, no upload.
//! 5. Changed content at a path that was uploaded → replace that image,
//!    unless another path shares it (then upload anew).
//! 6. Otherwise upload into the day's album, under its own name, or renamed
//!    if a different photo has that name there.

use anyhow::{Context, Result};
use chrono::{NaiveDate, Utc};
use colored::Colorize;
use futures_util::future::join_all;
use indicatif::{ProgressBar, ProgressStyle};
use md5::Context as Md5Context;
use serde::Serialize;
use sha2::{Digest, Sha256};
use std::collections::{BTreeMap, HashMap, VecDeque};
use std::io::{IsTerminal, Read};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};

use crate::api::SmugMugClient;
use crate::api::upload::{
    FileUpload, UploadPayload, UploadRejected, UploadResult, UploadTarget, upload_bytes,
    upload_file,
};
use crate::cache::hash_store::{HashStore, UploadedFile};
use crate::config::{LivePhotoVideos, RawHandling};
use crate::dated::date::{self, DateSource};
use crate::dated::index::{FileIndex, FileRecord};
use crate::dated::plan::{DayAlbums, NameCheck, Smug};
use crate::dated::scan::{self, ScannedFile};
use crate::uploader::collect::{COLLECT_BATCH, CollectBackend, Pending, collect_batch};

/// Set to stop a run early: no new files are read; files already read are
/// finished.
pub type StopFlag = Arc<AtomicBool>;

pub struct RunOptions {
    pub sources: Vec<PathBuf>,
    pub excludes: Vec<String>,
    /// SmugMug folder path the year folders go in, e.g. "Backup".
    pub root_folder: String,
    /// Privacy for the root folder if it's created (`None`: SmugMug's
    /// default). Year/month folders and day albums are always private.
    pub root_privacy: Option<&'static str>,
    pub upload_threads: usize,
    pub read_threads: usize,
    pub dry_run: bool,
    /// Ignore the index and hash store (still checks file names and MD5s in
    /// the day albums, so nothing is uploaded twice).
    pub no_cache: bool,
    pub raw_handling: RawHandling,
    pub live_photo_videos: LivePhotoVideos,
    pub retry_attempts: u32,
    pub cache_path: PathBuf,
}

/// How a run went. Serialized as the backup's `last_run.json`.
#[derive(Debug, Default, Clone, Serialize)]
pub struct RunStats {
    /// Supported files found (after exclusions and RAW/Live Photo choices).
    pub scanned: usize,
    pub unsupported: usize,
    pub scan_errors: Vec<String>,
    pub raw_rendered: usize,
    pub raw_skipped: usize,
    pub raw_skipped_with_sibling: usize,
    pub live_photo_videos_skipped: usize,
    /// Same size and modification time as last time: not read.
    pub unchanged: usize,
    /// Unchanged files SmugMug refused before (not retried).
    pub previously_refused: usize,
    /// New or changed files (read, or on a dry run, dated).
    pub to_process: usize,
    pub bytes_to_process: u64,
    /// Changed size or time but not content (or not rendered JPEG).
    pub touched: usize,
    pub uploaded: usize,
    pub replaced: usize,
    /// Already in its day album (an identical copy uploaded before).
    pub linked: usize,
    /// Already on SmugMug in another album; added to its day album.
    pub collected: usize,
    pub failed: usize,
    /// Refused by SmugMug (too big, unsupported); not retried until the
    /// file changes.
    pub refused: usize,
    pub bytes_uploaded: u64,
    pub albums_created: usize,
    /// How the processed files were dated.
    pub date_sources: BTreeMap<String, usize>,
    /// Processed files per capture day.
    pub days: BTreeMap<NaiveDate, usize>,
    /// The first few failures.
    pub errors: Vec<String>,
    pub duration_secs: u64,
    /// Stopped early (signal).
    pub interrupted: bool,
}

const MAX_ERRORS: usize = 20;

impl RunStats {
    fn error(&mut self, path: &Path, e: impl std::fmt::Display) {
        if self.errors.len() < MAX_ERRORS {
            self.errors.push(format!("{}: {}", path.display(), e));
        }
    }

    fn dated(&mut self, date: NaiveDate, source: DateSource) {
        *self.days.entry(date).or_default() += 1;
        *self
            .date_sources
            .entry(source.describe().to_string())
            .or_default() += 1;
    }
}

/// What a new upload carries.
pub enum Content {
    /// The file itself, streamed from disk.
    File,
    /// A JPEG rendered from a RAW file.
    Rendered(bytes::Bytes),
}

/// Sending files to 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 Uploads {
    async fn upload(
        &self,
        target: UploadTarget<'_>,
        path: &Path,
        content: &Content,
        size: u64,
        md5: &str,
        name: &str,
    ) -> Result<UploadResult>;

    /// The root folder's node URI, created if `create` (else `None` when
    /// missing).
    async fn root_folder(
        &self,
        path: &str,
        create: bool,
        privacy: Option<&str>,
    ) -> Result<Option<String>>;
}

impl Uploads for SmugMugClient {
    async fn upload(
        &self,
        target: UploadTarget<'_>,
        path: &Path,
        content: &Content,
        size: u64,
        md5: &str,
        name: &str,
    ) -> Result<UploadResult> {
        match content {
            Content::File => {
                upload_file(
                    self,
                    target,
                    &FileUpload {
                        path,
                        size,
                        md5_hex: md5,
                        file_name: name,
                    },
                )
                .await
            }
            Content::Rendered(data) => {
                upload_bytes(
                    self,
                    target,
                    &UploadPayload {
                        data: data.clone(),
                        file_name: name.to_string(),
                        mime_type: "image/jpeg".to_string(),
                    },
                )
                .await
            }
        }
    }

    async fn root_folder(
        &self,
        path: &str,
        create: bool,
        privacy: Option<&str>,
    ) -> Result<Option<String>> {
        if create {
            self.find_or_create_folder_path(path, privacy)
                .await
                .map(Some)
        } else {
            self.find_folder_path(path).await
        }
    }
}

/// A file that isn't known to be unchanged.
struct PendingFile {
    file: ScannedFile,
    previous: Option<FileRecord>,
}

enum Kind {
    /// Same content as the index records for this path.
    Same,
    /// A RAW file whose rendered JPEG (MD5 `md5`) is what this path
    /// uploaded last time.
    SameRender { md5: String },
    /// Content already on SmugMug.
    Known(UploadedFile),
    Upload {
        content: Content,
        name: String,
        md5: String,
        size: u64,
    },
}

/// A file read and classified, ready for the upload stage.
struct Prepared {
    file: ScannedFile,
    previous: Option<FileRecord>,
    sha256: String,
    date: NaiveDate,
    date_source: DateSource,
    kind: Kind,
}

impl Prepared {
    fn record(
        &self,
        uploaded_md5: Option<String>,
        image_uri: Option<String>,
        album_key: Option<String>,
        failed: Option<String>,
    ) -> FileRecord {
        FileRecord {
            size: self.file.size,
            mtime_ns: self.file.mtime_ns,
            sha256: self.sha256.clone(),
            uploaded_md5,
            capture_date: self.date,
            date_source: self.date_source,
            image_uri,
            album_key,
            failed,
        }
    }

    fn uploaded_file(&self, image_uri: &str, album_key: &str, size: u64) -> UploadedFile {
        UploadedFile {
            smugmug_uri: image_uri.to_string(),
            album_key: album_key.to_string(),
            image_key: image_uri.rsplit('/').next().unwrap_or("").to_string(),
            uploaded_at: Utc::now(),
            file_size: size,
            original_path: self.file.path.to_string_lossy().to_string(),
        }
    }
}

/// SHA-256 and MD5 of a file in one pass, and its length.
fn hash_file(path: &Path) -> std::io::Result<(String, String, u64)> {
    let mut file = std::fs::File::open(path)?;
    let mut sha = Sha256::new();
    let mut md5 = Md5Context::new();
    let mut buffer = vec![0u8; 1024 * 1024];
    let mut len = 0u64;
    loop {
        let n = file.read(&mut buffer)?;
        if n == 0 {
            break;
        }
        sha.update(&buffer[..n]);
        md5.consume(&buffer[..n]);
        len += n as u64;
    }
    Ok((
        hex::encode(sha.finalize()),
        format!("{:x}", md5.finalize()),
        len,
    ))
}

/// The day a file goes under: a Live Photo video's photo's, else its own.
fn date_for(file: &ScannedFile, data: Option<&[u8]>, index: &FileIndex) -> (NaiveDate, DateSource) {
    if let Some(photo) = &file.live_photo_of {
        if let Ok(Some(record)) = index.get(photo) {
            return (record.capture_date, DateSource::LivePhoto);
        }
        if let Some(date) = date::metadata_date(photo) {
            return (date, DateSource::LivePhoto);
        }
    }
    date::capture_date(&file.path, data, file.mtime_ns)
}

/// What readers need.
#[derive(Clone)]
struct ReadContext {
    store: HashStore,
    index: FileIndex,
    render_raw: bool,
    no_cache: bool,
}

/// Read one file and decide what it needs (steps 2–4 in the module docs).
fn prepare(pending: PendingFile, ctx: &ReadContext) -> Result<Prepared> {
    let PendingFile { file, previous } = pending;
    let same = |sha: &str| {
        previous
            .as_ref()
            .is_some_and(|r| r.sha256 == sha && r.is_settled())
    };
    let known = |sha: &str| -> Result<Option<UploadedFile>> {
        if ctx.no_cache {
            Ok(None)
        } else {
            ctx.store.get(sha)
        }
    };

    if ctx.render_raw && crate::scanner::is_raw_file(&file.path) {
        let data = std::fs::read(&file.path).context("Failed to read file")?;
        let sha256 = hex::encode(Sha256::digest(&data));
        let (date, date_source) = date_for(&file, Some(&data), &ctx.index);
        let kind = if same(&sha256) {
            Kind::Same
        } else if let Some(cached) = known(&sha256)? {
            Kind::Known(cached)
        } else {
            let rendered = crate::raw::render_jpeg_bytes(&data)
                .context("Failed to render RAW file to JPEG")?
                .data;
            let md5 = format!("{:x}", md5::compute(&rendered));
            let same_render = previous.as_ref().is_some_and(|r| {
                r.image_uri.is_some() && r.uploaded_md5.as_deref() == Some(md5.as_str())
            });
            if same_render {
                Kind::SameRender { md5 }
            } else {
                Kind::Upload {
                    size: rendered.len() as u64,
                    content: Content::Rendered(rendered.into()),
                    name: crate::raw::rendered_file_name(&file.path),
                    md5,
                }
            }
        };
        return Ok(Prepared {
            file,
            previous,
            sha256,
            date,
            date_source,
            kind,
        });
    }

    let (sha256, md5, size) = hash_file(&file.path).context("Failed to read file")?;
    let (date, date_source) = date_for(&file, None, &ctx.index);
    let kind = if same(&sha256) {
        Kind::Same
    } else if let Some(cached) = known(&sha256)? {
        Kind::Known(cached)
    } else {
        Kind::Upload {
            content: Content::File,
            name: file
                .path
                .file_name()
                .map(|n| n.to_string_lossy().into_owned())
                .unwrap_or_else(|| "image".to_string()),
            md5,
            size,
        }
    };
    Ok(Prepared {
        file,
        previous,
        sha256,
        date,
        date_source,
        kind,
    })
}

/// Shared by the upload workers.
struct Workers<'a, T> {
    smug: &'a T,
    days: DayAlbums<T>,
    store: HashStore,
    index: FileIndex,
    options: &'a RunOptions,
    stats: std::sync::Mutex<RunStats>,
    /// Content (SHA-256) being uploaded → files with the same content
    /// waiting for it, so identical copies read at the same time are
    /// uploaded once.
    in_flight: std::sync::Mutex<HashMap<String, Vec<Prepared>>>,
    /// Files to collect into each day's albums, sent in batches.
    collects: tokio::sync::Mutex<HashMap<NaiveDate, Vec<(Prepared, UploadedFile)>>>,
    progress: ProgressBar,
}

impl<T: Smug + Uploads + CollectBackend> Workers<'_, T> {
    fn stats(&self) -> std::sync::MutexGuard<'_, RunStats> {
        self.stats.lock().unwrap()
    }

    fn put(&self, prepared: &Prepared, record: &FileRecord) {
        if let Err(e) = self.index.put(&prepared.file.path, record) {
            self.stats().error(&prepared.file.path, e);
        }
    }

    async fn handle(&self, prepared: Prepared) {
        self.stats().dated(prepared.date, prepared.date_source);
        match &prepared.kind {
            Kind::Same => {
                let mut record = prepared.previous.clone().expect("Same needs a record");
                record.size = prepared.file.size;
                record.mtime_ns = prepared.file.mtime_ns;
                self.put(&prepared, &record);
                self.stats().touched += 1;
            }
            Kind::SameRender { md5 } => {
                let previous = prepared
                    .previous
                    .as_ref()
                    .expect("SameRender needs a record");
                let uri = previous.image_uri.clone().unwrap_or_default();
                let album_key = previous.album_key.clone().unwrap_or_default();
                let size = prepared.file.size;
                let _ = self.store.insert_deferred(
                    &prepared.sha256,
                    &prepared.uploaded_file(&uri, &album_key, size),
                );
                let record = prepared.record(
                    Some(md5.clone()),
                    previous.image_uri.clone(),
                    previous.album_key.clone(),
                    None,
                );
                self.put(&prepared, &record);
                self.stats().touched += 1;
            }
            Kind::Known(cached) => {
                let cached = cached.clone();
                self.place_known(prepared, cached).await;
            }
            Kind::Upload { .. } => self.upload(prepared).await,
        }
        self.progress.inc(1);
    }

    /// A file whose content is already on SmugMug as `cached`: link it if
    /// that's in its day's albums, otherwise collect it there.
    async fn place_known(&self, prepared: Prepared, cached: UploadedFile) {
        let day = match self.days.day(prepared.date).await {
            Ok(day) => day,
            Err(e) => return self.fail_with(&prepared, &e),
        };
        if day.album_keys().await.contains(&cached.album_key) {
            let record = prepared.record(
                None,
                Some(cached.smugmug_uri.clone()),
                Some(cached.album_key.clone()),
                None,
            );
            self.put(&prepared, &record);
            self.stats().linked += 1;
            return;
        }
        let batch = {
            let mut collects = self.collects.lock().await;
            let pending = collects.entry(prepared.date).or_default();
            pending.push((prepared, cached));
            if pending.len() >= COLLECT_BATCH {
                std::mem::take(pending)
            } else {
                Vec::new()
            }
        };
        if !batch.is_empty() {
            self.collect(batch).await;
        }
    }

    /// Collect files (all of one day) into that day's albums.
    async fn collect(&self, items: Vec<(Prepared, UploadedFile)>) {
        let Some(date) = items.first().map(|(p, _)| p.date) else {
            return;
        };
        let day = match self.days.day(date).await {
            Ok(day) => day,
            Err(e) => {
                for (p, _) in &items {
                    self.fail_with(p, &e);
                }
                return;
            }
        };

        // Claim room album by album, like uploads do.
        let mut groups: Vec<(crate::api::albums::Album, Vec<(Prepared, UploadedFile)>)> =
            Vec::new();
        for (prepared, cached) in items {
            let album = match day.series.claim().await {
                Ok(album) => album,
                Err(e) => {
                    self.fail_with(&prepared, &e);
                    continue;
                }
            };
            match groups.iter_mut().find(|(a, _)| a.name == album.name) {
                Some((_, members)) => members.push((prepared, cached)),
                None => groups.push((album, vec![(prepared, cached)])),
            }
        }

        for (album, members) in groups {
            let pending: Vec<Pending> = members
                .iter()
                .map(|(p, cached)| Pending {
                    path: p.file.path.clone(),
                    hash: p.sha256.clone(),
                    file_size: p.file.size,
                    image_uri: cached.smugmug_uri.clone(),
                    image_key: cached.image_key.clone(),
                })
                .collect();
            let outcome = collect_batch(pending, &album, &self.store, self.smug).await;
            let not_done: HashMap<PathBuf, &'static str> = outcome
                .refused
                .iter()
                .map(|p| {
                    (
                        p.path.clone(),
                        "no longer on SmugMug; it'll be uploaded on the next run",
                    )
                })
                .chain(
                    outcome
                        .failed
                        .iter()
                        .map(|p| (p.path.clone(), "couldn't be added to its album; will retry")),
                )
                .collect();
            for (prepared, cached) in members {
                match not_done.get(&prepared.file.path) {
                    Some(reason) => {
                        day.series.release(&album).await;
                        self.fail(&prepared, reason);
                    }
                    None => {
                        let record = prepared.record(
                            None,
                            Some(cached.smugmug_uri.clone()),
                            Some(album.album_key.clone()),
                            None,
                        );
                        self.put(&prepared, &record);
                        self.stats().collected += 1;
                    }
                }
            }
        }
    }

    async fn flush_collects(&self) {
        let batches: Vec<_> = self.collects.lock().await.drain().map(|(_, v)| v).collect();
        for mut batch in batches {
            while !batch.is_empty() {
                let rest = batch.split_off(batch.len().min(COLLECT_BATCH));
                self.collect(batch).await;
                batch = rest;
            }
        }
    }

    async fn upload(&self, prepared: Prepared) {
        // Another copy finished uploading since this one was read.
        if !self.options.no_cache
            && let Ok(Some(cached)) = self.store.get(&prepared.sha256)
        {
            return self.place_known(prepared, cached).await;
        }
        // Another copy is uploading right now: wait for it.
        let sha = prepared.sha256.clone();
        let prepared = {
            let mut in_flight = self.in_flight.lock().unwrap();
            match in_flight.get_mut(&sha) {
                Some(waiting) => {
                    waiting.push(prepared);
                    return;
                }
                None => {
                    in_flight.insert(sha.clone(), Vec::new());
                    prepared
                }
            }
        };

        let uploaded = self.upload_one(&prepared).await;

        let waiting = self
            .in_flight
            .lock()
            .unwrap()
            .remove(&sha)
            .unwrap_or_default();
        // (Their progress was counted when they were queued.)
        for copy in waiting {
            match &uploaded {
                Some(cached) => self.place_known(copy, cached.clone()).await,
                None => self.fail(&copy, "an identical file failed to upload"),
            }
        }
    }

    /// Upload or replace; the image it's now on SmugMug as, if it worked.
    async fn upload_one(&self, prepared: &Prepared) -> Option<UploadedFile> {
        let Kind::Upload {
            content,
            name,
            md5,
            size,
        } = &prepared.kind
        else {
            return None;
        };

        // Changed content at a path that was uploaded: replace its image,
        // unless another path is that image too.
        if let Some(previous) = &prepared.previous
            && let Some(old_uri) = &previous.image_uri
            && self.index.refs(old_uri).unwrap_or(u32::MAX) <= 1
        {
            match self
                .send(
                    UploadTarget::ReplaceImage(old_uri),
                    prepared,
                    content,
                    *size,
                    md5,
                    name,
                )
                .await
            {
                Ok(result) => {
                    let album_key = previous.album_key.clone().unwrap_or_default();
                    let cached = prepared.uploaded_file(&result.image_uri, &album_key, *size);
                    if self
                        .store
                        .get(&previous.sha256)
                        .ok()
                        .flatten()
                        .is_some_and(|c| &c.smugmug_uri == old_uri)
                    {
                        let _ = self.store.remove_deferred(&previous.sha256);
                    }
                    let _ = self.store.insert_deferred(&prepared.sha256, &cached);
                    let record = prepared.record(
                        Some(md5.clone()),
                        Some(result.image_uri),
                        Some(album_key),
                        None,
                    );
                    self.put(prepared, &record);
                    let mut stats = self.stats();
                    stats.replaced += 1;
                    stats.bytes_uploaded += size;
                    return Some(cached);
                }
                // The image is gone: upload it anew.
                Err(e)
                    if e.downcast_ref::<UploadRejected>()
                        .is_some_and(|r| r.status == 404) => {}
                Err(e) => {
                    self.fail_with(prepared, &e);
                    return None;
                }
            }
        }

        let day = match self.days.day(prepared.date).await {
            Ok(day) => day,
            Err(e) => {
                self.fail_with(prepared, &e);
                return None;
            }
        };
        let name = match day.check_name(name, md5).await {
            Ok(NameCheck::Present(image)) => {
                let cached = prepared.uploaded_file(&image.uri, &image.album_key, *size);
                let _ = self.store.insert_deferred(&prepared.sha256, &cached);
                let record = prepared.record(
                    Some(md5.clone()),
                    Some(image.uri),
                    Some(image.album_key),
                    None,
                );
                self.put(prepared, &record);
                self.stats().linked += 1;
                return Some(cached);
            }
            Ok(NameCheck::Upload(name)) => name,
            Err(e) => {
                self.fail_with(prepared, &e);
                return None;
            }
        };
        let album = match day.series.claim().await {
            Ok(album) => album,
            Err(e) => {
                day.release_name(&name).await;
                self.fail_with(prepared, &e);
                return None;
            }
        };
        match self
            .send(
                UploadTarget::Album(&album.uri),
                prepared,
                content,
                *size,
                md5,
                &name,
            )
            .await
        {
            Ok(result) => {
                day.name_uploaded(&name, &result.image_uri, &album.album_key)
                    .await;
                let cached = prepared.uploaded_file(&result.image_uri, &album.album_key, *size);
                let _ = self.store.insert_deferred(&prepared.sha256, &cached);
                let record = prepared.record(
                    Some(md5.clone()),
                    Some(result.image_uri),
                    Some(album.album_key.clone()),
                    None,
                );
                self.put(prepared, &record);
                let mut stats = self.stats();
                stats.uploaded += 1;
                stats.bytes_uploaded += size;
                Some(cached)
            }
            Err(e) => {
                day.series.release(&album).await;
                day.release_name(&name).await;
                self.fail_with(prepared, &e);
                None
            }
        }
    }

    /// Upload with retries for transient failures (network, 429, 5xx).
    async fn send(
        &self,
        target: UploadTarget<'_>,
        prepared: &Prepared,
        content: &Content,
        size: u64,
        md5: &str,
        name: &str,
    ) -> Result<UploadResult> {
        let attempts = self.options.retry_attempts.max(1);
        let mut attempt = 1;
        loop {
            match self
                .smug
                .upload(target, &prepared.file.path, content, size, md5, name)
                .await
            {
                Ok(result) => return Ok(result),
                Err(e) if attempt < attempts && is_transient(&e, target) => {
                    tokio::time::sleep(Duration::from_secs(2u64.pow(attempt))).await;
                    attempt += 1;
                }
                Err(e) => return Err(e),
            }
        }
    }

    /// Count a failure, to be retried next run.
    fn fail(&self, prepared: &Prepared, reason: &str) {
        let mut stats = self.stats();
        stats.failed += 1;
        stats.error(&prepared.file.path, reason);
    }

    /// Count a failed request. A file SmugMug refuses outright (too big,
    /// unsupported) is recorded so it isn't retried until it changes;
    /// anything else is retried next run.
    fn fail_with(&self, prepared: &Prepared, error: &anyhow::Error) {
        let refused = error
            .downcast_ref::<UploadRejected>()
            .is_some_and(UploadRejected::is_permanent);
        if !refused {
            return self.fail(prepared, &format!("{:#}", error));
        }
        let record = prepared.record(None, None, None, Some(error.to_string()));
        self.put(prepared, &record);
        let mut stats = self.stats();
        stats.refused += 1;
        stats.error(&prepared.file.path, error);
    }
}

/// Worth trying again. A new upload is only retried when SmugMug can't have
/// stored it (rate limited, or the connection never opened): after a 5xx or
/// a timeout the image may exist, and sending it again would duplicate it.
/// The next run finds such an image by name and MD5 and links it instead.
/// Replacing an image's content is safe to repeat.
fn is_transient(error: &anyhow::Error, target: UploadTarget<'_>) -> bool {
    let repeatable = matches!(target, UploadTarget::ReplaceImage(_));
    if let Some(rejected) = error.downcast_ref::<UploadRejected>() {
        return rejected.status == 429 || (repeatable && rejected.status >= 500);
    }
    match error.downcast_ref::<reqwest::Error>() {
        Some(e) if e.is_connect() => true,
        Some(e) => repeatable && (e.is_timeout() || e.is_request() || e.is_body()),
        // Reading the file failed: trying again won't help
        None => false,
    }
}

fn progress_bar(len: u64) -> ProgressBar {
    let bar = ProgressBar::new(len);
    bar.set_style(
        ProgressStyle::default_bar()
            .template(
                "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})",
            )
            .unwrap()
            .progress_chars("#>-"),
    );
    bar
}

/// Run once over `options.sources`.
pub async fn run<T: Smug + Uploads + CollectBackend>(
    smug: Arc<T>,
    options: &RunOptions,
    stop: &StopFlag,
) -> Result<RunStats> {
    let started = Instant::now();
    let store = HashStore::new(&options.cache_path.to_string_lossy())
        .context("Failed to open the cache")?;
    let index = FileIndex::open(&store)?;
    let mut stats = RunStats::default();

    // Scan (stat only).
    let scanned = {
        let sources = options.sources.clone();
        let excludes = options.excludes.clone();
        let raw = options.raw_handling;
        let live = options.live_photo_videos;
        tokio::task::spawn_blocking(move || scan::scan(&sources, &excludes, raw, live)).await??
    };
    stats.scanned = scanned.files.len();
    stats.unsupported = scanned.unsupported;
    stats.scan_errors = scanned.errors;
    stats.raw_rendered = scanned.raw_selection.rendered;
    stats.raw_skipped = scanned.raw_selection.skipped;
    stats.raw_skipped_with_sibling = scanned.raw_selection.skipped_with_sibling;
    stats.live_photo_videos_skipped = scanned.live_photo_videos_skipped;

    // Leave out files the index knows are unchanged.
    let (pending, unchanged, previously_refused) = {
        let index = index.clone();
        let no_cache = options.no_cache;
        let files = scanned.files;
        tokio::task::spawn_blocking(move || -> Result<_> {
            let mut pending = VecDeque::new();
            let (mut unchanged, mut refused) = (0, 0);
            for file in files {
                let previous = if no_cache {
                    None
                } else {
                    index.get(&file.path)?
                };
                match &previous {
                    Some(r) if r.matches(&file) && r.is_settled() => {
                        unchanged += 1;
                        if r.failed.is_some() {
                            refused += 1;
                        }
                    }
                    _ => pending.push_back(PendingFile { file, previous }),
                }
            }
            Ok((pending, unchanged, refused))
        })
        .await??
    };
    stats.unchanged = unchanged;
    stats.previously_refused = previously_refused;
    stats.to_process = pending.len();
    stats.bytes_to_process = pending.iter().map(|p| p.file.size).sum();

    if pending.is_empty() {
        stats.duration_secs = started.elapsed().as_secs();
        return Ok(stats);
    }

    if options.dry_run {
        let dated = dry_run_dates(pending, options.read_threads, &index, stop).await?;
        for (date, source) in dated {
            stats.dated(date, source);
        }
        stats.interrupted = stop.load(Ordering::Relaxed);
        stats.duration_secs = started.elapsed().as_secs();
        return Ok(stats);
    }

    let root = smug
        .root_folder(&options.root_folder, true, options.root_privacy)
        .await
        .with_context(|| format!("Failed to find or create folder {}", options.root_folder))?;
    let progress = progress_bar(pending.len() as u64);
    let workers = Workers {
        smug: smug.as_ref(),
        days: DayAlbums::new(smug.clone(), root, false),
        store: store.clone(),
        index: index.clone(),
        options,
        stats: std::sync::Mutex::new(stats),
        in_flight: std::sync::Mutex::new(HashMap::new()),
        collects: tokio::sync::Mutex::new(HashMap::new()),
        progress: progress.clone(),
    };

    // Readers: a few blocking threads reading files in path order.
    let queue = Arc::new(std::sync::Mutex::new(pending));
    let (tx, rx) = tokio::sync::mpsc::channel::<Result<Prepared, (PathBuf, anyhow::Error)>>(
        options.upload_threads.max(1) * 2,
    );
    let read_ctx = ReadContext {
        store: store.clone(),
        index: index.clone(),
        render_raw: options.raw_handling == RawHandling::Render,
        no_cache: options.no_cache,
    };
    let readers: Vec<_> = (0..options.read_threads.max(1))
        .map(|_| {
            let queue = queue.clone();
            let tx = tx.clone();
            let ctx = read_ctx.clone();
            let stop = stop.clone();
            tokio::task::spawn_blocking(move || {
                loop {
                    if stop.load(Ordering::Relaxed) {
                        break;
                    }
                    let Some(next) = queue.lock().unwrap().pop_front() else {
                        break;
                    };
                    let path = next.file.path.clone();
                    let result = prepare(next, &ctx).map_err(|e| (path, e));
                    if tx.blocking_send(result).is_err() {
                        break;
                    }
                }
            })
        })
        .collect();
    drop(tx);

    // Uploaders: several workers on this task (uploads are I/O-bound).
    let rx = tokio::sync::Mutex::new(rx);
    let uploaders = (0..options.upload_threads.max(1)).map(|_| async {
        loop {
            let next = rx.lock().await.recv().await;
            match next {
                Some(Ok(prepared)) => workers.handle(prepared).await,
                Some(Err((path, e))) => {
                    let mut stats = workers.stats();
                    stats.failed += 1;
                    stats.error(&path, format!("{:#}", e));
                    drop(stats);
                    workers.progress.inc(1);
                }
                None => break,
            }
        }
    });

    let done = tokio::sync::Notify::new();
    let ticker = async {
        if std::io::stderr().is_terminal() {
            return;
        }
        loop {
            tokio::select! {
                _ = done.notified() => return,
                _ = tokio::time::sleep(Duration::from_secs(60)) => {
                    let s = workers.stats();
                    println!(
                        "… {}/{} processed: {} uploaded ({}), {} replaced, {} already there, {} added from other albums, {} failed",
                        progress.position(),
                        progress.length().unwrap_or(0),
                        s.uploaded,
                        human_bytes(s.bytes_uploaded),
                        s.replaced,
                        s.linked + s.touched,
                        s.collected,
                        s.failed + s.refused,
                    );
                }
            }
        }
    };

    let work = async {
        let (_, readers) = tokio::join!(join_all(uploaders), join_all(readers));
        for reader in readers {
            if let Err(e) = reader {
                eprintln!("{}", format!("A reader thread failed: {}", e).red());
            }
        }
        workers.flush_collects().await;
        done.notify_one();
    };
    tokio::join!(work, ticker);
    progress.finish_and_clear();

    store.flush()?;
    let mut stats = workers.stats.into_inner().unwrap();
    for day in workers.days.loaded_days() {
        stats.albums_created += day
            .series
            .albums()
            .await
            .iter()
            .filter(|a| a.created)
            .count();
    }
    stats.interrupted = stop.load(Ordering::Relaxed);
    stats.duration_secs = started.elapsed().as_secs();
    Ok(stats)
}

/// A dry run only dates files (reading their metadata, not their content)
/// and touches nothing on SmugMug.
async fn dry_run_dates(
    pending: VecDeque<PendingFile>,
    threads: usize,
    index: &FileIndex,
    stop: &StopFlag,
) -> Result<Vec<(NaiveDate, DateSource)>> {
    let queue = Arc::new(std::sync::Mutex::new(pending));
    let handles: Vec<_> = (0..threads.max(1))
        .map(|_| {
            let queue = queue.clone();
            let index = index.clone();
            let stop = stop.clone();
            tokio::task::spawn_blocking(move || {
                let mut out = Vec::new();
                while !stop.load(Ordering::Relaxed) {
                    let Some(next) = queue.lock().unwrap().pop_front() else {
                        break;
                    };
                    out.push(date_for(&next.file, None, &index));
                }
                out
            })
        })
        .collect();
    let mut dated = Vec::new();
    for handle in handles {
        dated.extend(handle.await?);
    }
    Ok(dated)
}

pub fn human_bytes(bytes: u64) -> String {
    const UNITS: [&str; 5] = ["B", "KB", "MB", "GB", "TB"];
    let mut value = bytes as f64;
    let mut unit = 0;
    while value >= 1024.0 && unit < UNITS.len() - 1 {
        value /= 1024.0;
        unit += 1;
    }
    if unit == 0 {
        format!("{} B", bytes)
    } else {
        format!("{:.1} {}", value, UNITS[unit])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::images::{AlbumImage, CollectResult};
    use crate::dated::plan::tests::FakeSmug;
    use std::fs;
    use std::time::SystemTime;
    use tempfile::TempDir;

    impl Uploads for FakeSmug {
        async fn upload(
            &self,
            target: UploadTarget<'_>,
            _path: &Path,
            _content: &Content,
            size: u64,
            md5: &str,
            name: &str,
        ) -> Result<UploadResult> {
            if self.refuse.lock().unwrap().contains(name) {
                return Err(UploadRejected {
                    status: 413,
                    body: "too big".into(),
                }
                .into());
            }
            match target {
                UploadTarget::Album(album_uri) => {
                    self.calls
                        .lock()
                        .unwrap()
                        .push(format!("upload {} {}", album_uri, name));
                    let key = album_uri.rsplit('/').next().unwrap().to_string();
                    let uri = format!("{}/image/{}-{}", album_uri, self.id(), name);
                    self.images
                        .lock()
                        .unwrap()
                        .entry(key.clone())
                        .or_default()
                        .push(AlbumImage {
                            image_key: uri.rsplit('/').next().unwrap().into(),
                            file_name: name.into(),
                            archived_uri: String::new(),
                            file_size: size,
                            format: "JPG".into(),
                            uri: uri.clone(),
                            title: None,
                            archived_md5: Some(md5.into()),
                        });
                    Ok(UploadResult {
                        image_key: uri.rsplit('/').next().unwrap().into(),
                        image_uri: uri,
                        status_code: 200,
                    })
                }
                UploadTarget::ReplaceImage(uri) => {
                    self.calls.lock().unwrap().push(format!("replace {}", uri));
                    Ok(UploadResult {
                        image_key: uri.rsplit('/').next().unwrap().into(),
                        image_uri: uri.into(),
                        status_code: 200,
                    })
                }
            }
        }

        async fn root_folder(
            &self,
            _path: &str,
            _create: bool,
            _privacy: Option<&str>,
        ) -> Result<Option<String>> {
            Ok(Some("/node/root".into()))
        }
    }

    impl CollectBackend for FakeSmug {
        async fn collect(&self, album_key: &str, uris: &[String]) -> Result<CollectResult> {
            self.calls
                .lock()
                .unwrap()
                .push(format!("collect {} {}", album_key, uris.len()));
            Ok(CollectResult::default())
        }
    }

    struct Setup {
        photos: TempDir,
        cache: TempDir,
        smug: Arc<FakeSmug>,
    }

    impl Setup {
        fn new() -> Self {
            let (smug, _) = FakeSmug::with_root();
            Setup {
                photos: TempDir::new().unwrap(),
                cache: TempDir::new().unwrap(),
                smug,
            }
        }

        fn write(&self, rel: &str, data: &[u8]) -> PathBuf {
            let path = self.photos.path().join(rel);
            fs::create_dir_all(path.parent().unwrap()).unwrap();
            fs::write(&path, data).unwrap();
            path
        }

        fn options(&self) -> RunOptions {
            RunOptions {
                sources: vec![self.photos.path().to_path_buf()],
                excludes: Vec::new(),
                root_folder: "Backup".into(),
                root_privacy: Some("Private"),
                upload_threads: 4,
                read_threads: 2,
                dry_run: false,
                no_cache: false,
                raw_handling: RawHandling::Render,
                live_photo_videos: LivePhotoVideos::Upload,
                retry_attempts: 1,
                cache_path: self.cache.path().join("db"),
            }
        }

        async fn run(&self) -> RunStats {
            self.run_with(self.options()).await
        }

        async fn run_with(&self, options: RunOptions) -> RunStats {
            let stop: StopFlag = Arc::new(AtomicBool::new(false));
            let stats = run(self.smug.clone(), &options, &stop).await.unwrap();
            assert!(
                stats.errors.is_empty() || stats.failed + stats.refused > 0,
                "{:?}",
                stats.errors
            );
            stats
        }

        fn calls(&self, prefix: &str) -> usize {
            self.smug.calls(prefix)
        }

        fn album_names(&self) -> Vec<String> {
            let nodes = self.smug.nodes.lock().unwrap();
            let mut names: Vec<String> = nodes
                .values()
                .flatten()
                .filter(|c| c.node_type == "Album")
                .map(|c| c.name.clone())
                .collect();
            names.sort();
            names
        }
    }

    fn set_mtime(path: &Path, secs_ago: u64) {
        let file = fs::File::options().write(true).open(path).unwrap();
        file.set_modified(SystemTime::now() - Duration::from_secs(secs_ago))
            .unwrap();
    }

    #[tokio::test]
    async fn uploads_into_day_albums_then_skips_unchanged_files_unread() {
        let s = Setup::new();
        s.write("a/IMG_20140712_0001.jpg", b"one");
        s.write("b/IMG_20140712_0002.jpg", b"two");
        s.write("b/IMG_20150101_0003.jpg", b"three");

        let stats = s.run().await;
        assert_eq!(stats.uploaded, 3);
        assert_eq!(stats.albums_created, 2);
        assert_eq!(s.album_names(), vec!["2014-07-12", "2015-01-01"]);

        // Second run: nothing read, nothing sent, no lookups at all.
        let calls_before = s.smug.calls.lock().unwrap().len();
        let stats = s.run().await;
        assert_eq!(stats.unchanged, 3);
        assert_eq!(stats.to_process, 0);
        assert_eq!(s.smug.calls.lock().unwrap().len(), calls_before);
    }

    #[tokio::test]
    async fn touched_files_are_rerecorded_not_uploaded() {
        let s = Setup::new();
        let path = s.write("IMG_20140712_0001.jpg", b"one");
        s.run().await;
        set_mtime(&path, 3600);

        let stats = s.run().await;
        assert_eq!(stats.touched, 1);
        assert_eq!(s.calls("upload"), 1);

        // And now it's unchanged again.
        assert_eq!(s.run().await.unchanged, 1);
    }

    #[tokio::test]
    async fn edited_files_replace_their_image() {
        let s = Setup::new();
        let path = s.write("IMG_20140712_0001.jpg", b"one");
        s.run().await;
        fs::write(&path, b"one, edited").unwrap();
        set_mtime(&path, 10);

        let stats = s.run().await;
        assert_eq!(stats.replaced, 1);
        assert_eq!(s.calls("replace"), 1);
        assert_eq!(s.calls("upload"), 1);
    }

    #[tokio::test]
    async fn identical_copies_upload_once() {
        let s = Setup::new();
        for i in 0..6 {
            s.write(&format!("copy{}/IMG_20140712_0001.jpg", i), b"same bytes");
        }
        let stats = s.run().await;
        assert_eq!(stats.uploaded, 1);
        assert_eq!(stats.linked, 5);
        assert_eq!(s.calls("upload"), 1);
    }

    #[tokio::test]
    async fn editing_one_of_two_copies_uploads_it_anew() {
        let s = Setup::new();
        let a = s.write("a/IMG_20140712_0001.jpg", b"same");
        s.write("b/IMG_20140712_0001.jpg", b"same");
        s.run().await;
        fs::write(&a, b"different now").unwrap();
        set_mtime(&a, 10);

        let stats = s.run().await;
        // The shared image keeps b's content; a's edit goes up separately,
        // renamed since the name is taken by other content.
        assert_eq!(stats.replaced, 0);
        assert_eq!(stats.uploaded, 1);
        assert_eq!(s.calls("replace"), 0);
        assert!(
            s.smug
                .calls
                .lock()
                .unwrap()
                .iter()
                .any(|c| c.contains("IMG_20140712_0001~"))
        );
    }

    #[tokio::test]
    async fn same_name_different_content_is_renamed() {
        let s = Setup::new();
        s.write("cam1/IMG_20140712_0001.jpg", b"camera one");
        s.write("cam2/IMG_20140712_0001.jpg", b"camera two");
        let stats = s.run().await;
        assert_eq!(stats.uploaded, 2);
        let names: Vec<String> = s
            .smug
            .images
            .lock()
            .unwrap()
            .values()
            .flatten()
            .map(|i| i.file_name.clone())
            .collect();
        assert!(names.contains(&"IMG_20140712_0001.jpg".to_string()));
        assert!(
            names
                .iter()
                .any(|n| n.starts_with("IMG_20140712_0001~") && n.ends_with(".jpg"))
        );
    }

    #[tokio::test]
    async fn content_already_in_the_day_album_is_linked_without_cache() {
        let s = Setup::new();
        s.write("IMG_20140712_0001.jpg", b"one");
        s.run().await;
        // A fresh cache (new container) finds it by name and MD5.
        let mut options = s.options();
        options.cache_path = s.cache.path().join("fresh");
        let stats = s.run_with(options).await;
        assert_eq!(stats.linked, 1);
        assert_eq!(s.calls("upload"), 1);
    }

    #[tokio::test]
    async fn files_in_other_albums_are_collected() {
        let s = Setup::new();
        let path = s.write("IMG_20140712_0001.jpg", b"one");
        // The cache knows this content from an album elsewhere.
        {
            let store = HashStore::new(&s.cache.path().join("db").to_string_lossy()).unwrap();
            let (sha, _, size) = hash_file(&path).unwrap();
            store
                .insert(
                    &sha,
                    UploadedFile {
                        smugmug_uri: "/api/v2/album/ELSEWHERE/image/X-0".into(),
                        album_key: "ELSEWHERE".into(),
                        image_key: "X-0".into(),
                        uploaded_at: Utc::now(),
                        file_size: size,
                        original_path: "elsewhere".into(),
                    },
                )
                .unwrap();
        }
        let stats = s.run().await;
        assert_eq!(stats.collected, 1);
        assert_eq!(s.calls("collect"), 1);
        assert_eq!(s.calls("upload"), 0);
        assert_eq!(s.run().await.unchanged, 1);
    }

    #[tokio::test]
    async fn live_photo_videos_go_to_their_photos_day() {
        let s = Setup::new();
        s.write("IMG_20140712_0001.HEIC", b"photo");
        let video = s.write("IMG_20140712_0001.MOV", b"video");
        // Without the pairing, the video's mtime would date it today.
        set_mtime(&video, 0);
        let stats = s.run().await;
        assert_eq!(stats.uploaded, 2);
        assert_eq!(s.album_names(), vec!["2014-07-12"]);
    }

    fn fake_raw(extra: &[u8]) -> Vec<u8> {
        let mut raw = crate::raw::exif::tests::fake_tiff_raw_be();
        raw.extend(crate::raw::jpeg::tests::fake_jpeg(0xC0, 6000, 4000, None));
        raw.extend_from_slice(extra);
        raw
    }

    #[tokio::test]
    async fn raw_files_are_rendered_once() {
        let s = Setup::new();
        let path = s.write("IMG_0001.CR2", &fake_raw(b""));
        let stats = s.run().await;
        assert_eq!(stats.uploaded, 1);
        // Dated from the RAW's EXIF.
        assert_eq!(s.album_names(), vec!["2024-06-01"]);
        let uploaded = s
            .smug
            .images
            .lock()
            .unwrap()
            .values()
            .flatten()
            .next()
            .unwrap()
            .clone();
        assert_eq!(uploaded.file_name, "IMG_0001.jpg");

        // Touched: same content, nothing rendered or sent.
        set_mtime(&path, 3600);
        assert_eq!(s.run().await.touched, 1);

        // Changed bytes outside the preview (a metadata edit): rendered
        // again, but the JPEG is the same, so nothing is sent.
        fs::write(&path, fake_raw(b"\0\0\0\0")).unwrap();
        set_mtime(&path, 10);
        let stats = s.run().await;
        assert_eq!(stats.touched, 1);
        assert_eq!(s.calls("upload") + s.calls("replace"), 1);

        // Moved: found by content, linked without rendering.
        let moved = s.write("moved/IMG_0001.CR2", &fs::read(&path).unwrap());
        fs::remove_file(&path).unwrap();
        let _ = moved;
        let stats = s.run().await;
        assert_eq!(stats.linked, 1);
        assert_eq!(s.calls("upload") + s.calls("replace"), 1);
    }

    #[tokio::test]
    async fn refused_files_are_not_retried_until_they_change() {
        let s = Setup::new();
        let path = s.write("IMG_20140712_0001.jpg", b"huge");
        s.smug
            .refuse
            .lock()
            .unwrap()
            .insert("IMG_20140712_0001.jpg".into());
        let stats = s.run().await;
        assert_eq!(stats.refused, 1);

        let stats = s.run().await;
        assert_eq!(stats.previously_refused, 1);
        assert_eq!(stats.to_process, 0);

        s.smug.refuse.lock().unwrap().clear();
        fs::write(&path, b"smaller").unwrap();
        set_mtime(&path, 10);
        assert_eq!(s.run().await.uploaded, 1);
    }

    #[tokio::test]
    async fn dry_run_dates_files_and_touches_nothing() {
        let s = Setup::new();
        s.write("IMG_20140712_0001.jpg", b"one");
        s.write("IMG_20140713_0001.jpg", b"two");
        let mut options = s.options();
        options.dry_run = true;
        let stats = s.run_with(options).await;
        assert_eq!(stats.to_process, 2);
        assert_eq!(stats.days.len(), 2);
        assert_eq!(stats.date_sources.get("file name"), Some(&2));
        assert!(s.smug.calls.lock().unwrap().is_empty());
        // Nothing recorded either.
        assert_eq!(s.run().await.uploaded, 2);
    }

    #[tokio::test]
    async fn excluded_paths_are_left_out() {
        let s = Setup::new();
        s.write("keep/IMG_20140712_0001.jpg", b"one");
        s.write("old_bak/IMG_20140712_0002.jpg", b"two");
        let mut options = s.options();
        options.excludes = vec!["old_bak/".into()];
        let stats = s.run_with(options).await;
        assert_eq!(stats.scanned, 1);
        assert_eq!(stats.uploaded, 1);
    }

    #[test]
    fn new_uploads_are_retried_only_when_nothing_can_have_been_stored() {
        let rejected = |status| {
            anyhow::Error::from(UploadRejected {
                status,
                body: String::new(),
            })
        };
        let album = UploadTarget::Album("/api/v2/album/A");
        let replace = UploadTarget::ReplaceImage("/api/v2/album/A/image/B-0");
        assert!(is_transient(&rejected(429), album));
        assert!(!is_transient(&rejected(500), album));
        assert!(!is_transient(&rejected(503), album));
        assert!(is_transient(&rejected(503), replace));
        assert!(!is_transient(&rejected(413), replace));
        assert!(!is_transient(&anyhow::anyhow!("file vanished"), replace));
    }

    #[test]
    fn human_sizes() {
        assert_eq!(human_bytes(512), "512 B");
        assert_eq!(human_bytes(1536), "1.5 KB");
        assert_eq!(human_bytes(5 * 1024 * 1024 * 1024), "5.0 GB");
    }
}