From c746f70c10e1406a1d59c20086ef962e9c398e4f Mon Sep 17 00:00:00 2001 From: Andrei Marcu Date: Mon, 14 Jan 2019 14:51:02 -0800 Subject: [PATCH] Allow changing the "selif" path name --- README.md | 1 + csp_test.go | 1 + server.go | 12 ++++++++++-- server_test.go | 4 ++-- templates.go | 1 + templates/display/audio.html | 4 ++-- templates/display/base.html | 2 +- templates/display/file.html | 2 +- templates/display/image.html | 4 ++-- templates/display/pdf.html | 4 ++-- templates/display/video.html | 4 ++-- torrent.go | 2 +- torrent_test.go | 2 +- 13 files changed, 27 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index df13b18..8d4ef11 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ allowhotlink = true - ```-bind 127.0.0.1:8080``` -- what to bind to (default is 127.0.0.1:8080) - ```-sitename myLinx``` -- the site name displayed on top (default is inferred from Host header) - ```-siteurl "http://mylinx.example.org/"``` -- the site url (default is inferred from execution context) +- ```-selifpath "selif"``` -- path relative to site base url (the "selif" in https://mylinx.example.org/selif/image.jpg) where files are accessed directly (default: selif) - ```-filespath files/``` -- Path to store uploads (default is files/) - ```-metapath meta/``` -- Path to store information about uploads (default is meta/) - ```-maxsize 4294967296``` -- maximum upload file size in bytes (default 4GB) diff --git a/csp_test.go b/csp_test.go index 3d1d499..e3dbbdd 100644 --- a/csp_test.go +++ b/csp_test.go @@ -23,6 +23,7 @@ func TestContentSecurityPolicy(t *testing.T) { Config.maxSize = 1024 * 1024 * 1024 Config.noLogs = true Config.siteName = "linx" + Config.selifPath = "selif" Config.contentSecurityPolicy = testCSPHeaders["Content-Security-Policy"] Config.referrerPolicy = testCSPHeaders["Referrer-Policy"] Config.xFrameOptions = testCSPHeaders["X-Frame-Options"] diff --git a/server.go b/server.go index 1dd1f09..c7b4342 100644 --- a/server.go +++ b/server.go @@ -42,6 +42,7 @@ var Config struct { siteName string siteURL string sitePath string + selifPath string certFile string keyFile string contentSecurityPolicy string @@ -129,6 +130,11 @@ func setup() *web.Mux { Config.sitePath = "/" } + Config.selifPath = strings.TrimLeft(Config.selifPath, "/") + if lastChar := Config.selifPath[len(Config.selifPath)-1:]; lastChar != "/" { + Config.selifPath = Config.selifPath + "/" + } + metaStorageBackend = localfs.NewLocalfsBackend(Config.metaDir) metaBackend = metajson.NewMetaJSONBackend(metaStorageBackend) fileBackend = localfs.NewLocalfsBackend(Config.filesDir) @@ -150,8 +156,8 @@ func setup() *web.Mux { // Routing setup nameRe := regexp.MustCompile("^" + Config.sitePath + `(?P[a-z0-9-\.]+)$`) - selifRe := regexp.MustCompile("^" + Config.sitePath + `selif/(?P[a-z0-9-\.]+)$`) - selifIndexRe := regexp.MustCompile("^" + Config.sitePath + `selif/$`) + selifRe := regexp.MustCompile("^" + Config.sitePath + Config.selifPath + `(?P[a-z0-9-\.]+)$`) + selifIndexRe := regexp.MustCompile("^" + Config.sitePath + Config.selifPath + `$`) torrentRe := regexp.MustCompile("^" + Config.sitePath + `(?P[a-z0-9-\.]+)/torrent$`) if Config.authFile == "" { @@ -211,6 +217,8 @@ func main() { "name of the site") flag.StringVar(&Config.siteURL, "siteurl", "", "site base url (including trailing slash)") + flag.StringVar(&Config.selifPath, "selifpath", "selif", + "path relative to site base url where files are accessed directly") flag.Int64Var(&Config.maxSize, "maxsize", 4*1024*1024*1024, "maximum upload file size in bytes (default 4GB)") flag.Uint64Var(&Config.maxExpiry, "maxexpiry", 0, diff --git a/server_test.go b/server_test.go index 7727f0c..d9374ab 100644 --- a/server_test.go +++ b/server_test.go @@ -173,7 +173,7 @@ func TestFileNotFound(t *testing.T) { filename := generateBarename() - req, err := http.NewRequest("GET", "/selif/"+filename, nil) + req, err := http.NewRequest("GET", "/"+Config.selifPath+filename, nil) if err != nil { t.Fatal(err) } @@ -941,7 +941,7 @@ func TestPutAndOverwrite(t *testing.T) { // Make sure it's the new file w = httptest.NewRecorder() - req, err = http.NewRequest("GET", "/selif/"+myjson.Filename, nil) + req, err = http.NewRequest("GET", "/"+Config.selifPath+myjson.Filename, nil) mux.ServeHTTP(w, req) if w.Code == 404 { diff --git a/templates.go b/templates.go index 0687bce..79c90ce 100644 --- a/templates.go +++ b/templates.go @@ -83,6 +83,7 @@ func renderTemplate(tpl *pongo2.Template, context pongo2.Context, r *http.Reques } context["sitepath"] = Config.sitePath + context["selifpath"] = Config.selifPath context["using_auth"] = Config.authFile != "" return tpl.ExecuteWriter(context, writer) diff --git a/templates/display/audio.html b/templates/display/audio.html index 689bab3..b5ae1e3 100644 --- a/templates/display/audio.html +++ b/templates/display/audio.html @@ -2,8 +2,8 @@ {% block main %} {% endblock %} diff --git a/templates/display/base.html b/templates/display/base.html index 8f33b46..172e17a 100644 --- a/templates/display/base.html +++ b/templates/display/base.html @@ -18,7 +18,7 @@ {% block infomore %}{% endblock %} {{ size }} | torrent | - get + get {% block infoleft %}{% endblock %} diff --git a/templates/display/file.html b/templates/display/file.html index 5eeb424..670651e 100644 --- a/templates/display/file.html +++ b/templates/display/file.html @@ -2,7 +2,7 @@ {% block main %}
-

You are requesting {{ filename }}, click here to download.

+

You are requesting {{ filename }}, click here to download.

{% if files|length > 0 %}

Contents of the archive:

diff --git a/templates/display/image.html b/templates/display/image.html index b1ea7dd..807b7ad 100644 --- a/templates/display/image.html +++ b/templates/display/image.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% block main %} - - + + {% endblock %} diff --git a/templates/display/pdf.html b/templates/display/pdf.html index 7ad20f1..69501f7 100644 --- a/templates/display/pdf.html +++ b/templates/display/pdf.html @@ -1,10 +1,10 @@ {% extends "base.html" %} {% block main %} - +

