-
Notifications
You must be signed in to change notification settings - Fork 0
/
Uploader.cs
408 lines (376 loc) · 11.7 KB
/
Uploader.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Microsoft.Win32;
using FlickrNet;
namespace MyPicturesSync {
/// <summary>
/// Photo info from Flickr
/// </summary>
public class FlickrPhotoInfo {
public string Id;
public DateTime? Uploaded;
}
/// <summary>
/// Local photo info
/// </summary>
public class PhotoInfo {
public PhotoInfo() {
Flickr = new FlickrPhotoInfo();
}
public PhotoInfo(string path)
: this(new FileInfo(path)) {
}
public PhotoInfo(FileInfo f)
: this() {
FullName = f.FullName;
FileName = f.Name;
Folder = f.Directory.Name;
Name = Path.GetFileNameWithoutExtension(f.Name);
Description = Folder + "-" + Name;
}
public void LoadHash() {
using (FileStream s = new FileStream(FullName, FileMode.Open, FileAccess.Read)) {
using (SHA1Managed sha1 = new SHA1Managed()) {
Hash = BitConverter.ToString(sha1.ComputeHash(s));
}
}
FlickrPhotoInfo p;
if (StatusForm.Instance.Hashes.TryGetValue(Hash, out p)) {
Flickr = p;
} else {
StatusForm.Instance.Hashes[Hash] = Flickr;
}
}
public string Hash;
public string Description;
public string FileName;
public string FullName;
public string Folder;
public string Name;
public FlickrPhotoInfo Flickr;
public override string ToString() {
return FileName;
}
}
/// <summary>
/// Local folder info
/// </summary>
public class Set {
public Set(string path) {
Folder = new DirectoryInfo(path);
Photos = new List<PhotoInfo>();
if (System.IO.File.Exists(Path.Combine(path, "ignore")))
return;
foreach (FileInfo file in Folder.EnumerateFiles("*.jpg")) {
PhotoInfo p = new PhotoInfo(file);
Photos.Add(p);
}
}
/// <summary>
/// Flickr set Id
/// </summary>
public string Id;
public DirectoryInfo Folder;
public List<PhotoInfo> Photos;
public override string ToString() {
return Folder.Name;
}
}
/// <summary>
/// Does the work of syncing with Flickr
/// </summary>
class Uploader {
/// <summary>
/// Running thread
/// </summary>
Task task;
/// <summary>
/// Signal to stop thread
/// </summary>
AutoResetEvent signal;
/// <summary>
/// Time to stop uploading
/// </summary>
DateTime stop;
/// <summary>
/// The Flickr instance
/// </summary>
Flickr Flickr;
/// <summary>
/// The Status form
/// </summary>
StatusForm Form;
public Uploader(StatusForm form) {
Form = form;
Flickr = form.Flickr = Form.NewFlickr();
if (Settings.Default.AccessToken != null) {
Flickr.OAuthAccessToken = Settings.Default.AccessToken.Token;
Flickr.OAuthAccessTokenSecret = Settings.Default.AccessToken.TokenSecret;
}
signal = new AutoResetEvent(false);
task = new Task(UploadPhotoList);
}
/// <summary>
/// Sets from the form (so if this thread runs on, it won't update the new Sets)
/// </summary>
public Dictionary<string, Set> Sets;
/// <summary>
/// AltSets from the form (so if this thread runs on, it won't update the new AltSets)
/// </summary>
public Dictionary<string, Set> AltSets;
void Log(string s) {
if (Settings.Default.WaitLogging) {
using (StreamWriter w = new StreamWriter("Wait.log", true))
w.WriteLine(DateTime.Now.ToString("s") + ":" + s);
}
}
public void Start() {
Form.Log("Start");
task.Start();
}
public void Stop() {
if (task != null) {
Form.Log("Stop");
signal.Set();
}
}
/// <summary>
/// The main thread
/// </summary>
void UploadPhotoList() {
// If a folder is just a date (yyyy-mm-dd), we don't upload it - we only do so if
// some text has been added to provide a meaningful name. This regex extracts the
// meaningful part of the name in group 1
Regex alt = new Regex(@"^[\d- ]*(.*?)$", RegexOptions.Compiled);
PowerModeChangedEventHandler eh = null;
if (Settings.Default.WaitLogging) {
Log("Adding power handler");
eh = new PowerModeChangedEventHandler(delegate(object sender, PowerModeChangedEventArgs e) {
Log("Power " + e.Mode);
});
SystemEvents.PowerModeChanged += eh;
}
try {
do {
try {
Form.Log("Starting");
Form.Sets = Sets = new Dictionary<string, Set>();
Form.AltSets = AltSets = new Dictionary<string, Set>();
Form.ClearFolders();
Form.LogError("");
foreach (string folder in Directory.EnumerateDirectories(Settings.Default.Folder)) {
if (TaskCancelled()) return;
Form.Status("Loading folder {0}", folder);
Set s = new Set(folder);
if (s.Photos.Count > 0) {
Match m = alt.Match(s.Folder.Name);
if (m.Success) {
// The folder name starts with a date
string key = m.Groups[1].Value;
if (key == "") {
Form.Error("Ignored folder {0} with no description", folder);
continue;
}
// Index set by name part only, in case date has been removed from Flickr set name
AltSets[key] = s;
}
Sets[s.Folder.Name] = s;
Form.AddFolder(s);
}
}
// Have finished building local folder list
Form.Status("");
// Now compute hashes
foreach (Set s in Sets.Values) {
if (TaskCancelled()) return;
Form.Status("Checking folder {0}", s.Folder.Name);
int i = 0;
foreach (PhotoInfo p in s.Photos) {
if (TaskCancelled()) return;
p.LoadHash();
Form.UpdatePhoto(s, i++);
}
}
Form.Status("");
if (Settings.Default.AccessToken == null) {
Form.Log("Not logged in");
return;
}
// Wait until start time (if a start time is specified)
if (Wait(false)) return;
// Prevent computer going to sleep during upload
uint executionState = NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);
if (executionState == 0)
Form.LogError("SetThreadExecutionState failed.");
try {
Form.Status("Downloading Flickr sets");
// Get Flickr sets
PhotosetCollection sets = Flickr.PhotosetsGetList(1, 1000);
if (TaskCancelled()) return;
foreach (Photoset set in sets) {
Set ours;
// Match Flickr sets to ours
if (Sets.TryGetValue(set.Title, out ours) || AltSets.TryGetValue(set.Title, out ours)) {
ours.Id = set.PhotosetId;
}
}
foreach (Set s in Sets.Values) {
if (TaskCancelled()) return;
// Photos matched in the Flickr set
HashSet<PhotoInfo> matched = new HashSet<PhotoInfo>();
bool changed = false;
if (s.Id != null) {
// There is a Flickr set - get Flickr's list of photos in the set
Form.Status("Downloading photo details for set {0}", s.Folder.Name);
foreach (Photo p in Flickr.PhotosetsGetPhotos(s.Id, PhotoSearchExtras.DateUploaded, 1, 1000)) {
if (TaskCancelled()) return;
// Match by Id, name, file name or description
PhotoInfo info = s.Photos.FirstOrDefault(pf => pf.Flickr.Id == p.PhotoId
|| pf.Name == p.Title
|| pf.FileName == p.Title
|| pf.Description == p.Description);
if (info != null) {
matched.Add(info);
if (info.Flickr.Uploaded != p.DateUploaded)
changed = true;
info.Flickr.Id = p.PhotoId;
info.Flickr.Uploaded = p.DateUploaded;
}
}
}
// Now look for local photos not in the Flickr set
foreach (PhotoInfo p in s.Photos) {
if (!matched.Contains(p)) {
if (p.Flickr.Id != null) {
// Photo is already on Flickr, but is not in this set
if (TaskCancelled()) return;
if (DateTime.Now >= stop) {
Form.Log("Reached stop time");
return;
}
try {
Flickr.PhotosetsAddPhoto(s.Id, p.Flickr.Id);
} catch {
// Cannot add photo - perhaps it was deleted from Flickr - mark it as missing
p.Flickr.Id = null;
p.Flickr.Uploaded = null;
changed = true;
}
}
}
}
if (changed)
Form.UpdateSet(s);
// Upload remaining photos
foreach (PhotoInfo p in s.Photos) {
if (TaskCancelled()) return;
if (p.Flickr.Id != null)
continue; // Photo is already on Flickr
if (DateTime.Now >= stop) {
Form.Log("Reached stop time");
return;
}
Form.Log("Uploading {0}", p.FullName);
try {
if (TaskCancelled()) return;
p.Flickr.Id = Flickr.UploadPicture(p.FullName, p.Name, p.Description, "", false, true, false);
p.Flickr.Uploaded = DateTime.Now;
if (changed)
Form.UpdatePhoto(s, s.Photos.IndexOf(p));
if (s.Id == null) {
Form.Log("Creating set {0}", s.Folder.Name);
s.Id = Flickr.PhotosetsCreate(s.Folder.Name, p.Flickr.Id).PhotosetId;
} else {
Flickr.PhotosetsAddPhoto(s.Id, p.Flickr.Id);
}
} catch (Exception ex) {
Form.LogError("Exception: {0}", ex);
}
}
}
Form.Status("");
Form.LogError("Completed");
} finally {
if (executionState != 0)
NativeMethods.SetThreadExecutionState(executionState);
}
} catch (Exception ex) {
try {
Form.LogError("Exception: {0}", ex);
} catch {
}
}
} while (!Wait(true));
} finally {
if (eh != null) {
Log("Removing power handler");
SystemEvents.PowerModeChanged -= eh;
}
}
}
/// <summary>
/// Check for stop signal
/// </summary>
bool TaskCancelled() {
if (signal.WaitOne(1)) {
Form.Status("Interrupted");
return true;
}
return false;
}
/// <summary>
/// Wait until start time (if specified)
/// </summary>
/// <param name="next">Wait until start time tomorrow</param>
/// <returns>true if stop signal was received</returns>
bool Wait(bool next) {
Log("Wait");
DateTime start = DateTime.Now;
// Have they specified to start and stop in the same day, and we have already passed stop time?
if (Settings.Default.Start < Settings.Default.Stop && start.Hour >= Settings.Default.Stop)
next = true;
if (next || start.Hour < Settings.Default.Start) {
string day = "";
start = new DateTime(start.Year, start.Month, start.Day, Settings.Default.Start, 0, 0);
if (next && start.Hour >= Settings.Default.Start) {
start = start.AddDays(1);
day = " on " + start.ToString("dddd");
}
TimeSpan delay = start - DateTime.Now;
Form.Log("Waiting until {0:HH:mm}{1}", start, day);
int msec = (int)delay.TotalMilliseconds;
while (start > DateTime.Now) {
delay = start - DateTime.Now;
msec = Math.Min((int)delay.TotalMilliseconds, 30 * 60 * 1000); // 30 mins
if (msec > 0 && signal.WaitOne(msec)) {
Log("Interrupted");
Form.Status("Interrupted");
return true;
}
Log("Tick");
}
}
// Have they specified a stop time?
stop = Settings.Default.Stop >= 24 ? DateTime.MaxValue : new DateTime(start.Year, start.Month, start.Day, Settings.Default.Stop, 0, 0);
if (stop < start)
stop = stop.AddDays(1);
Log("Wait ended");
return false;
}
}
internal static class NativeMethods {
// Import SetThreadExecutionState Win32 API and necessary flags
[DllImport("kernel32.dll")]
public static extern uint SetThreadExecutionState(uint esFlags);
public const uint ES_CONTINUOUS = 0x80000000;
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
}
}