-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncPhotos.cs
73 lines (62 loc) · 3.21 KB
/
SyncPhotos.cs
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
using GooglePhotoSync.Google;
using GooglePhotoSync.Local;
using GooglePhotoSync.Sync;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
namespace GooglePhotoSync
{
public class SyncPhotos
{
private readonly GoogleBearerTokenRetriever m_GoogleBearerTokenRetriever;
private readonly LocalSource m_LocalSource;
private readonly GoogleSource m_GoogleSource;
private readonly CollectionComparer m_CollectionComparer;
private readonly CollectionSync m_CollectionSync;
private readonly ILogger<SyncPhotos> m_Logger;
public SyncPhotos(GoogleBearerTokenRetriever googleBearerTokenRetriever,
LocalSource localSource,
GoogleSource googleSource,
CollectionComparer collectionComparer,
CollectionSync collectionSync,
IOptions<SyncSettings> settings,
ILogger<SyncPhotos> logger)
{
m_GoogleBearerTokenRetriever = googleBearerTokenRetriever;
m_LocalSource = localSource;
m_GoogleSource = googleSource;
m_CollectionComparer = collectionComparer;
m_CollectionSync = collectionSync;
m_Logger = logger;
if (settings.Value.BatchSize > 50)
throw new InvalidOperationException($"{nameof(SyncSettings.BatchSize)} must not be greater than 50.");
if (settings.Value.BatchSize <= 0)
throw new InvalidOperationException($"{nameof(SyncSettings.BatchSize)} must be greater than 0.");
if (settings.Value.ParallelUploads > settings.Value.BatchSize)
throw new InvalidOperationException($"{nameof(SyncSettings.ParallelUploads)} must not be greater than {nameof(SyncSettings.BatchSize)}.");
if (settings.Value.ParallelUploads <= 0)
throw new InvalidOperationException($"{nameof(SyncSettings.ParallelUploads)} must be greater than 0.");
}
public async Task Sync()
{
if (!await m_GoogleBearerTokenRetriever.Init())
return;
m_Logger.LogInformation("Loading Google Albums");
await m_GoogleSource.Load();
m_Logger.LogInformation($"Total Albums: {m_GoogleSource.Albums.Count}");
m_Logger.LogInformation("Loading local collection");
m_LocalSource.Load();
m_Logger.LogInformation($"Total Albums: {m_LocalSource.PhotoAlbums.Count}");
m_Logger.LogInformation($"Total Files: {m_LocalSource.TotalFiles}");
m_Logger.LogInformation($"Total Size: {m_LocalSource.TotalBytes.AsHumanReadableBytes("MB")}");
m_Logger.LogInformation("Comparing");
var collectionDiff = await m_CollectionComparer.Compare(m_LocalSource, m_GoogleSource);
m_Logger.LogInformation(collectionDiff.ToString());
m_Logger.LogInformation("Syncing");
await m_CollectionSync.SyncCollection(collectionDiff);
m_Logger.LogInformation("Finished. Press Enter to Quit.");
Console.ReadLine();
}
}
}