summaryrefslogtreecommitdiff
path: root/PublishMedia.cs
diff options
context:
space:
mode:
Diffstat (limited to 'PublishMedia.cs')
-rw-r--r--PublishMedia.cs61
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));
+ }
+ }
+}