CmdUpload.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Quadarax.Application.TemporarySharedStorage.Client.Dtos.Request;
  2. using Quadarax.Application.TemporarySharedStorage.Client.Services;
  3. using Quadarax.Foundation.Core.Value.Extensions;
  4. using Quadarax.Foundation.Core.Logging;
  5. using Quadarax.Foundation.Core.QConsole;
  6. using Quadarax.Foundation.Core.QConsole.Argument;
  7. using Quadarax.Foundation.Core.QConsole.Attributes;
  8. using Quadarax.Foundation.Core.QConsole.Value;
  9. using Quadarax.Foundation.Core.Value;
  10. namespace qdr.app.qbstack.Commands
  11. {
  12. [CommandDefinition]
  13. internal class CmdUpload : BaseCmd
  14. {
  15. #region *** Constants ***
  16. private const string CS_CMD_NAME = "upload";
  17. private const string CS_CMD_DESCR = "Upload a file to QDR Temporary Shared Storage.";
  18. private const string ARG_FILE_NAME = "filePath";
  19. private const string ARG_FILE_HINT = "file_path";
  20. private const string ARG_FILE_DESCR = "Path to the file to upload.";
  21. private const string ARG_DESCR_NAME = "descr";
  22. private const string ARG_DESCR_HINT = "description";
  23. private const string ARG_DESCR_DESCR = "Description of uploading file.";
  24. private const string ARG_PWD_NAME = "pwd";
  25. private const string ARG_PWD_HINT = "password";
  26. private const string ARG_PWD_DESCR = "Password for download file.";
  27. private const string ARG_MSG_NAME = "msg";
  28. private const string ARG_MSG_HINT = "message";
  29. private const string ARG_MSG_DESCR = "Message for download file.";
  30. private const string ARG_REF_NAME = "ref";
  31. private const string ARG_REF_HINT = "reference";
  32. private const string ARG_REF_DESCR = "Custom reference value for download file.";
  33. private const string ARG_AFROM_NAME = "activeFrom";
  34. private const string ARG_AFROM_HINT = "active_from";
  35. private const string ARG_AFROM_DESCR = "Date and time from which the file will be active (format: \"yyyy-MM-dd HH:mm:ss\").";
  36. private const string ARG_ATO_NAME = "activeTo";
  37. private const string ARG_ATO_HINT = "active_to";
  38. private const string ARG_ATO_DESCR = "Date and time until which the file will be active (format: \"yyyy-MM-dd HH:mm:ss\").";
  39. private const string ARG_CHSIZE_NAME = "chunckSize";
  40. private const string ARG_CHSIZE_HINT = "chunck_size";
  41. private const string ARG_CHSIZE_DESCR = "Size of chunks in bytes for uploading.";
  42. #endregion
  43. public override string Name => CS_CMD_NAME;
  44. public override string Description => CS_CMD_DESCR;
  45. protected string FileName {get;private set;} = string.Empty;
  46. protected string FileDescription {get;private set;} = string.Empty;
  47. protected string Message {get;private set;} = string.Empty;
  48. protected string Password {get;private set;} = string.Empty;
  49. protected string Reference {get;private set;} = string.Empty;
  50. protected int ChunckSize {get;private set;} = 1048576;
  51. protected DateTime ActiveFrom {get;private set;} = DateTime.MinValue;
  52. protected DateTime ActiveTo {get;private set;} = DateTime.MinValue;
  53. #region *** Constructors ***
  54. public CmdUpload(Engine engine) : base(engine)
  55. {
  56. }
  57. #endregion
  58. #region *** Overrides ***
  59. protected override void OnValidateArguments()
  60. {
  61. base.OnValidateArguments();
  62. var file = GetArgumentValueOrDefault<string>(ARG_FILE_NAME);
  63. if (!FileSystem.File.Exists(file))
  64. {
  65. throw new FileNotFoundException($"File not found: {file}",file);
  66. }
  67. FileName = file;
  68. FileDescription = GetArgumentValueOrDefault<string>(ARG_DESCR_NAME)!;
  69. Password = GetArgumentValueOrDefault<string>(ARG_PWD_NAME)!;
  70. Reference = GetArgumentValueOrDefault<string>(ARG_REF_NAME)!;
  71. Message = GetArgumentValueOrDefault<string>(ARG_MSG_NAME)!;
  72. ChunckSize = GetArgumentValueOrDefault<int>(ARG_CHSIZE_NAME);
  73. if (ChunckSize <= 0)
  74. {
  75. throw new ArgumentException($"Chunk size must be greater than 0.", ARG_CHSIZE_NAME);
  76. }
  77. ActiveFrom = GetArgumentValueOrDefault<DateTime>(ARG_AFROM_NAME);
  78. ActiveTo = GetArgumentValueOrDefault<DateTime>(ARG_ATO_NAME);
  79. if (ActiveFrom >= ActiveTo)
  80. {
  81. throw new ArgumentException($"Active from date must be earlier than active to date.", ARG_AFROM_NAME);
  82. }
  83. }
  84. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  85. {
  86. var list = base.OnSetupArguments().ToList();
  87. list.Add(new NamedArgument(ARG_FILE_NAME, ARG_FILE_DESCR, ARG_FILE_HINT, TypeValuesEnum.String, string.Empty, true));
  88. list.Add(new NamedArgument(ARG_DESCR_NAME, ARG_DESCR_DESCR, ARG_DESCR_HINT, TypeValuesEnum.String, string.Empty, false));
  89. list.Add(new NamedArgument(ARG_PWD_NAME, ARG_PWD_DESCR, ARG_PWD_HINT, TypeValuesEnum.String, string.Empty, true));
  90. list.Add(new NamedArgument(ARG_REF_NAME, ARG_REF_DESCR, ARG_REF_HINT, TypeValuesEnum.String, string.Empty, true));
  91. list.Add(new NamedArgument(ARG_AFROM_NAME, ARG_AFROM_DESCR, ARG_AFROM_HINT, TypeValuesEnum.DateTime, string.Empty, true));
  92. list.Add(new NamedArgument(ARG_ATO_NAME, ARG_ATO_DESCR, ARG_ATO_HINT, TypeValuesEnum.DateTime, string.Empty, true));
  93. list.Add(new NamedArgument(ARG_MSG_NAME, ARG_MSG_DESCR, ARG_MSG_HINT, TypeValuesEnum.String, string.Empty, true));
  94. list.Add(new NamedArgument(ARG_CHSIZE_NAME, ARG_CHSIZE_DESCR, ARG_CHSIZE_HINT, TypeValuesEnum.Integer, "0", false));
  95. return list;
  96. }
  97. protected override Result OnExecute()
  98. {
  99. var srv = new FileUploadService(Url.ToString(), ApiKey, new ConsoleLogger(LogSeverityEnum.Info, LogSeverityEnum.Debug, LogSeverityEnum.Error));
  100. var uploadRequest = new UploadFileRequest
  101. {
  102. FilePath = FileName,
  103. Description = FileDescription,
  104. Password = Password,
  105. Message = Message,
  106. Reference = Reference,
  107. ActiveFrom = ActiveFrom,
  108. ActiveTo = ActiveTo,
  109. ChunkSize = ChunckSize
  110. };
  111. Task.Run(() => srv.UploadFileAsync(uploadRequest)).Wait();
  112. return new Result();
  113. }
  114. #endregion
  115. }
  116. }