This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
storage.go
63 lines (50 loc) · 1.41 KB
/
storage.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
package staticbackend
import (
"net/http"
"github.com/staticbackendhq/core/backend"
"github.com/staticbackendhq/core/middleware"
)
func upload(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(32 << 20); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
conf, auth, err := middleware.Extract(r, true)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
file, h, err := r.FormFile("file")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// check for file size
// TODO: This should be based on current plan
if h.Size/(1000*1000) > 150 {
http.Error(w, "file size exeeded your limit", http.StatusBadRequest)
return
}
name := r.Form.Get("name")
fileSvc := backend.Storage(auth, conf)
savedFile, err := fileSvc.Save(h.Filename, name, file, h.Size)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respond(w, http.StatusOK, savedFile)
}
func deleteFile(w http.ResponseWriter, r *http.Request) {
conf, auth, err := middleware.Extract(r, true)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fileID := r.URL.Query().Get("id")
fileSvc := backend.Storage(auth, conf)
if err := fileSvc.Delete(fileID); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respond(w, http.StatusOK, true)
}