Skip to content

Commit

Permalink
add http example
Browse files Browse the repository at this point in the history
  • Loading branch information
mazrean committed Mar 7, 2024
1 parent f4f314b commit fcf75c3
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions example/http-upload/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"

"github.com/mazrean/formstream"
httpform "github.com/mazrean/formstream/http"
)

const iconDir = "icons"

func main() {
err := os.MkdirAll(iconDir, 0755)
if err != nil {
panic(err)
}

mux := http.NewServeMux()

mux.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}

parser, err := httpform.NewParser(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

err = parser.Register("icon", func(r io.Reader, header formstream.Header) error {
if header.ContentType() != "image/png" {
http.Error(w, "content type is not supported", http.StatusBadRequest)
return fmt.Errorf("content type is not supported")
}

id, _, _ := parser.Value("id")
iconPath := filepath.Join(iconDir, id)

_, err := os.Stat(iconPath)
if err != nil && !os.IsNotExist(err) {
http.Error(w, "failed to check file existence", http.StatusInternalServerError)
return fmt.Errorf("failed to check file existence: %w", err)
}

if err == nil {
http.Error(w, "user already exists", http.StatusConflict)
return fmt.Errorf("user already exists")
}

file, err := os.Create(iconPath)
if err != nil {
http.Error(w, "failed to create file", http.StatusInternalServerError)
return fmt.Errorf("failed to create file: %w", err)
}
defer file.Close()

_, err = io.Copy(file, r)
if err != nil {
http.Error(w, "failed to copy", http.StatusInternalServerError)
return fmt.Errorf("failed to copy: %w", err)
}

return nil
}, formstream.WithRequiredPart("id"))
if err != nil {
http.Error(w, "failed to register hook", http.StatusInternalServerError)
return
}

err = parser.Parse()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

w.WriteHeader(http.StatusCreated)
})
mux.Handle("/icons/", http.StripPrefix("/icons/", http.FileServer(http.Dir(iconDir))))

err = http.ListenAndServe(":8080", mux)
if err != nil {
panic(err)
}
}

0 comments on commit fcf75c3

Please sign in to comment.