File size: 943 Bytes
7b9f3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package handlers

import (
	"encoding/base64"
	"io"
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

func file(rw http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	sessionId := vars["sessionId"]
	instanceName := vars["instanceName"]

	query := req.URL.Query()

	path := query.Get("path")

	if path == "" {
		rw.WriteHeader(http.StatusBadRequest)
		return
	}

	s, _ := core.SessionGet(sessionId)
	if s == nil {
		rw.WriteHeader(http.StatusNotFound)
		return
	}

	i := core.InstanceGet(s, instanceName)
	if i == nil {
		rw.WriteHeader(http.StatusNotFound)
		return
	}

	instanceFile, err := core.InstanceFile(i, path)
	if err != nil {
		log.Println(err)
		rw.WriteHeader(http.StatusInternalServerError)
		return
	}

	encoder := base64.NewEncoder(base64.StdEncoding, rw)

	if _, err = io.Copy(encoder, instanceFile); err != nil {
		log.Println(err)
		rw.WriteHeader(http.StatusInternalServerError)
		return
	}

	encoder.Close()
}