diff options
| author | Jake Mannens <jake@asger.xyz> | 2023-08-25 15:47:14 +1000 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2026-05-06 04:21:54 +1000 |
| commit | 7984d2ecd4bdba7061429f98fdfae88f44f4a358 (patch) | |
| tree | 70dcda52dbd0cd955a1857442ceb0e0ae1f189b2 /PublishMedia.cs | |
v0.13av0.13a
Diffstat (limited to 'PublishMedia.cs')
| -rw-r--r-- | PublishMedia.cs | 61 |
1 files changed, 61 insertions, 0 deletions
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<Guid>(); + + 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)); + } + } +} |
