CmdUpload.cs 6.6 KB

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