-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediaServer.go
330 lines (284 loc) · 9.91 KB
/
mediaServer.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"crypto/md5"
"encoding/hex"
"errors"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"imuslab.com/arozos/mod/compatibility"
"imuslab.com/arozos/mod/filesystem"
fs "imuslab.com/arozos/mod/filesystem"
"imuslab.com/arozos/mod/network/gzipmiddleware"
"imuslab.com/arozos/mod/utils"
)
/*
Media Server
This function serve large file objects like video and audio file via asynchronize go routine :)
Example usage:
/media/?file=user:/Desktop/test/02.Orchestra- エミール (Addendum version).mp3
/media/?file=user:/Desktop/test/02.Orchestra- エミール (Addendum version).mp3&download=true
This will serve / download the file located at files/users/{username}/Desktop/test/02.Orchestra- エミール (Addendum version).mp3
PLEASE ALWAYS USE URLENCODE IN THE LINK PASSED INTO THE /media ENDPOINT
*/
func mediaServer_init() {
if *enable_gzip {
http.HandleFunc("/media/", gzipmiddleware.CompressFunc(serverMedia))
http.HandleFunc("/media/getMime/", gzipmiddleware.CompressFunc(serveMediaMime))
} else {
http.HandleFunc("/media/", serverMedia)
http.HandleFunc("/media/getMime/", serveMediaMime)
}
//Download API always bypass gzip no matter if gzip mode is enabled
http.HandleFunc("/media/download/", serverMedia)
}
// This function validate the incoming media request and return fsh, vpath, rpath and err if any
func media_server_validateSourceFile(w http.ResponseWriter, r *http.Request) (*filesystem.FileSystemHandler, string, string, error) {
username, err := authAgent.GetUserName(w, r)
if err != nil {
return nil, "", "", errors.New("User not logged in")
}
userinfo, _ := userHandler.GetUserInfoFromUsername(username)
//Validate url valid
if strings.Count(r.URL.String(), "?") > 1 {
return nil, "", "", errors.New("Invalid paramters. Multiple ? found")
}
targetfile, _ := utils.GetPara(r, "file")
targetfile, err = url.QueryUnescape(targetfile)
if err != nil {
return nil, "", "", err
}
if targetfile == "" {
return nil, "", "", errors.New("Missing paramter 'file'")
}
//Translate the virtual directory to realpath
fsh, subpath, err := GetFSHandlerSubpathFromVpath(targetfile)
if err != nil {
return nil, "", "", errors.New("Unable to load from target file system")
}
fshAbs := fsh.FileSystemAbstraction
realFilepath, err := fshAbs.VirtualPathToRealPath(subpath, userinfo.Username)
if fshAbs.FileExists(realFilepath) && fshAbs.IsDir(realFilepath) {
return nil, "", "", errors.New("Given path is not a file")
}
if err != nil {
return nil, "", "", errors.New("Unable to translate the given filepath")
}
if !fshAbs.FileExists(realFilepath) {
//Sometime if url is not URL encoded, this error might be shown as well
//Try to use manual segmentation
originalURL := r.URL.String()
//Must be pre-processed with system special URI Decode function to handle edge cases
originalURL = fs.DecodeURI(originalURL)
if strings.Contains(originalURL, "&download=true") {
originalURL = strings.ReplaceAll(originalURL, "&download=true", "")
} else if strings.Contains(originalURL, "download=true") {
originalURL = strings.ReplaceAll(originalURL, "download=true", "")
}
if strings.Contains(originalURL, "&file=") {
originalURL = strings.ReplaceAll(originalURL, "&file=", "file=")
}
urlInfo := strings.Split(originalURL, "file=")
possibleVirtualFilePath := urlInfo[len(urlInfo)-1]
possibleRealpath, err := fshAbs.VirtualPathToRealPath(possibleVirtualFilePath, userinfo.Username)
if err != nil {
systemWideLogger.PrintAndLog("Media Server", "Error when trying to serve file in compatibility mode", err)
return nil, "", "", errors.New("Error when trying to serve file in compatibility mode")
}
if fshAbs.FileExists(possibleRealpath) {
realFilepath = possibleRealpath
systemWideLogger.PrintAndLog("Media Server", "Serving file "+filepath.Base(possibleRealpath)+" in compatibility mode. Do not to use '&' or '+' sign in filename! ", nil)
return fsh, targetfile, realFilepath, nil
} else {
return nil, "", "", errors.New("File not exists")
}
}
return fsh, targetfile, realFilepath, nil
}
func serveMediaMime(w http.ResponseWriter, r *http.Request) {
targetFsh, _, realFilepath, err := media_server_validateSourceFile(w, r)
if err != nil {
utils.SendErrorResponse(w, err.Error())
return
}
targetFshAbs := targetFsh.FileSystemAbstraction
if targetFsh.RequireBuffer {
//File is not on local. Guess its mime by extension
utils.SendTextResponse(w, "application/"+filepath.Ext(realFilepath)[1:])
return
}
mime := "text/directory"
if !targetFshAbs.IsDir(realFilepath) {
m, _, err := fs.GetMime(realFilepath)
if err != nil {
mime = ""
}
mime = m
}
utils.SendTextResponse(w, mime)
}
func serverMedia(w http.ResponseWriter, r *http.Request) {
userinfo, _ := userHandler.GetUserInfoFromRequest(w, r)
//Serve normal media files
targetFsh, vpath, realFilepath, err := media_server_validateSourceFile(w, r)
if err != nil {
utils.SendErrorResponse(w, err.Error())
return
}
targetFshAbs := targetFsh.FileSystemAbstraction
//Check if downloadMode
downloadMode := false
dw, _ := utils.GetPara(r, "download")
if dw == "true" {
downloadMode = true
}
//New download implementations, allow /download to be used instead of &download=true
if strings.Contains(r.RequestURI, "media/download/?file=") {
downloadMode = true
}
//Serve the file
if downloadMode {
escapedRealFilepath, err := url.PathUnescape(realFilepath)
if err != nil {
utils.SendErrorResponse(w, err.Error())
return
}
filename := filepath.Base(escapedRealFilepath)
w.Header().Set("Content-Disposition", "attachment; filename=\""+filename+"\"")
w.Header().Set("Content-Type", compatibility.BrowserCompatibilityOverrideContentType(r.UserAgent(), filename, r.Header.Get("Content-Type")))
if targetFsh.RequireBuffer || !filesystem.FileExists(realFilepath) {
//Stream it directly from remote
w.Header().Set("Content-Length", strconv.Itoa(int(targetFshAbs.GetFileSize(realFilepath))))
remoteStream, err := targetFshAbs.ReadStream(realFilepath)
if err != nil {
utils.SendErrorResponse(w, err.Error())
return
}
io.Copy(w, remoteStream)
remoteStream.Close()
} else {
http.ServeFile(w, r, escapedRealFilepath)
}
} else {
if targetFsh.RequireBuffer {
w.Header().Set("Content-Length", strconv.Itoa(int(targetFshAbs.GetFileSize(realFilepath))))
//Check buffer exists
ps, _ := targetFsh.GetUniquePathHash(vpath, userinfo.Username)
buffpool := filepath.Join(*tmp_directory, "fsbuffpool")
buffFile := filepath.Join(buffpool, ps)
if fs.FileExists(buffFile) {
//Stream the buff file if hash matches
remoteFileHash, err := getHashFromRemoteFile(targetFsh.FileSystemAbstraction, realFilepath)
if err == nil {
localFileHash, err := os.ReadFile(buffFile + ".hash")
if err == nil {
if string(localFileHash) == remoteFileHash {
//Hash matches. Serve local buffered file
http.ServeFile(w, r, buffFile)
return
}
}
}
}
remoteStream, err := targetFshAbs.ReadStream(realFilepath)
if err != nil {
utils.SendErrorResponse(w, err.Error())
return
}
defer remoteStream.Close()
io.Copy(w, remoteStream)
if *enable_buffering {
os.MkdirAll(buffpool, 0775)
go func() {
BufferRemoteFileToTmp(buffFile, targetFsh, realFilepath)
}()
}
} else if !filesystem.FileExists(realFilepath) {
//Streaming from remote file system that support fseek
f, err := targetFsh.FileSystemAbstraction.Open(realFilepath)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Internal Server Error"))
return
}
fstat, _ := f.Stat()
defer f.Close()
http.ServeContent(w, r, filepath.Base(realFilepath), fstat.ModTime(), f)
} else {
http.ServeFile(w, r, realFilepath)
}
}
}
func BufferRemoteFileToTmp(buffFile string, fsh *filesystem.FileSystemHandler, rpath string) error {
if fs.FileExists(buffFile + ".download") {
return errors.New("another buffer process running")
}
//Generate a stat file for the buffer
hash, err := getHashFromRemoteFile(fsh.FileSystemAbstraction, rpath)
if err != nil {
//Do not buffer
return err
}
os.WriteFile(buffFile+".hash", []byte(hash), 0775)
//Buffer the file from remote to local
f, err := fsh.FileSystemAbstraction.ReadStream(rpath)
if err != nil {
os.Remove(buffFile + ".hash")
return err
}
defer f.Close()
dest, err := os.OpenFile(buffFile+".download", os.O_CREATE|os.O_WRONLY, 0775)
if err != nil {
os.Remove(buffFile + ".hash")
return err
}
defer dest.Close()
io.Copy(dest, f)
f.Close()
dest.Close()
os.Rename(buffFile+".download", buffFile)
//Clean the oldest buffpool item if size too large
dirsize, _ := fs.GetDirctorySize(filepath.Dir(buffFile), false)
oldestModtime := time.Now().Unix()
oldestFile := ""
for int(dirsize) > *bufferPoolSize<<20 {
//fmt.Println("CLEARNING BUFF", dirsize)
files, _ := filepath.Glob(filepath.ToSlash(filepath.Dir(buffFile)) + "/*")
for _, file := range files {
if filepath.Ext(file) == ".hash" {
continue
}
thisModTime, _ := fs.GetModTime(file)
if thisModTime < oldestModtime {
oldestModtime = thisModTime
oldestFile = file
}
}
os.Remove(oldestFile)
os.Remove(oldestFile + ".hash")
dirsize, _ = fs.GetDirctorySize(filepath.Dir(buffFile), false)
oldestModtime = time.Now().Unix()
}
return nil
}
func getHashFromRemoteFile(fshAbs filesystem.FileSystemAbstraction, rpath string) (string, error) {
filestat, err := fshAbs.Stat(rpath)
if err != nil {
//Always pull from remote
return "", err
}
if filestat.Size() >= int64(*bufferPoolSize<<20) {
return "", errors.New("Unable to buffer: file larger than buffpool size")
}
if filestat.Size() >= int64(*bufferFileMaxSize<<20) {
return "", errors.New("File larger than max buffer file size")
}
statHash := strconv.Itoa(int(filestat.ModTime().Unix() + filestat.Size()))
hash := md5.Sum([]byte(statHash))
return hex.EncodeToString(hash[:]), nil
}