This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
hFileBrowser.go
158 lines (135 loc) · 4.95 KB
/
hFileBrowser.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
package my
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/go-chi/chi"
"github.com/seknox/trasa/server/api/redis"
"github.com/seknox/trasa/server/models"
"github.com/seknox/trasa/server/utils"
"github.com/sirupsen/logrus"
)
func FileUploadHandler(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, 5000000000)
err := r.ParseForm()
if err != nil {
// redirect or set error status code.
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "File too large", "File not uploaded", nil)
return
}
userContext := r.Context().Value("user").(models.UserContext)
// Parse our multipart form, 10 << 20 specifies a maximum
// upload of 10 MB files.
r.ParseMultipartForm(10 << 20)
// FormFile returns the first file for the given key `myFile`
// it also returns the FileHeader so we can get the Filename,
// the Header and the size of the file
file, handler, err := r.FormFile("file")
if err != nil {
logrus.Errorf("error retrieving the file: %v", err)
utils.TrasaResponse(w, 200, "failed", "no file uploaded", "File not uploaded", nil)
return
}
defer file.Close()
// Create a temporary file within our temp-images directory that follows
// a particular naming pattern
err = os.MkdirAll(filepath.Join(utils.GetTmpDir(), "trasa", "accessproxy", "guac", "shared", userContext.User.ID), os.ModePerm)
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "could not create shared directory ", "File not uploaded", nil)
return
}
f, err := os.Create(filepath.Join(utils.GetTmpDir(), "trasa", "accessproxy", "guac", "shared", userContext.User.ID, handler.Filename))
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "could not create a file", "File not uploaded", nil)
return
}
defer f.Close()
io.Copy(f, file)
utils.TrasaResponse(w, 200, "success", "file uploaded", fmt.Sprintf(`File %s uploaded`, handler.Filename), nil)
}
func GetDownloadableFileList(w http.ResponseWriter, r *http.Request) {
userContext := r.Context().Value("user").(models.UserContext)
userDir := filepath.Join(utils.GetTmpDir(), "trasa", "accessproxy", "guac", "shared", userContext.User.ID)
fileList, err := ioutil.ReadDir(userDir)
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "could not get file list", "GetDownloadableFileList")
return
}
fileNameList := []string{}
for _, fileI := range fileList {
if !fileI.IsDir() {
fileNameList = append(fileNameList, fileI.Name())
}
}
utils.TrasaResponse(w, 200, "success", "dir list", "GetDownloadableFileList", fileNameList)
}
func GetFileDownloadToken(w http.ResponseWriter, r *http.Request) {
userContext := r.Context().Value("user").(models.UserContext)
userID := userContext.User.ID
authKey := utils.GetRandomString(17)
err := redis.Store.Set(authKey, time.Second*600, "userID", userID)
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "could not save token", "file not uploaded")
return
}
w.Header().Set("sskey", authKey)
utils.TrasaResponse(w, 200, "success", "", fmt.Sprintf(`File downloaded`), nil)
return
}
func FileDownloadHandler(w http.ResponseWriter, r *http.Request) {
fileNameEnc := chi.URLParam(r, "fileName")
fileName, err := url.PathUnescape(fileNameEnc)
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "bad file name", "FileDownloadHandler", nil)
return
}
ssKey := chi.URLParam(r, "sskey")
userID, err := redis.Store.Get(ssKey, "userID")
if err != nil || ssKey == "" || userID == "" {
utils.TrasaResponse(w, 200, "failed", "invalid sskey", "FileDownloadHandler", nil, nil)
return
}
if strings.Contains(fileName, "/") {
utils.TrasaResponse(w, 200, "failed", "invalid filename", "FileDownloadHandler", nil, nil)
return
}
filePath := filepath.Join(utils.GetTmpDir(), "trasa", "accessproxy", "guac", "shared", userID, fileName)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
http.ServeFile(w, r, filePath)
}
func FileDeleteHandler(w http.ResponseWriter, r *http.Request) {
//http.StripPrefix("/api/v1/download_file", http.FileServer(http.Dir("../accessproxy/shared"))).ServeHTTP
userContext := r.Context().Value("user").(models.UserContext)
userID := userContext.User.ID
fileNameEnc := chi.URLParam(r, "fileName")
fileName, err := url.PathUnescape(fileNameEnc)
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "bad file name", "file delete", nil)
return
}
if strings.Contains(fileName, "/") {
utils.TrasaResponse(w, 200, "failed", "invalid filename", "FileDownloadHandler")
return
}
filePath := filepath.Join(utils.GetTmpDir(), "trasa", "accessproxy", "guac", "shared", userID, fileName)
err = os.Remove(filePath)
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "could not delete file", "file delete")
return
}
utils.TrasaResponse(w, 200, "success", "file deleted", "file delete")
}