PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ZebraFlickr/Core/Processing/FlickrUploader.cs

https://bitbucket.org/garethl/zebraflickr
C# | 170 lines | 113 code | 31 blank | 26 comment | 7 complexity | 77bdc36a51e30ed4f6f12f86282565fc MD5 | raw file
  1. #region License
  2. // Copyright (c) 2012 Gareth Lennox (garethl@dwakn.com)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Gareth Lennox nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. using System;
  29. using System.Diagnostics;
  30. using System.IO;
  31. using System.Threading;
  32. using FlickrNet;
  33. using ZebraFlickr.Core.Store;
  34. namespace ZebraFlickr.Core.Processing
  35. {
  36. public class FlickrUploader
  37. {
  38. private readonly Flickr _flickr;
  39. private readonly string _root;
  40. private readonly LocalState _state;
  41. private string _currentFile;
  42. private Stopwatch _currentTime;
  43. public FlickrUploader(string root, LocalState state, UserDetails details)
  44. {
  45. _root = root;
  46. _state = state;
  47. _flickr = FlickrClient.Create(details);
  48. _flickr.OnUploadProgress += OnFlickrOnOnUploadProgress;
  49. }
  50. private void OnFlickrOnOnUploadProgress(object sender, UploadProgressEventArgs e)
  51. {
  52. var bytesPerSecond = e.BytesSent/(double) _currentTime.ElapsedMilliseconds*1000;
  53. if (CancelHandler.Cancelled)
  54. throw new OperationCanceledException();
  55. ConsoleRenderer.RenderConsoleProgress(e.ProcessPercentage, _currentFile, (int)e.BytesSent, (int)e.TotalBytesToSend, _currentTime.Elapsed);
  56. }
  57. public FileEntry Execute(FileEntry entry)
  58. {
  59. if (entry.Uploaded)
  60. {
  61. Log.Debug("Upload: Ignoring file {0}", entry.Name);
  62. return entry;
  63. }
  64. if (entry.Skip)
  65. {
  66. Log.Warn("Skipping previously errored file {0}", entry.Name);
  67. return entry;
  68. }
  69. Log.Info("Upload: Uploading {0}", entry.Name);
  70. var file = Path.Combine(_root, entry.Name);
  71. var tags = TagCreator.Create(entry);
  72. int count = 0;
  73. while (true)
  74. {
  75. try
  76. {
  77. _currentFile = entry.Name;
  78. _currentTime = Stopwatch.StartNew();
  79. ConsoleRenderer.EnsureConsoleSpace();
  80. var pictureId = _flickr.UploadPicture(file,
  81. null,
  82. null,
  83. string.Join(",", tags),
  84. _state.Config.Public,
  85. _state.Config.Family,
  86. _state.Config.Friend);
  87. _currentTime.Stop();
  88. ConsoleRenderer.RenderConsoleProgress(100, '■', ConsoleColor.Green, "");
  89. Console.WriteLine();
  90. Console.WriteLine();
  91. entry.PictureId = pictureId;
  92. entry.Uploaded = true;
  93. entry.UploadedMetaData = true;
  94. entry.ContentChanged = false;
  95. entry.MetaDataChanged = false;
  96. _state.UpdateEntry(entry);
  97. var totalBytes = new FileInfo(file).Length;
  98. var bytesPerSecond = totalBytes/(double) _currentTime.ElapsedMilliseconds*1000;
  99. Log.Info("Upload: Completed {0}. {1} / second. {2} in total.", entry.Name, bytesPerSecond.ToFormattedByteCountString(2), totalBytes.ToFormattedByteCountString(2));
  100. return entry;
  101. }
  102. catch (ThreadAbortException)
  103. {
  104. //nothing
  105. break;
  106. }
  107. catch (FlickrApiException ex)
  108. {
  109. if (CancelHandler.Cancelled)
  110. break;
  111. Console.WriteLine();
  112. Console.WriteLine();
  113. Log.Error("Error received from flickr. Skipping file and continuing", ex);
  114. _state.SkipEntry(entry);
  115. break;
  116. }
  117. catch (Exception ex)
  118. {
  119. if (CancelHandler.Cancelled)
  120. break;
  121. Console.WriteLine();
  122. Console.WriteLine();
  123. Log.Error("Error uploading {0}. Trying again", ex, entry.Name);
  124. count++;
  125. if (count >= 3)
  126. {
  127. Log.Error("Attempted 3 times to upload, skipping this file temporarily.");
  128. return entry;
  129. }
  130. }
  131. finally
  132. {
  133. _currentTime.Stop();
  134. }
  135. }
  136. return entry;
  137. }
  138. }
  139. }