From 440a556ade3a52dd94a8a4a4d446c187df3754e1 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Thu, 9 Jul 2026 10:37:42 -0700 Subject: Add an API upload --- roseh.moe.go | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'roseh.moe.go') diff --git a/roseh.moe.go b/roseh.moe.go index c831b01..51caf17 100644 --- a/roseh.moe.go +++ b/roseh.moe.go @@ -398,13 +398,9 @@ func uploadWormhole(reader *multipart.Reader) (string, error) { if part.FormName() != "file" || part.FileName() == "" { continue } - hole, holeWriter, err := writeHoleFile(part.FileName()) + hole, err := writeSingleHoleFile(part.FileName(), part) if err != nil { - return "", fmt.Errorf("open hole file: %s", err) - } - _, err = io.Copy(holeWriter, part) - if err := cmp.Or(err, holeWriter.Close()); err != nil { - return "", fmt.Errorf("write hole file: %s", err) + return "", err } holes = append(holes, hole) } @@ -481,6 +477,32 @@ func wormholeUpload(w http.ResponseWriter, r *http.Request) { } } +func writeSingleHoleFile(filename string, r io.Reader) (string, error) { + hole, holeWriter, err := writeHoleFile(filename) + if err != nil { + return "", fmt.Errorf("write hole file: %s", err) + } + defer holeWriter.Close() + if _, err := io.Copy(holeWriter, r); err != nil { + return "", fmt.Errorf("write hole file: %s", err) + } + if err := holeWriter.Close(); err != nil { + return "", fmt.Errorf("write hole file: %s", err) + } + return hole, nil +} + +func wormholeAPIUpload(w http.ResponseWriter, r *http.Request) { + hole, err := writeSingleHoleFile(r.PathValue("name"), r.Body) + if err != nil { + log.Printf("Failed to write hole file: %s", err) + http.Error(w, "Failed to write hole file", http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "text/plain") + fmt.Fprintf(w, "%s/wormhole/%s", *selfURL, hole) +} + func wormholeDownload(w http.ResponseWriter, r *http.Request) { if err := readHoleFile(w, r, r.PathValue("hole")); err != nil { if err == errNoHole { @@ -830,6 +852,7 @@ func main() { mux.Handle("GET /code/", http.StripPrefix("/code/", http.FileServer(http.Dir(*codeDir)))) mux.HandleFunc("GET /static/", static) mux.HandleFunc("GET /favicon.ico", favicon) + mux.HandleFunc("POST /api/wormhole/{name}", wormholeAPIUpload) mux.HandleFunc("GET /", notFound) http.Handle("/", http.NewCrossOriginProtection().Handler(mux)) jellyfinURL, err := url.Parse("http://127.0.0.1:8096") -- cgit v1.3.1