mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-04 11:19:39 -06:00
Merge pull request #2493 from owncloud/web_not_found_redirect
redirect to root when file not found
This commit is contained in:
5
changelog/unreleased/redirect-invalid-links-to-oC-web.md
Normal file
5
changelog/unreleased/redirect-invalid-links-to-oC-web.md
Normal 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
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user