blossom: upload from stdin.

This commit is contained in:
fiatjaf
2025-05-14 23:36:45 -03:00
parent 150625ee74
commit 4eb5e929d4

View File

@@ -1,8 +1,10 @@
package main package main
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"io"
"os" "os"
"fiatjaf.com/nostr" "fiatjaf.com/nostr"
@@ -73,22 +75,44 @@ var blossomCmd = &cli.Command{
return err return err
} }
hasError := false if isPiped() {
for _, fpath := range c.Args().Slice() { // get file from stdin
bd, err := client.UploadFilePath(ctx, fpath) if c.Args().Len() > 0 {
return fmt.Errorf("do not pass arguments when piping from stdin")
}
data, err := io.ReadAll(os.Stdin)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err) return fmt.Errorf("failed to read stdin: %w", err)
hasError = true }
continue
bd, err := client.UploadBlob(ctx, bytes.NewReader(data), "")
if err != nil {
return err
} }
j, _ := json.Marshal(bd) j, _ := json.Marshal(bd)
stdout(string(j)) stdout(string(j))
} else {
// get filenames from arguments
hasError := false
for _, fpath := range c.Args().Slice() {
bd, err := client.UploadFilePath(ctx, fpath)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
hasError = true
continue
}
j, _ := json.Marshal(bd)
stdout(string(j))
}
if hasError {
os.Exit(3)
}
} }
if hasError {
os.Exit(3)
}
return nil return nil
}, },
}, },