Skip to content

Commit

Permalink
feat: Allow web serving of user files (xbapps#1457)
Browse files Browse the repository at this point in the history
* Environment Variable to override WSAddr

* Add command line and Env Variable config items

* Create MyFiles Web Endpoint

* Remove debug log left by mistake

* Make Myfiles Directory Configurable

---------

Co-authored-by: crwxaj <52156245+crwxaj@users.noreply.github.com>
  • Loading branch information
toshski and crwxaj authored Nov 5, 2023
1 parent 6f26070 commit ef6f065
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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

0 comments on commit ef6f065

Please sign in to comment.