extraction.go
854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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"))
}