vStationVidData.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.IO;
  3. using Vinno.IUS.Common.Log;
  4. using Vinno.vCloud.Common.FIS.Remedicals;
  5. namespace Vinno.vCloud.Common.FIS.vStation
  6. {
  7. public class vStationVidData : IDisposable
  8. {
  9. private UploadStatus _status;
  10. private bool _disposed;
  11. /// <inheritdoc />
  12. /// <summary>
  13. /// Gets the id of this vStation Vid Data
  14. /// </summary>
  15. public string Id { get; }
  16. /// <inheritdoc />
  17. /// <summary>
  18. /// Gets the vid file path.
  19. /// </summary>
  20. public string VidFilePath { get; private set; }
  21. /// <summary>
  22. /// Gets the store file path.
  23. /// </summary>
  24. public string StoreFilePath { get; private set; }
  25. /// <summary>
  26. /// Raised when the status changed.
  27. /// </summary>
  28. public event EventHandler StatusChanged;
  29. /// <inheritdoc />
  30. /// <summary>
  31. /// Gets the status of this Scan data.
  32. /// </summary>
  33. public UploadStatus Status
  34. {
  35. get => _status;
  36. internal set
  37. {
  38. if (_status != value)
  39. {
  40. var old = _status;
  41. _status = value;
  42. if (_status == UploadStatus.Uploaded || _status == UploadStatus.Deleted)
  43. {
  44. Delete(old);
  45. }
  46. else
  47. {
  48. Move(old, _status);
  49. }
  50. OnStatusChanged();
  51. }
  52. }
  53. }
  54. public vStationVidData(string id, string vidFilePath, UploadStatus status)
  55. {
  56. _status = status;
  57. Id = id;
  58. VidFilePath = vidFilePath;
  59. Save();
  60. }
  61. ~vStationVidData()
  62. {
  63. DoDispose();
  64. }
  65. private void OnStatusChanged()
  66. {
  67. StatusChanged?.Invoke(this, EventArgs.Empty);
  68. }
  69. public void Delete()
  70. {
  71. Status = UploadStatus.Deleted;
  72. }
  73. /// <summary>
  74. /// Force update the vStation VID data's status.
  75. /// </summary>
  76. /// <param name="status">The new status</param>
  77. public void ForceUpdateStatus(UploadStatus status)
  78. {
  79. Status = status;
  80. }
  81. public void Update()
  82. {
  83. Save();
  84. }
  85. private string GetStoreFilePath(UploadStatus status)
  86. {
  87. if (status == UploadStatus.Deleted)
  88. {
  89. throw new InvalidOperationException("File already deleted.");
  90. }
  91. var subFolder = string.Empty;
  92. if (status == UploadStatus.Idle || status == UploadStatus.Waiting || status == UploadStatus.Uploading)
  93. {
  94. subFolder = "Upload";
  95. }
  96. if (status == UploadStatus.Fail)
  97. {
  98. subFolder = "Failed";
  99. }
  100. var folder = GetWorkingFolder(subFolder);
  101. var fileName = $"{Id}.vd";
  102. return Path.Combine(folder, fileName);
  103. }
  104. private string GetWorkingFolder(string subFolder)
  105. {
  106. var folder = Path.Combine(vStation.FolderPath, "vStation", subFolder);
  107. if (!Directory.Exists(folder))
  108. {
  109. Directory.CreateDirectory(folder);
  110. }
  111. return folder;
  112. }
  113. private void Delete(UploadStatus status)
  114. {
  115. var storeFilePath = GetStoreFilePath(status);
  116. if (File.Exists(storeFilePath))
  117. {
  118. File.Delete(storeFilePath);
  119. }
  120. }
  121. private void Save()
  122. {
  123. StoreFilePath = GetStoreFilePath(Status);
  124. this.Serialize(StoreFilePath);
  125. }
  126. private void Move(UploadStatus source, UploadStatus dest)
  127. {
  128. var sourceStoreFilePath = GetStoreFilePath(source);
  129. var destStoreFilePath = GetStoreFilePath(dest);
  130. if (File.Exists(sourceStoreFilePath) && !File.Exists(destStoreFilePath))
  131. {
  132. File.Move(sourceStoreFilePath, destStoreFilePath);
  133. StoreFilePath = destStoreFilePath;
  134. }
  135. }
  136. private void DoDispose()
  137. {
  138. try
  139. {
  140. if (!_disposed)
  141. {
  142. //if not uploaded just save it to disk.
  143. if (Status != UploadStatus.Uploaded && Status != UploadStatus.Deleted)
  144. {
  145. Save();
  146. }
  147. }
  148. }
  149. catch (Exception ex)
  150. {
  151. Logger.WriteLineError($"vCloudScanData: dispose upload data error:{ex}");
  152. }
  153. finally
  154. {
  155. _disposed = true;
  156. }
  157. }
  158. public void Dispose()
  159. {
  160. DoDispose();
  161. GC.SuppressFinalize(this);
  162. }
  163. }
  164. }