aboutsummaryrefslogtreecommitdiffstats
path: root/web/command/handler/upload.go
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.net>2025-07-08 23:26:05 +0100
committerLeonardo Bishop <me@leonardobishop.net>2025-07-08 23:26:05 +0100
commitcdb75d3fcbc9339b897f8c6ff4d69a577f017393 (patch)
tree5e757cd236540c2cea9874c1bc09f19548db05d5 /web/command/handler/upload.go
parentb56101f1a11552067f594679a497ebd4cf7427d4 (diff)
Rewrite in Go
Diffstat (limited to 'web/command/handler/upload.go')
-rw-r--r--web/command/handler/upload.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/web/command/handler/upload.go b/web/command/handler/upload.go
new file mode 100644
index 0000000..0e4928c
--- /dev/null
+++ b/web/command/handler/upload.go
@@ -0,0 +1,49 @@
+package handler
+
+import (
+ "fmt"
+ "net/http"
+
+ "github.com/LMBishop/scrapbook/pkg/config"
+ "github.com/LMBishop/scrapbook/pkg/index"
+ "github.com/LMBishop/scrapbook/pkg/upload"
+ "github.com/LMBishop/scrapbook/web/command/html"
+ . "maragu.dev/gomponents"
+ ghttp "maragu.dev/gomponents/http"
+)
+
+func GetUpload(index *index.SiteIndex) func(http.ResponseWriter, *http.Request) {
+ return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) {
+ siteName := r.PathValue("site")
+ if siteName == "" {
+ return nil, fmt.Errorf("unknown site")
+ }
+ site := index.GetSite(siteName)
+ if site == nil {
+ return nil, fmt.Errorf("unknown site")
+ }
+
+ return html.UploadPage("", "", siteName), nil
+ })
+}
+
+func PostUpload(mainConfig *config.MainConfig, index *index.SiteIndex) func(http.ResponseWriter, *http.Request) {
+ return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) {
+ siteName := r.PathValue("site")
+ if siteName == "" {
+ return nil, fmt.Errorf("unknown site")
+ }
+
+ reader, err := r.MultipartReader()
+ if err != nil {
+ return html.UploadPage("", fmt.Errorf("Unexpected error: could not read stream: %w", err).Error(), siteName), nil
+ }
+
+ version, err := upload.HandleUpload(siteName, reader, index)
+ if err != nil {
+ return html.UploadPage("", fmt.Errorf("Unexpected error: %w", err).Error(), siteName), nil
+ }
+
+ return html.UploadPage(fmt.Sprintf("Version %s created successfully", version), "", siteName), nil
+ })
+}