-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
71 lines (65 loc) · 1.84 KB
/
functions.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
package main
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/google/uuid"
)
type Filename struct {
Name string `json:"name" required:"true"`
}
func respond(w http.ResponseWriter, status int, obj map[string]string) {
w.WriteHeader(status)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(obj)
}
func SendFile(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
respond(w, http.StatusBadRequest, map[string]string{"status": "ERROR_INVALID_METHOD"})
return
}
var fileInfos Filename
err := json.NewDecoder(r.Body).Decode(&fileInfos)
if err != nil {
respond(w, http.StatusBadRequest, map[string]string{"status": "ERROR_INVALID_BODY"})
return
}
pathToFile := filepath.Join(".", "files", fileInfos.Name)
fileBytes, err := ioutil.ReadFile(pathToFile)
if err != nil {
respond(w, http.StatusBadRequest, map[string]string{"status": "ERROR_READING_FILE"})
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/octet-stream")
w.Write(fileBytes)
}
func ReceiveFile(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
respond(w, http.StatusBadRequest, map[string]string{"status": "ERROR_INVALID_METHOD"})
return
}
r.ParseMultipartForm(32 << 20) // limit max input length
var buf bytes.Buffer
file, _, err := r.FormFile("file")
if err != nil {
respond(w, http.StatusBadRequest, map[string]string{"status": "ERROR_NO_FILE"})
return
}
defer file.Close()
io.Copy(&buf, file)
id := uuid.New().String()
pathToFile := filepath.Join(".", "files", id)
f, err := os.Create(pathToFile)
if err != nil {
respond(w, http.StatusBadRequest, map[string]string{"status": "ERROR_FILE_CREATION"})
return
}
f.Write(buf.Bytes())
buf.Reset()
respond(w, http.StatusOK, map[string]string{"status": "OK", "name": id})
}