using Quadarax.Application.TemporarySharedStorage.Client.Dtos.Request; using Quadarax.Application.TemporarySharedStorage.Client.Services; using Quadarax.Foundation.Core.Logging; using Quadarax.Foundation.Core.QConsole; using Quadarax.Foundation.Core.QConsole.Argument; using Quadarax.Foundation.Core.QConsole.Attributes; using Quadarax.Foundation.Core.QConsole.Value; using Quadarax.Foundation.Core.Value; namespace qdr.app.qbstack.Commands { [CommandDefinition] internal class CmdUpload : BaseCmd { #region *** Constants *** private const string CS_CMD_NAME = "upload"; private const string CS_CMD_DESCR = "Upload a file to QDR Temporary Shared Storage."; private const string ARG_FILE_NAME = "file"; private const string ARG_FILE_HINT = "file"; private const string ARG_FILE_DESCR = "Path to the file to upload."; private const string ARG_DESCR_NAME = "descr"; private const string ARG_DESCR_HINT = "description"; private const string ARG_DESCR_DESCR = "Description of uploading file."; private const string ARG_PWD_NAME = "pwd"; private const string ARG_PWD_HINT = "password"; private const string ARG_PWD_DESCR = "Password for download file."; private const string ARG_MSG_NAME = "msg"; private const string ARG_MSG_HINT = "message"; private const string ARG_MSG_DESCR = "Message for download file."; private const string ARG_REF_NAME = "ref"; private const string ARG_REF_HINT = "reference"; private const string ARG_REF_DESCR = "Custom reference value for download file."; private const string ARG_AFROM_NAME = "activeFrom"; private const string ARG_AFROM_HINT = "active_from"; private const string ARG_AFROM_DESCR = "Date and time from which the file will be active (format: \"yyyy-MM-dd HH:mm:ss\")."; private const string ARG_ATO_NAME = "activeTo"; private const string ARG_ATO_HINT = "active_to"; private const string ARG_ATO_DESCR = "Date and time until which the file will be active (format: \"yyyy-MM-dd HH:mm:ss\")."; private const string ARG_CHSIZE_NAME = "chunckSize"; private const string ARG_CHSIZE_HINT = "chunck_size"; private const string ARG_CHSIZE_DESCR = "Size of chunks in bytes for uploading."; #endregion public override string Name => CS_CMD_NAME; public override string Description => CS_CMD_DESCR; protected string FileName {get;private set;} = string.Empty; protected string FileDescription {get;private set;} = string.Empty; protected string Message {get;private set;} = string.Empty; protected string Password {get;private set;} = string.Empty; protected string Reference {get;private set;} = string.Empty; protected int ChunckSize {get;private set;} = 0; protected DateTime ActiveFrom {get;private set;} = DateTime.MinValue; protected DateTime ActiveTo {get;private set;} = DateTime.MinValue; #region *** Constructors *** public CmdUpload(Engine engine) : base(engine) { } #endregion #region *** Overrides *** protected override void OnValidateArguments() { base.OnValidateArguments(); var file = GetArgumentValueOrDefault(ARG_FILE_NAME); if (!FileSystem.File.Exists(file)) { throw new FileNotFoundException($"File not found: {file}",file); } FileName = file; FileDescription = GetArgumentValueOrDefault(ARG_DESCR_NAME)!; Password = GetArgumentValueOrDefault(ARG_PWD_NAME)!; Reference = GetArgumentValueOrDefault(ARG_REF_NAME)!; Message = GetArgumentValueOrDefault(ARG_MSG_NAME)!; ChunckSize = GetArgumentValueOrDefault(ARG_CHSIZE_NAME); if (ChunckSize <= 0) { throw new ArgumentException($"Chunk size must be greater than 0.", ARG_CHSIZE_NAME); } ActiveFrom = GetArgumentValueOrDefault(ARG_AFROM_NAME); ActiveTo = GetArgumentValueOrDefault(ARG_ATO_NAME); if (ActiveFrom >= ActiveTo) { throw new ArgumentException($"Active from date must be earlier than active to date.", ARG_AFROM_NAME); } } protected override IEnumerable OnSetupArguments() { var list = base.OnSetupArguments().ToList(); list.Add(new NamedArgument(ARG_FILE_NAME, ARG_FILE_DESCR, ARG_FILE_HINT, TypeValuesEnum.String, string.Empty, true)); list.Add(new NamedArgument(ARG_DESCR_NAME, ARG_DESCR_DESCR, ARG_DESCR_HINT, TypeValuesEnum.String, string.Empty, false)); list.Add(new NamedArgument(ARG_PWD_NAME, ARG_PWD_DESCR, ARG_PWD_HINT, TypeValuesEnum.String, string.Empty, true)); list.Add(new NamedArgument(ARG_REF_NAME, ARG_REF_DESCR, ARG_REF_HINT, TypeValuesEnum.String, string.Empty, true)); list.Add(new NamedArgument(ARG_AFROM_NAME, ARG_AFROM_DESCR, ARG_AFROM_HINT, TypeValuesEnum.DateTime, string.Empty, true)); list.Add(new NamedArgument(ARG_ATO_NAME, ARG_ATO_DESCR, ARG_ATO_HINT, TypeValuesEnum.DateTime, string.Empty, true)); list.Add(new NamedArgument(ARG_MSG_NAME, ARG_MSG_DESCR, ARG_MSG_HINT, TypeValuesEnum.String, string.Empty, true)); list.Add(new NamedArgument(ARG_CHSIZE_NAME, ARG_CHSIZE_DESCR, ARG_CHSIZE_HINT, TypeValuesEnum.Integer, "1048576", false)); return list; } protected override Result OnExecute() { var srv = new FileUploadService(Url.ToString(), ApiKey, new ConsoleLogger()); var uploadRequest = new UploadFileRequest { FilePath = FileName, Description = FileDescription, Password = Password, Message = Message, Reference = Reference, ActiveFrom = ActiveFrom, ActiveTo = ActiveTo, ChunkSize = ChunckSize }; var tResult = Task.Run(() => srv.UploadFileAsync(uploadRequest)); tResult.Wait(); this.Log.Info("File upload result: " + tResult.Result.Item2); return new Result(tResult.Result.Item2 == TssResultEnum.Success); } #endregion } }