Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow web serving of user files #1457

Merged
merged 7 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/common/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var ScrapeCacheDir string
var VideoPreviewDir string
var VideoThumbnailDir string
var ScriptHeatmapDir string
var MyFilesDir string
var DownloadDir string
var WebPort int

Expand All @@ -46,6 +47,7 @@ func InitPaths() {
search_dir := flag.String("search_dir", "", "Optional: path to the Search Index directory")
preview_dir := flag.String("preview_dir", "", "Optional: path to the Scraper Cache directory")
scriptsheatmap_dir := flag.String("scripts_heatmap_dir", "", "Optional: path to the scripts_heatmap directory")
myfiles_dir := flag.String("myfiles_dir", "", "Optional: path to the myfiles directory for serving users own content (eg images")
databaseurl := flag.String("database_url", "", "Optional: override default database path")
web_port := flag.Int("web_port", 0, "Optional: override default Web Page port 9999")
ws_addr := flag.String("ws_addr", "", "Optional: override default Websocket address from the default 0.0.0.0:9998")
Expand Down Expand Up @@ -85,6 +87,7 @@ func InitPaths() {
VideoThumbnailDir = filepath.Join(AppDir, "video_thumbnail")
ScriptHeatmapDir = getPath(*scriptsheatmap_dir, "XBVR_SCRIPTHEATMAPDIR", "script_heatmap")

MyFilesDir = getPath(*myfiles_dir, "XBVR_MYFILESDIR", "myfiles")
DownloadDir = filepath.Join(AppDir, "download")

// Initialize DATABASE_URL once appdir path is known
Expand Down Expand Up @@ -120,6 +123,7 @@ func InitPaths() {
_ = os.MkdirAll(IndexDirV2, os.ModePerm)
_ = os.MkdirAll(ScrapeCacheDir, os.ModePerm)
_ = os.MkdirAll(ScriptHeatmapDir, os.ModePerm)
_ = os.MkdirAll(MyFilesDir, os.ModePerm)
_ = os.MkdirAll(DownloadDir, os.ModePerm)
}
func getPath(commandLinePath string, environmentName string, directoryName string) string {
Expand Down
36 changes: 36 additions & 0 deletions pkg/server/myfiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package server

import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/xbapps/xbvr/pkg/common"
)

type MyFilesHandler struct {
}

func (h MyFilesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := filepath.Join(common.MyFilesDir, r.URL.Path)
fi, err := os.Stat(path)

if os.IsNotExist(err) || strings.Contains(path, "..") { // check the path exists and not trying to go up directory levels
// file does not exist
http.Error(w, err.Error(), http.StatusNotFound)
return
}

//copy the relevant headers. If you want to preserve the downloaded file name, extract it with go's url parser.
if strings.HasSuffix(path, ".json") {
w.Header().Set("Content-Type", r.Header.Get("application/json"))
}
w.Header().Set("Content-Length", fmt.Sprint(fi.Size())) // useful for download progress

//stream the body to the client without fully loading it into memory
reader, _ := os.Open(path)
io.Copy(w, reader)
}
2 changes: 2 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ func StartServer(version, commit, branch, date string) {
r.PathPrefix("/imghm/").Handler(http.StripPrefix("/imghm", hmp))
downloadhandler := DownloadHandler{}
r.PathPrefix("/download/").Handler(http.StripPrefix("/download/", downloadhandler))
myfileshandler := MyFilesHandler{}
r.PathPrefix("/myfiles/").Handler(http.StripPrefix("/myfiles/", myfileshandler))
r.SkipClean(true)

r.PathPrefix("/").Handler(http.DefaultServeMux)
Expand Down
Loading