-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileserver.go
More file actions
82 lines (69 loc) · 2.06 KB
/
Copy pathfileserver.go
File metadata and controls
82 lines (69 loc) · 2.06 KB
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
package main
import (
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"strings"
)
// LocalFileServer serves .glb/.gltf files on its own HTTP port.
// This avoids issues with Wails' asset server and Vite proxying.
type LocalFileServer struct {
Port int
}
func StartLocalFileServer() *LocalFileServer {
mux := http.NewServeMux()
mux.HandleFunc("/localfile/", serveLocalFile)
// Find a free port
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
fmt.Printf("[fileserver] failed to listen: %v\n", err)
return &LocalFileServer{Port: 0}
}
port := listener.Addr().(*net.TCPAddr).Port
fmt.Printf("[fileserver] serving on http://127.0.0.1:%d\n", port)
go func() {
if err := http.Serve(listener, mux); err != nil {
fmt.Printf("[fileserver] server error: %v\n", err)
}
}()
return &LocalFileServer{Port: port}
}
func serveLocalFile(w http.ResponseWriter, r *http.Request) {
filePath := r.URL.Query().Get("path")
fmt.Printf("[fileserver] request: %s -> path=%s\n", r.URL.String(), filePath)
if filePath == "" {
http.Error(w, "missing path parameter", http.StatusBadRequest)
return
}
// Security: only serve .glb / .gltf files
lower := strings.ToLower(filePath)
if !strings.HasSuffix(lower, ".glb") && !strings.HasSuffix(lower, ".gltf") {
http.Error(w, "forbidden file type", http.StatusForbidden)
return
}
// Check file exists
info, err := os.Stat(filePath)
if err != nil {
http.Error(w, "file not found", http.StatusNotFound)
return
}
// Set appropriate content type
contentType := "model/gltf-binary"
if strings.HasSuffix(lower, ".gltf") {
contentType = "model/gltf+json"
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", fmt.Sprintf("%d", info.Size()))
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Cache-Control", "public, max-age=3600")
// Open and serve the file
f, err := os.Open(filePath)
if err != nil {
http.Error(w, "cannot open file", http.StatusInternalServerError)
return
}
defer f.Close()
http.ServeContent(w, r, filepath.Base(filePath), info.ModTime(), f)
}