This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_upload.go
83 lines (68 loc) · 1.76 KB
/
handler_upload.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"encoding/csv"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"strconv"
)
func handlerUpload(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
if _, err := io.WriteString(w, "Invalid request"); err != nil {
log.Error(err)
}
return
}
// Parse our multipart form, 10 << 20 specifies a maximum
// upload of 200 MB files.
if err := r.ParseMultipartForm(200 << 20); err != nil {
log.Warn(err)
return
}
// FormFile returns the first file for the given key `myFile`
// it also returns the FileHeader so we can get the Filename,
// the Header and the size of the file
file, handler, err := r.FormFile("datei")
if err != nil {
log.Warn("Error Retrieving the File: ", err)
return
}
defer file.Close()
log.Debugf("Uploaded File: %+v\n", handler.Filename)
log.Debugf("File Size: %+v\n", handler.Size)
log.Debugf("MIME Header: %+v\n", handler.Header)
csvReader := csv.NewReader(file)
// List of new persons
var persons []Person
for {
// Read each record from csv
record, err := csvReader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Println(err)
return
}
// Parse the group number into an integer and return on errors
groupNum, err := strconv.Atoi(record[1])
if err != nil {
log.Warn(err)
return
}
// Try to create a new persion object from the data and return on
// errors
p, err := NewPerson(0, groupNum, record[1], false)
if err != nil {
log.Warn(err)
return
}
// If everything went well until here, add the persion to the list of new persons
persons = append(persons, p)
}
// Add the list of new persons to the bridge/database
if err := bridge.AddPersons(persons); err != nil {
log.Error(err)
// TODO show an error in the UI
}
}