-
Notifications
You must be signed in to change notification settings - Fork 90
/
contents.go
83 lines (69 loc) · 1.55 KB
/
contents.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 handlers
import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/gorilla/mux"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/store"
)
type GetAppContentsResponse struct {
Files map[string][]byte `json:"files"`
}
func (h *Handler) GetAppContents(w http.ResponseWriter, r *http.Request) {
appSlug := mux.Vars(r)["appSlug"]
sequence, err := strconv.Atoi(mux.Vars(r)["sequence"])
if err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}
a, err := store.GetStore().GetAppFromSlug(appSlug)
if err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}
archivePath, err := ioutil.TempDir("", "kotsadm")
if err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}
defer os.RemoveAll(archivePath)
err = store.GetStore().GetAppVersionArchive(a.ID, int64(sequence), archivePath)
if err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}
// walk the parth, adding all to the files map
// base64 decode these
archiveFiles := map[string][]byte{}
err = filepath.Walk(archivePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
contents, err := ioutil.ReadFile(path)
if err != nil {
return err
}
archiveFiles[strings.TrimPrefix(path, archivePath)] = contents
return nil
})
if err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}
getAppContentsResponse := GetAppContentsResponse{
Files: archiveFiles,
}
JSON(w, 200, getAppContentsResponse)
}