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
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct UploadedFile {
    pub smugmug_uri: String,
    pub album_key: String,
    pub image_key: String,
    pub uploaded_at: DateTime<Utc>,
    pub file_size: u64,
    pub original_path: String,
}

/// Thread-safe hash store using sled embedded database
pub struct HashStore {
    db: Arc<sled::Db>,
}

impl HashStore {
    /// Create a new HashStore at the specified path
    pub fn new(path: &str) -> Result<Self> {
        let db = sled::open(path).context("Failed to open sled database")?;

        Ok(HashStore { db: Arc::new(db) })
    }

    /// Get an uploaded file by its hash
    pub fn get(&self, hash: &str) -> Result<Option<UploadedFile>> {
        let value = self
            .db
            .get(hash.as_bytes())
            .context("Failed to read from database")?;

        match value {
            Some(bytes) => {
                let file: UploadedFile =
                    serde_json::from_slice(&bytes).context("Failed to deserialize UploadedFile")?;
                Ok(Some(file))
            }
            None => Ok(None),
        }
    }

    /// Insert a new uploaded file record
    pub fn insert(&self, hash: &str, file: UploadedFile) -> Result<()> {
        let serialized = serde_json::to_vec(&file).context("Failed to serialize UploadedFile")?;

        self.db
            .insert(hash.as_bytes(), serialized)
            .context("Failed to insert into database")?;

        self.db.flush().context("Failed to flush database")?;

        Ok(())
    }

    /// Insert without flushing. Sled flushes on its own every half second,
    /// so a crash loses at most that much; for runs that insert many records
    /// and call `flush` when done.
    pub fn insert_deferred(&self, hash: &str, file: &UploadedFile) -> Result<()> {
        let serialized = serde_json::to_vec(file).context("Failed to serialize UploadedFile")?;
        self.db
            .insert(hash.as_bytes(), serialized)
            .context("Failed to insert into database")?;
        Ok(())
    }

    /// Forget one file without flushing (see `insert_deferred`).
    pub fn remove_deferred(&self, hash: &str) -> Result<()> {
        self.db
            .remove(hash.as_bytes())
            .context("Failed to remove from database")?;
        Ok(())
    }

    /// Write everything to disk.
    pub fn flush(&self) -> Result<()> {
        self.db.flush().context("Failed to flush database")?;
        Ok(())
    }

    /// Another keyspace in the same database (e.g. the per-path file index),
    /// since sled lets only one process open it at a time.
    pub fn open_tree(&self, name: &str) -> Result<sled::Tree> {
        self.db
            .open_tree(name)
            .with_context(|| format!("Failed to open database tree '{}'", name))
    }

    /// Forget one file, e.g. when the image it points to no longer exists
    pub fn remove(&self, hash: &str) -> Result<()> {
        self.db
            .remove(hash.as_bytes())
            .context("Failed to remove from database")?;
        self.db.flush().context("Failed to flush database")?;
        Ok(())
    }

    /// Clear all entries from the cache
    pub fn clear(&self) -> Result<()> {
        self.db.clear().context("Failed to clear database")?;
        // And every other keyspace (the file index), which would otherwise
        // keep files counted as uploaded.
        for name in self.db.tree_names() {
            if name.as_ref() != b"__sled__default" {
                self.db
                    .drop_tree(&name)
                    .context("Failed to clear database tree")?;
            }
        }

        self.db.flush().context("Failed to flush database")?;

        Ok(())
    }

    /// Get cache statistics
    pub fn stats(&self) -> Result<CacheStats> {
        let mut total_entries = 0;
        let mut total_size = 0u64;
        let mut oldest: Option<DateTime<Utc>> = None;
        let mut newest: Option<DateTime<Utc>> = None;

        for item in self.db.iter() {
            let (_key, value) = item.context("Failed to read database entry")?;
            total_entries += 1;

            // Deserialize to get file size and timestamp
            if let Ok(file) = serde_json::from_slice::<UploadedFile>(&value) {
                total_size += file.file_size;
                let timestamp = file.uploaded_at;

                oldest = Some(oldest.map_or(timestamp, |old| old.min(timestamp)));
                newest = Some(newest.map_or(timestamp, |new| new.max(timestamp)));
            }
        }

        Ok(CacheStats {
            total_entries,
            total_size,
            oldest_entry: oldest,
            newest_entry: newest,
        })
    }

