/
get_desktop_file.go
165 lines (151 loc) · 4.48 KB
/
get_desktop_file.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright 2020,2021 Avi Zimmerman
This file is part of kvdi.
kvdi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
kvdi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with kvdi. If not, see <https://www.gnu.org/licenses/>.
*/
package api
import (
"io"
"net/http"
"strconv"
"strings"
"github.com/gorilla/mux"
"github.com/tinyzimmer/kvdi/pkg/proxyproto"
"github.com/tinyzimmer/kvdi/pkg/types"
"github.com/tinyzimmer/kvdi/pkg/util/apiutil"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// swagger:operation GET /api/desktops/fs/{namespace}/{name}/stat/{fpath} Desktops statDesktopFile
// ---
// summary: Retrieve filesystem info for the given path inside a desktop session.
// parameters:
// - name: namespace
// in: path
// description: The namespace of the desktop session
// type: string
// required: true
// - name: name
// in: path
// description: The name of the desktop session
// type: string
// required: true
// - name: fpath
// in: path
// description: The path to retrieve information about
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/statDesktopFileResponse"
// "400":
// "$ref": "#/responses/error"
// "403":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/error"
func (d *desktopAPI) GetStatDesktopFile(w http.ResponseWriter, r *http.Request) {
proxy, err := d.getProxyClientForRequest(r)
if err != nil {
if client.IgnoreNotFound(err) == nil {
apiutil.ReturnAPINotFound(err, w)
return
}
apiutil.ReturnAPIError(err, w)
return
}
path := getPathFromRequest(r)
res, err := proxy.StatFile(&proxyproto.FStatRequest{
Path: path,
})
if err != nil {
apiutil.ReturnAPIError(err, w)
return
}
defer res.Close()
if _, err := io.Copy(w, res); err != nil {
apiLogger.Error(err, "Error copying proxy response to client")
}
}
// File stat response
// swagger:response statDesktopFileResponse
type swaggerStatDesktopFileResponse struct {
// in:body
Body types.StatDesktopFileResponse
}
// swagger:operation GET /api/desktops/fs/{namespace}/{name}/get/{fpath} Desktops downloadDesktopFile
// ---
// summary: Download the given file from a desktop session.
// parameters:
// - name: namespace
// in: path
// description: The namespace of the desktop session
// type: string
// required: true
// - name: name
// in: path
// description: The name of the desktop session
// type: string
// required: true
// - name: fpath
// in: path
// description: The file path to download
// type: string
// required: true
// responses:
// "200":
// content:
// "application/octet-stream":
// type: string
// format: binary
// "400":
// "$ref": "#/responses/error"
// "403":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/error"
func (d *desktopAPI) GetDownloadDesktopFile(w http.ResponseWriter, r *http.Request) {
proxy, err := d.getProxyClientForRequest(r)
if err != nil {
if client.IgnoreNotFound(err) == nil {
apiutil.ReturnAPINotFound(err, w)
return
}
apiutil.ReturnAPIError(err, w)
return
}
path := getPathFromRequest(r)
res, err := proxy.GetFile(&proxyproto.FGetRequest{
Path: path,
})
if err != nil {
apiutil.ReturnAPIError(err, w)
return
}
defer res.Body.Close()
fileSizeStr := strconv.FormatInt(res.Size, 10)
w.Header().Set("Content-Length", fileSizeStr)
w.Header().Set("Content-Type", res.Type)
w.Header().Set("Content-Disposition", "attachment; filename="+res.Name)
w.Header().Set("X-Suggested-Filename", res.Name)
w.Header().Set("X-Decompressed-Content-Length", fileSizeStr)
w.WriteHeader(http.StatusOK)
// Copy the file contents to the response
if _, err := io.Copy(w, res.Body); err != nil {
apiLogger.Error(err, "Failed to copy file contents to response buffer")
}
}
func getPathFromRequest(r *http.Request) string {
pathPrefix := apiutil.GetGorillaPath(r)
pathPrefix = strings.Replace(pathPrefix, "{name}", mux.Vars(r)["name"], 1)
pathPrefix = strings.Replace(pathPrefix, "{namespace}", mux.Vars(r)["namespace"], 1)
return strings.TrimPrefix(r.URL.Path, pathPrefix)
}