extraction.go 897 Bytes
package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"strings"

	"github.com/google/uuid"
)

func (app *App) PostExtractions(w http.ResponseWriter, r *http.Request) {
	userNo := r.Context().Value(PropUserNo).(uint64)
	r.ParseMultipartForm(32 << 20)

	form, _, err := r.FormFile("file")
	if err != nil {
		WriteError(w, http.StatusInternalServerError, "Unknown error")
		return
	}

	defer form.Close()

	dir := fmt.Sprintf("data/%d", userNo)
	os.MkdirAll(dir, 0644)

	name := strings.Replace(uuid.New().String(), "-", "", -1)
	file, err := os.Create(fmt.Sprintf("%s/%s", dir, name))
	if err != nil {
		WriteError(w, http.StatusInternalServerError, "Unknown error")
		return
	}
	defer file.Close()

	_, err = io.Copy(file, form)
	if err != nil {
		WriteError(w, http.StatusInternalServerError, "Unknown error")
		return
	}

	w.Write([]byte("success"))
}