vStationVidData.cs 4.9 KB

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