forked from play-with-docker/play-with-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_upload.go
78 lines (66 loc) · 1.56 KB
/
file_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
package handlers
import (
"io"
"log"
"net/http"
"path/filepath"
"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/storage"
)
func FileUpload(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
sessionId := vars["sessionId"]
instanceName := vars["instanceName"]
s, err := core.SessionGet(sessionId)
if err == storage.NotFoundError {
rw.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
i := core.InstanceGet(s, instanceName)
// allow up to 32 MB which is the default
// has a url query parameter, ignore body
if url := req.URL.Query().Get("url"); url != "" {
_, fileName := filepath.Split(url)
err := core.InstanceUploadFromUrl(i, fileName, "", req.URL.Query().Get("url"))
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
rw.WriteHeader(http.StatusOK)
return
} else {
red, err := req.MultipartReader()
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusBadRequest)
return
}
path := req.URL.Query().Get("path")
for {
p, err := red.NextPart()
if err == io.EOF {
break
}
if err != nil {
log.Println(err)
continue
}
if p.FileName() == "" {
continue
}
err = core.InstanceUploadFromReader(i, p.FileName(), path, p)
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
log.Printf("Uploaded [%s] to [%s]\n", p.FileName(), i.Name)
}
rw.WriteHeader(http.StatusOK)
return
}
}