UploadContent.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using FISLib.Vid;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using Vinno.FIS.Sonopost.Common;
  7. namespace Vinno.FIS.Sonopost.Features.Dicom
  8. {
  9. public class UploadContent
  10. {
  11. /// <summary>
  12. /// Get file path.
  13. /// </summary>
  14. public string UploadFilePath { get; }
  15. /// <summary>
  16. /// Get vid type.
  17. /// </summary>
  18. public FISVidType VidType { get; }
  19. /// <summary>
  20. /// Get id.
  21. /// </summary>
  22. public string Id { get; }
  23. /// <summary>
  24. /// Get date.
  25. /// </summary>
  26. public string Date { get; }
  27. public bool IsVidFile { get; }
  28. public string ExamRecordId { get; }
  29. private UploadContent(string uploadFilePath, FISVidType vidType, string id, string date, bool isVidFile, string examRecordId)
  30. {
  31. UploadFilePath = uploadFilePath;
  32. VidType = vidType;
  33. Id = id;
  34. Date = date;
  35. IsVidFile = isVidFile;
  36. ExamRecordId = examRecordId;
  37. }
  38. /// <summary>
  39. /// Parse upload content.
  40. /// </summary>
  41. /// <param name="filePath">Dicom file path.</param>
  42. /// <returns>Upload content.</returns>
  43. public static UploadContent Parse(string filePath)
  44. {
  45. var fileInfo = new FileInfo(filePath);
  46. var fileName = fileInfo.Name;
  47. var names = fileName.Split('.');
  48. if (!ValidateFileNames(names))
  49. {
  50. return null;
  51. }
  52. names = names[0].Split('_');
  53. var dataType = names[1];
  54. bool isVid = false;
  55. string examRecordId = string.Empty;
  56. if (names.Length == 3)
  57. {
  58. isVid = false;
  59. }
  60. else if (names.Length == 4)
  61. {
  62. isVid = true;
  63. examRecordId = names[3];
  64. }
  65. else
  66. {
  67. return null;
  68. }
  69. if (string.IsNullOrWhiteSpace(dataType) || string.IsNullOrWhiteSpace(names[2]))
  70. {
  71. return null;
  72. }
  73. return Enum.TryParse(dataType, true, out FISVidType vidType)
  74. ? new UploadContent(filePath, vidType, names[0], names[2], isVid, examRecordId)
  75. : null;
  76. }
  77. private static bool ValidateFileNames(IReadOnlyList<string> names)
  78. {
  79. return names.Count == 2 && names[1] == SonopostConstants.VidFileName && names[0].Contains('_');
  80. }
  81. }
  82. }