private bool Upload(PhotoModel item) { if (!string.IsNullOrEmpty(item.PhotoId)) { return true; } using (var stream = new ThrottledStream(File.OpenRead(item.LocalPath))) { var fname = item.Filename; if (string.IsNullOrEmpty(item.Title)) item.Title = fname; try { var tags = item.Tags == null ? null : "\"" + string.Join("\",\"", item.Tags) + "\""; var f = _mgr.Surrogate; //https://www.flickr.com/services/api/upload.api.html item.PhotoId = f.UploadPicture(stream, fname, item.Title, item.Description, tags, item.IsPublic, item.IsFamily, item.IsFriend, item.ContentType, item.SafetyLevel, item.SearchState); Log($"{item.Filename} uploaded (remote ID={item.PhotoId})"); } catch (OperationCanceledException) { } catch (FlickrApiException ex) { //Filetype was not recognised (5) if (ex.Code == 5) { item.AddError($"File not recognized by Flickr (file corrupt?): {ex.Message}", ex); item.State = ProcessingStateType.Failed; return false; } item.AddError($"Error uploading photo: {ex.Message}", ex); return false; } catch (Exception ex) { item.AddError($"Error uploading photo: {ex.Message}", ex); return false; } } return true; }
private void AddToSets(PhotoModel item) { var f = _mgr.Surrogate; foreach (var set in item.Sets) { try { var added = false; if (string.IsNullOrEmpty(set.Id)) { //multithreaded! don't create album multiple times! lock (set) { if (string.IsNullOrEmpty(set.Id)) { Log($"Creating new Set '{set.Title}'"); var newSet = f.PhotosetsCreate(set.Title, item.PhotoId); set.Id = newSet.PhotosetId; Log($"Created new Set '{set.Title}'"); added = true; } } } if (!added) { f.PhotosetsAddPhoto(set.Id, item.PhotoId); } Log($"Added {item.Filename} to Set '{set.Title}'", item); } catch (FlickrApiException ex) { //Photo already in set (3) if (ex.Code == 3) return; } catch (OperationCanceledException) { } catch (Exception ex) { item.AddError($"Error adding photo to Set {set.Title}: {ex.Message}", ex); return; } } }
private void ProcessItem(PhotoModel item) { //Parallel.ForEach reuses the threads, and you can't change the name once set! //Thread.CurrentThread.Name = $"UploadManager.ProcessItem({item.LocalPath}) background thread"; var token = _cancellationToken; try { token.ThrowIfCancellationRequested(); if (!ShouldProcess(item)) return; Debug.WriteLine($"Processing {item.Filename} ({item.State})..."); item.LogStateChange = true; EnsureItemProperties(item); if (item.State == ProcessingStateType.Pending) { if (CheckForDuplicates && CheckIsDuplicate(item)) item.State = ProcessingStateType.Duplicate; else item.State = ProcessingStateType.ReadyToUpload; } if (CheckOnly) { item.State = ProcessingStateType.Completed; return; } token.ThrowIfCancellationRequested(); if (item.State == ProcessingStateType.ReadyToUpload) { if (UpdateOnly) item.State = ProcessingStateType.Completed; else { token.ThrowIfCancellationRequested(); item.State = ProcessingStateType.Uploading; if (Upload(item)) item.State = ProcessingStateType.Uploaded; } } if (item.State == ProcessingStateType.Uploaded) { AddToSets(item); FixDateTakenIfNotInExif(item); item.State = ProcessingStateType.Completed; } if (item.State == ProcessingStateType.Duplicate) { if (UpdateDuplicates) { UpdateItem(item); AddToSets(item); FixDateTakenIfNotInExif(item); } item.State = ProcessingStateType.Completed; } } catch (OperationCanceledException) { } catch (Exception ex) { item.AddError(ex.Message, ex); } finally { item.LogStateChange = false; Debug.WriteLine($"Completed {item.Filename} in state ({item.State})"); switch (item.State) { case ProcessingStateType.Failed: case ProcessingStateType.Retry: Log($"{item.Filename} completed in state {item.State}"); break; //case ProcessingStateType.Duplicate: //case ProcessingStateType.Completed: //case ProcessingStateType.Pending: //case ProcessingStateType.ReadyToUpload: //case ProcessingStateType.Uploading: //case ProcessingStateType.Uploaded: //default: // break; } } }