From 7984d2ecd4bdba7061429f98fdfae88f44f4a358 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Fri, 25 Aug 2023 15:47:14 +1000 Subject: v0.13a --- PublishMedia.cs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 PublishMedia.cs (limited to 'PublishMedia.cs') diff --git a/PublishMedia.cs b/PublishMedia.cs new file mode 100644 index 0000000..523b00f --- /dev/null +++ b/PublishMedia.cs @@ -0,0 +1,61 @@ +using HyperBooru.ApiClient; +using System.Management.Automation; + +namespace HyperBooru.PowerShell; + +[Alias("pbhbm")] +[Cmdlet(VerbsData.Publish, "HyperBooruMedia")] +public class PublishMediaCmdlet : PSCmdlet { + [Parameter(Position = 0, Mandatory = true)] + public HBSession Session { get; set; } + + [Parameter(Position = 1, Mandatory = true, ValueFromPipeline = true)] + public string Path { get; set; } + + [Parameter(Position = 2)] + public SwitchParameter Delete { get; set; } + + [Parameter(Position = 3)] + public Guid[] TagId { get; set; } = Array.Empty(); + + protected override void ProcessRecord() { + Path = System.IO.Path.GetFullPath( + Path, + SessionState.Path.CurrentLocation.Path); + + ApiModels.Media media; + + try { + media = Session.Media.UploadAsync(Path, TagId, !TagId.Any()) + .GetAwaiter() + .GetResult(); + } catch(Exception e) { + ThrowTerminatingError( + new ErrorRecord( + e, + "UploadFile", + ErrorCategory.WriteError, + Path)); + return; + } + + WriteVerbose($"Uploaded file: {Path}"); + + WriteObject(media); + + if(!Delete) + return; + + try { + File.Delete(Path); + WriteVerbose($"Deleted file: {Path}"); + } catch(Exception e) { + ThrowTerminatingError( + new ErrorRecord( + e, + "DeleteFile", + ErrorCategory.InvalidResult, + Path)); + } + } +} -- cgit v1.3