-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (84 loc) · 3.07 KB
/
main.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
package main
import (
"html/template"
"log"
"net/http"
"os"
"path"
"github.com/LinuxSploit/MediaBucket/middleware"
"github.com/LinuxSploit/MediaBucket/transcoder"
"github.com/LinuxSploit/MediaBucket/tus"
"github.com/joho/godotenv"
)
func init() {
// load .env file
godotenv.Load()
log.Println("init completed!")
}
func main() {
log.Println("service started...")
// Create TUS video Upload handler, init basePath, storageDir
videoHandler, err := tus.SetupTusVideoHandler(os.Getenv("ServerURL")+os.Getenv("VideoUploadPath"), "/storage/MediaBucket/videos")
if err != nil {
log.Fatalf("Unable to create photo handler: %v", err)
}
// Create TUS video Upload handler, init basePath, storageDir
imageHandler, err := tus.SetupTusImageHandler(os.Getenv("ServerURL")+os.Getenv("ImageUploadPath"), "/storage/MediaBucket/images")
if err != nil {
log.Fatalf("Unable to create photo handler: %v", err)
}
mux := http.NewServeMux()
// Register TUS video Upload handler to /upload/ route
mux.Handle("/video/", http.StripPrefix("/video/", videoHandler))
// Register TUS image Upload handler to /image/ route
mux.Handle("/image/", http.StripPrefix("/image/", imageHandler))
// Serve HLS video streams
videoFileServer := http.StripPrefix("/hls/", NoDirListingFileServer(http.Dir("/storage/MediaBucket/hls/")))
mux.Handle("/hls/", middleware.CORSMiddleware(videoFileServer))
//thumbnail server
thumbnailFileServer := http.StripPrefix("/thumbnail/", NoDirListingFileServer(http.Dir("/storage/MediaBucket/thumbnail/")))
mux.Handle("/thumbnail/", middleware.CORSMiddleware(thumbnailFileServer))
// Serve the video upload demo page
mux.HandleFunc("/video-demo", func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("./templates/video-demo.html"))
tmpl.Execute(w, os.Getenv("ServerURL"))
})
// Serve the image upload demo page
mux.HandleFunc("/image-demo", func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("./templates/image-demo.html"))
tmpl.Execute(w, os.Getenv("ServerURL"))
})
// Start the video transcoding worker
transcoder.StartTranscodeWorker("/storage/MediaBucket/videos/", "/storage/MediaBucket/hls/")
// Start the HTTP server
if err := http.ListenAndServe("0.0.0.0:8080", mux); err != nil {
log.Fatalf("Unable to start server: %v", err)
}
}
// NoDirListingFileServer wraps the http.FileServer to disable directory listings
func NoDirListingFileServer(root http.FileSystem) http.Handler {
fs := http.FileServer(root)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get the absolute path to prevent directory traversal
upath := path.Clean(r.URL.Path)
f, err := root.Open(upath)
if err != nil {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
defer f.Close()
// Get file info
info, err := f.Stat()
if err != nil {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
// deny access to directory path
if info.IsDir() {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
// Serve the file
fs.ServeHTTP(w, r)
})
}