diff --git a/server/ctrl/static.go b/server/ctrl/static.go index 8fd54037..4eacec7c 100644 --- a/server/ctrl/static.go +++ b/server/ctrl/static.go @@ -2,9 +2,8 @@ package ctrl import ( "bytes" - "embed" + _ "embed" "encoding/base64" - "errors" "fmt" "io" "io/fs" @@ -24,9 +23,6 @@ import ( var ( WWWDir fs.FS - //go:embed static/www - WWWEmbed embed.FS - //go:embed static/404.html HtmlPage404 []byte @@ -38,103 +34,6 @@ func init() { WWWDir = os.DirFS(GetAbsolutePath("../")) } -func LegacyStaticHandler(_path string) func(*App, http.ResponseWriter, *http.Request) { // TODO: migrate away - return func(ctx *App, res http.ResponseWriter, req *http.Request) { - var chroot string = GetAbsolutePath(_path) - if srcPath := JoinPath(chroot, req.URL.Path); strings.HasPrefix(srcPath, chroot) == false { - http.NotFound(res, req) - return - } - legacyServeFile(res, req, JoinPath(_path, TrimBase(req.URL.Path))) - } -} - -func LegacyIndexHandler(ctx *App, res http.ResponseWriter, req *http.Request) { // TODO: migrate away - url := TrimBase(req.URL.Path) - if url != URL_SETUP && Config.Get("auth.admin").String() == "" { - http.Redirect(res, req, URL_SETUP, http.StatusTemporaryRedirect) - return - } else if url != "/" && strings.HasPrefix(url, "/s/") == false && - strings.HasPrefix(url, "/view/") == false && strings.HasPrefix(url, "/files/") == false && - url != "/login" && url != "/logout" && strings.HasPrefix(url, "/admin") == false && strings.HasPrefix(url, "/tags") == false { - NotFoundHandler(ctx, res, req) - return - } - ua := req.Header.Get("User-Agent") - if strings.Contains(ua, "MSIE ") || strings.Contains(ua, "Trident/") || strings.Contains(ua, "Edge/") { - // Microsoft is behaving on many occasion differently than Firefox / Chrome. - // I have neither the time / motivation for it to work properly - res.WriteHeader(http.StatusBadRequest) - res.Write([]byte(Page(` -

Internet explorer is not supported

-

- We don't support IE / Edge at this time -
- Please use either Chromium, Firefox or Chrome -

- `))) - return - } - legacyServeFile(res, req, "/index.html") -} - -func legacyServeFile(res http.ResponseWriter, req *http.Request, filePath string) { // TODO: migrate away - staticConfig := []struct { - ContentType string - FileExt string - }{ - {"br", ".br"}, - {"gzip", ".gz"}, - {"", ""}, - } - - statusCode := 200 - if req.URL.Path == "/" { - if errName := req.URL.Query().Get("error"); errName != "" { - statusCode = HTTPError(errors.New(errName)).Status() - } - } - - head := res.Header() - acceptEncoding := req.Header.Get("Accept-Encoding") - for _, cfg := range staticConfig { - if strings.Contains(acceptEncoding, cfg.ContentType) == false { - continue - } - curPath := filePath + cfg.FileExt - var ( - file fs.File - err error - ) - if env := os.Getenv("DEBUG"); env == "true" { - file, err = WWWDir.Open("server/ctrl/static/www" + curPath) - } else { - file, err = WWWEmbed.Open("static/www" + curPath) - } - if err != nil { - continue - } else if stat, err := file.Stat(); err == nil { - etag := QuickHash(fmt.Sprintf( - "%s %d %d %s", - curPath, stat.Size(), stat.Mode(), stat.ModTime()), 10, - ) - if etag == req.Header.Get("If-None-Match") { - res.WriteHeader(http.StatusNotModified) - return - } - head.Set("Etag", etag) - } - if cfg.ContentType != "" { - head.Set("Content-Encoding", cfg.ContentType) - } - res.WriteHeader(statusCode) - io.Copy(res, file) - file.Close() - return - } - http.NotFound(res, req) -} - func ServeBackofficeHandler(ctx *App, res http.ResponseWriter, req *http.Request) { url := req.URL.Path if filepath.Ext(filepath.Base(url)) != "" { diff --git a/server/routes.go b/server/routes.go index d4dee85b..37e965b9 100644 --- a/server/routes.go +++ b/server/routes.go @@ -75,11 +75,7 @@ func Build(r *mux.Router, a App) { // Webdav server / Shared Link middlewares = []Middleware{IndexHeaders, SecureHeaders, PluginInjector} - if os.Getenv("LEGACY") == "true" { // TODO: remove once migration is done - r.HandleFunc(WithBase("/s/{share}"), NewMiddlewareChain(LegacyIndexHandler, middlewares, a)).Methods("GET") - } else { - r.HandleFunc(WithBase("/s/{share}"), NewMiddlewareChain(ServeFrontofficeHandler, middlewares, a)).Methods("GET") - } + r.HandleFunc(WithBase("/s/{share}"), NewMiddlewareChain(ServeFrontofficeHandler, middlewares, a)).Methods("GET") middlewares = []Middleware{WebdavBlacklist, SessionStart, PluginInjector} r.PathPrefix(WithBase("/s/{share}")).Handler(NewMiddlewareChain(WebdavHandler, middlewares, a)) middlewares = []Middleware{ApiHeaders, SecureHeaders, RedirectSharedLoginIfNeeded, SessionStart, LoggedInOnly, PluginInjector} @@ -91,17 +87,11 @@ func Build(r *mux.Router, a App) { r.HandleFunc(WithBase("/api/plugin"), NewMiddlewareChain(PluginExportHandler, append(middlewares, PublicCORS), a)).Methods("GET", "OPTIONS") r.HandleFunc(WithBase("/api/config"), NewMiddlewareChain(PublicConfigHandler, append(middlewares, PublicCORS), a)).Methods("GET", "OPTIONS") middlewares = []Middleware{StaticHeaders, SecureHeaders, PublicCORS, PluginInjector} - if os.Getenv("LEGACY") == "true" { // TODO: remove after migration is done - r.PathPrefix(WithBase("/assets")).Handler(http.HandlerFunc(NewMiddlewareChain(LegacyStaticHandler("/"), middlewares, a))).Methods("GET", "OPTIONS") - r.HandleFunc(WithBase("/favicon.ico"), NewMiddlewareChain(LegacyStaticHandler("/assets/logo/"), middlewares, a)).Methods("GET") - r.HandleFunc(WithBase("/sw_cache.js"), NewMiddlewareChain(LegacyStaticHandler("/assets/worker/"), middlewares, a)).Methods("GET") - } else { // TODO: remove this after migration is done - r.PathPrefix(WithBase("/assets")).Handler(http.HandlerFunc(NewMiddlewareChain(ServeFile("/"), middlewares, a))).Methods("GET", "OPTIONS") - r.HandleFunc(WithBase("/sw.js"), http.HandlerFunc(NewMiddlewareChain(ServeFile("/assets/"), middlewares, a))).Methods("GET") - r.HandleFunc(WithBase("/favicon.ico"), NewMiddlewareChain(ServeFavicon, middlewares, a)).Methods("GET") - r.HandleFunc(WithBase("/plugin/{name}/{path:.+}"), NewMiddlewareChain(PluginStaticHandler, middlewares, a)).Methods("GET") - r.HandleFunc(WithBase("/plugin/{name}.zip"), NewMiddlewareChain(PluginDownloadHandler, middlewares, a)).Methods("GET") - } + r.PathPrefix(WithBase("/assets")).Handler(http.HandlerFunc(NewMiddlewareChain(ServeFile("/"), middlewares, a))).Methods("GET", "OPTIONS") + r.HandleFunc(WithBase("/sw.js"), http.HandlerFunc(NewMiddlewareChain(ServeFile("/assets/"), middlewares, a))).Methods("GET") + r.HandleFunc(WithBase("/favicon.ico"), NewMiddlewareChain(ServeFavicon, middlewares, a)).Methods("GET") + r.HandleFunc(WithBase("/plugin/{name}/{path:.+}"), NewMiddlewareChain(PluginStaticHandler, middlewares, a)).Methods("GET") + r.HandleFunc(WithBase("/plugin/{name}.zip"), NewMiddlewareChain(PluginDownloadHandler, middlewares, a)).Methods("GET") // Other endpoints middlewares = []Middleware{ApiHeaders, PluginInjector, PublicCORS} @@ -125,11 +115,7 @@ func CatchAll(r *mux.Router, a App) { middlewares := []Middleware{SecureHeaders, PluginInjector} r.PathPrefix(WithBase("/admin")).Handler(http.HandlerFunc(NewMiddlewareChain(ServeBackofficeHandler, middlewares, a))).Methods("GET") middlewares = []Middleware{IndexHeaders, SecureHeaders, PluginInjector} - if os.Getenv("LEGACY") == "true" { // TODO: remove once migration is done - r.PathPrefix("/").Handler(http.HandlerFunc(NewMiddlewareChain(LegacyIndexHandler, middlewares, a))).Methods("GET", "POST") - } else { - r.PathPrefix("/").Handler(http.HandlerFunc(NewMiddlewareChain(ServeFrontofficeHandler, middlewares, a))).Methods("GET", "POST") - } + r.PathPrefix("/").Handler(http.HandlerFunc(NewMiddlewareChain(ServeFrontofficeHandler, middlewares, a))).Methods("GET", "POST") } func initDebugRoutes(r *mux.Router) {