It appears your Web browser is not configured to display PDF files. -No worries, just click here to download the PDF file.

+No worries, just click here to download the PDF file.

{% endblock %} diff --git a/templates/display/video.html b/templates/display/video.html index 9fc90d5..317664b 100644 --- a/templates/display/video.html +++ b/templates/display/video.html @@ -2,7 +2,7 @@ {% block main %} {% endblock %} diff --git a/torrent.go b/torrent.go index 77cd2fd..4155872 100644 --- a/torrent.go +++ b/torrent.go @@ -45,7 +45,7 @@ func createTorrent(fileName string, f io.ReadCloser, r *http.Request) ([]byte, e PieceLength: TORRENT_PIECE_LENGTH, Name: fileName, }, - UrlList: []string{fmt.Sprintf("%sselif/%s", getSiteURL(r), fileName)}, + UrlList: []string{fmt.Sprintf("%s%s%s", getSiteURL(r), Config.selifPath, fileName)}, } for { diff --git a/torrent_test.go b/torrent_test.go index 38132f2..b553231 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -45,7 +45,7 @@ func TestCreateTorrent(t *testing.T) { t.Fatal("Length was less than or equal to 0, expected more") } - tracker := fmt.Sprintf("%sselif/%s", Config.siteURL, fileName) + tracker := fmt.Sprintf("%s%s%s", Config.siteURL, Config.selifPath, fileName) if decoded.UrlList[0] != tracker { t.Fatalf("First entry in URL list was %s, expected %s", decoded.UrlList[0], tracker) }