Merge pull request #2493 from owncloud/web_not_found_redirect

redirect to root when file not found
This commit is contained in:
Jörn Friedrich Dreyer
2021-09-15 13:53:19 +02:00
committed by GitHub
2 changed files with 42 additions and 6 deletions

View File

@@ -0,0 +1,5 @@
Enhancement: redirect invalid links to oC Web
Invalid links (eg. https://foo.bar/index.php/apps/pdfviewer) will be redirect to ownCloud Web instead of displaying a blank page with a "not found" message.
https://github.com/owncloud/ocis/pull/2493

View File

@@ -133,14 +133,15 @@ func (p Web) Static(ttl int) http.HandlerFunc {
if !strings.HasSuffix(rootWithSlash, "/") {
rootWithSlash = rootWithSlash + "/"
}
assets := assets.New(
assets.Logger(p.logger),
assets.Config(p.config),
)
static := http.StripPrefix(
rootWithSlash,
http.FileServer(
assets.New(
assets.Logger(p.logger),
assets.Config(p.config),
),
interceptNotFound(
http.FileServer(assets),
rootWithSlash,
),
)
@@ -182,3 +183,33 @@ func (p Web) Static(ttl int) http.HandlerFunc {
static.ServeHTTP(w, r)
}
}
func interceptNotFound(h http.Handler, root string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
notFoundInterceptor := &NotFoundInterceptor{ResponseWriter: w}
h.ServeHTTP(notFoundInterceptor, r)
if notFoundInterceptor.status == http.StatusNotFound {
http.Redirect(w, r, root, http.StatusTemporaryRedirect)
// TODO: replace the redirect with a not found page containing a link to the Web UI
}
}
}
type NotFoundInterceptor struct {
http.ResponseWriter
status int
}
func (w *NotFoundInterceptor) WriteHeader(status int) {
w.status = status
if status != http.StatusNotFound {
w.ResponseWriter.WriteHeader(status)
}
}
func (w *NotFoundInterceptor) Write(p []byte) (int, error) {
if w.status != http.StatusNotFound {
return w.ResponseWriter.Write(p)
}
return len(p), nil
}