    /// Get the total number of entries in the cache
    #[allow(dead_code)]
    pub fn count(&self) -> Result<usize> {
        let mut count = 0;
        for item in self.db.iter() {
            item.context("Failed to read database entry")?;
            count += 1;
        }
        Ok(count)
    }

    /// Get the total size of cached data in bytes (sum of file_size fields)
    #[allow(dead_code)]
    pub fn size(&self) -> Result<u64> {
        let mut total_size = 0u64;
        for item in self.db.iter() {
            let (_key, value) = item.context("Failed to read database entry")?;
            if let Ok(file) = serde_json::from_slice::<UploadedFile>(&value) {
                total_size += file.file_size;
            }
        }
        Ok(total_size)
    }
}

// Implement Clone for HashStore since it uses Arc internally
impl Clone for HashStore {
    fn clone(&self) -> Self {
        HashStore {
            db: Arc::clone(&self.db),
        }
    }
}

#[derive(Debug, Clone)]
pub struct CacheStats {
    pub total_entries: usize,
    pub total_size: u64,
    pub oldest_entry: Option<DateTime<Utc>>,
    pub newest_entry: Option<DateTime<Utc>>,
}

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

    fn temp_dir() -> String {
        let dir = tempfile::tempdir().unwrap();
        dir.path().to_str().unwrap().to_string()
    }

    fn create_test_file(timestamp: DateTime<Utc>) -> UploadedFile {
        UploadedFile {
            smugmug_uri: "https://api.smugmug.com/api/v2/image/test".to_string(),
            album_key: "album123".to_string(),
            image_key: "img456".to_string(),
            uploaded_at: timestamp,
            file_size: 1024,
            original_path: "/test/path.jpg".to_string(),
        }
    }

    #[test]
    fn test_new_store() {
        let dir = temp_dir();
        let store = HashStore::new(&dir);
        assert!(store.is_ok());
    }

    #[test]
    fn test_insert_and_get() {
        let dir = temp_dir();
        let store = HashStore::new(&dir).unwrap();

        let file = create_test_file(Utc::now());
        let hash = "abc123";

        // Insert
        store.insert(hash, file.clone()).unwrap();

        // Retrieve
        let retrieved = store.get(hash).unwrap();
        assert!(retrieved.is_some());
        let retrieved = retrieved.unwrap();
        assert_eq!(retrieved.smugmug_uri, file.smugmug_uri);
        assert_eq!(retrieved.album_key, file.album_key);
        assert_eq!(retrieved.image_key, file.image_key);
    }

    #[test]
    fn test_get_nonexistent() {
        let dir = temp_dir();
        let store = HashStore::new(&dir).unwrap();

        let result = store.get("nonexistent").unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_clear() {
        let dir = temp_dir();
        let store = HashStore::new(&dir).unwrap();

        // Insert multiple entries
        for i in 0..5 {
            let hash = format!("hash{}", i);
            let file = create_test_file(Utc::now());
            store.insert(&hash, file).unwrap();
        }

        // Verify entries exist
        let count_before = store.count().unwrap();
        assert_eq!(count_before, 5);

        // Clear
        store.clear().unwrap();

        // Verify cleared
        let count_after = store.count().unwrap();
        assert_eq!(count_after, 0);
    }

    #[test]
    fn test_count() {
        let dir = temp_dir();
        let store = HashStore::new(&dir).unwrap();

        // Empty store
        assert_eq!(store.count().unwrap(), 0);

        // Add entries
        for i in 0..10 {
            let hash = format!("hash{}", i);
            let file = create_test_file(Utc::now());
            store.insert(&hash, file).unwrap();
        }

        assert_eq!(store.count().unwrap(), 10);
    }

    #[test]
    fn test_size() {
        let dir = temp_dir();
        let store = HashStore::new(&dir).unwrap();

        // Empty store
        assert_eq!(store.size().unwrap(), 0);

        // Add entries
        let file = create_test_file(Utc::now());
        store.insert("hash1", file).unwrap();

        // Size should be greater than 0
        let size = store.size().unwrap();
        assert!(size > 0);
    }

    #[test]
    fn test_stats_empty() {
        let dir = temp_dir();
        let store = HashStore::new(&dir).unwrap();

        let stats = store.stats().unwrap();
        assert_eq!(stats.total_entries, 0);
        assert_eq!(stats.total_size, 0);
        assert!(stats.oldest_entry.is_none());
        assert!(stats.newest_entry.is_none());
    }

    #[test]
    fn test_stats_single_entry() {
        let dir = temp_dir();
        let store = HashStore::new(&dir).unwrap();

        let timestamp = Utc.with_ymd_and_hms(2024, 3, 15, 12, 0, 0).unwrap();
        let file = create_test_file(timestamp);
        store.insert("hash1", file).unwrap();

        let stats = store.stats().unwrap();
        assert_eq!(stats.total_entries, 1);
        assert!(stats.total_size > 0);
        assert_eq!(stats.oldest_entry, Some(timestamp));
        assert_eq!(stats.newest_entry, Some(timestamp));
    }

    #[test]
    fn test_stats_multiple_entries() {
        let dir = temp_dir();
        let store = HashStore::new(&dir).unwrap();

        // Create entries with different timestamps
        let oldest = Utc.with_ymd_and_hms(2024, 1, 15, 12, 0, 0).unwrap();
        let middle = Utc.with_ymd_and_hms(2024, 2, 15, 12, 0, 0).unwrap();
        let newest = Utc.with_ymd_and_hms(2024, 3, 15, 12, 0, 0).unwrap();

        store.insert("hash1", create_test_file(middle)).unwrap();
        store.insert("hash2", create_test_file(oldest)).unwrap();
        store.insert("hash3", create_test_file(newest)).unwrap();

        let stats = store.stats().unwrap();
        assert_eq!(stats.total_entries, 3);
        assert!(stats.total_size > 0);
        assert_eq!(stats.oldest_entry, Some(oldest));
        assert_eq!(stats.newest_entry, Some(newest));
    }

    #[test]
    fn test_stats_size_calculation() {
        let dir = temp_dir();
        let store = HashStore::new(&dir).unwrap();

        // Insert one entry
        let file1 = create_test_file(Utc::now());
        store.insert("hash1", file1).unwrap();
        let size1 = store.stats().unwrap().total_size;

        // Insert another entry
        let file2 = create_test_file(Utc::now());
        store.insert("hash2", file2).unwrap();
        let size2 = store.stats().unwrap().total_size;

        // Size should have increased
        assert!(size2 > size1);
    }

    #[test]
    fn test_clone() {
        let dir = temp_dir();
        let store1 = HashStore::new(&dir).unwrap();

        let file = create_test_file(Utc::now());
        store1.insert("hash1", file).unwrap();

        // Clone the store
        let store2 = store1.clone();

        // Both should be able to read the same data
        assert!(store1.get("hash1").unwrap().is_some());
        assert!(store2.get("hash1").unwrap().is_some());
        assert_eq!(store1.count().unwrap(), store2.count().unwrap());
    }

    #[test]
    fn test_thread_safety() {
        use std::thread;

        let dir = temp_dir();
        let store = HashStore::new(&dir).unwrap();

        let store1 = store.clone();
        let store2 = store.clone();

        let handle1 = thread::spawn(move || {
            for i in 0..50 {
                let hash = format!("thread1_hash{}", i);
                let file = create_test_file(Utc::now());
                store1.insert(&hash, file).unwrap();
            }
        });

        let handle2 = thread::spawn(move || {
            for i in 0..50 {
                let hash = format!("thread2_hash{}", i);
                let file = create_test_file(Utc::now());
                store2.insert(&hash, file).unwrap();
            }
        });

        handle1.join().unwrap();
        handle2.join().unwrap();

        // Should have 100 entries total
        assert_eq!(store.count().unwrap(), 100);
    }

    #[test]
    fn test_update_existing_entry() {
        let dir = temp_dir();
        let store = HashStore::new(&dir).unwrap();

        let hash = "hash1";
        let file1 = create_test_file(Utc.with_ymd_and_hms(2024, 1, 1, 12, 0, 0).unwrap());
        let file2 = create_test_file(Utc.with_ymd_and_hms(2024, 2, 1, 12, 0, 0).unwrap());

        // Insert first version
        store.insert(hash, file1).unwrap();
        let retrieved1 = store.get(hash).unwrap().unwrap();
        assert_eq!(
            retrieved1.uploaded_at,
            Utc.with_ymd_and_hms(2024, 1, 1, 12, 0, 0).unwrap()
        );

        // Update with second version
        store.insert(hash, file2).unwrap();
        let retrieved2 = store.get(hash).unwrap().unwrap();
        assert_eq!(
            retrieved2.uploaded_at,
            Utc.with_ymd_and_hms(2024, 2, 1, 12, 0, 0).unwrap()
        );

        // Should still have only 1 entry
        assert_eq!(store.count().unwrap(), 1);
    }
}