2019-01-06 06:44:33 +08:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2021-03-10 01:59:19 +08:00
|
|
|
"io/fs"
|
2019-01-06 06:44:33 +08:00
|
|
|
"net/http"
|
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2019-01-08 18:29:09 +08:00
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
2019-01-06 06:44:33 +08:00
|
|
|
"github.com/filebrowser/filebrowser/v2/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type modifyRequest struct {
|
|
|
|
What string `json:"what"` // Answer to: what data type?
|
|
|
|
Which []string `json:"which"` // Answer to: which fields?
|
|
|
|
}
|
|
|
|
|
2021-03-10 01:59:19 +08:00
|
|
|
func NewHandler(
|
|
|
|
imgSvc ImgService,
|
|
|
|
fileCache FileCache,
|
|
|
|
store *storage.Storage,
|
|
|
|
server *settings.Server,
|
|
|
|
assetsFs fs.FS,
|
|
|
|
) (http.Handler, error) {
|
2019-01-09 16:13:24 +08:00
|
|
|
server.Clean()
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-01-09 16:13:24 +08:00
|
|
|
r := mux.NewRouter()
|
2021-07-26 17:08:39 +08:00
|
|
|
r.Use(func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-05-05 23:38:39 +08:00
|
|
|
w.Header().Set("Content-Security-Policy", `default-src 'self'; style-src 'unsafe-inline';`)
|
2021-07-26 17:08:39 +08:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
})
|
2021-03-10 01:59:19 +08:00
|
|
|
index, static := getStaticHandlers(store, server, assetsFs)
|
2019-01-08 18:29:09 +08:00
|
|
|
|
2019-05-12 16:20:53 +08:00
|
|
|
// NOTE: This fixes the issue where it would redirect if people did not put a
|
|
|
|
// trailing slash in the end. I hate this decision since this allows some awful
|
|
|
|
// URLs https://www.gorillatoolkit.org/pkg/mux#Router.SkipClean
|
|
|
|
r = r.SkipClean(true)
|
|
|
|
|
2019-01-08 18:29:09 +08:00
|
|
|
monkey := func(fn handleFunc, prefix string) http.Handler {
|
2020-06-01 07:12:36 +08:00
|
|
|
return handle(fn, prefix, store, server)
|
2019-01-08 18:29:09 +08:00
|
|
|
}
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2021-03-21 19:30:48 +08:00
|
|
|
r.HandleFunc("/health", healthHandler)
|
2019-01-06 06:44:33 +08:00
|
|
|
r.PathPrefix("/static").Handler(static)
|
|
|
|
r.NotFoundHandler = index
|
|
|
|
|
|
|
|
api := r.PathPrefix("/api").Subrouter()
|
|
|
|
|
2019-01-08 18:29:09 +08:00
|
|
|
api.Handle("/login", monkey(loginHandler, ""))
|
|
|
|
api.Handle("/signup", monkey(signupHandler, ""))
|
|
|
|
api.Handle("/renew", monkey(renewHandler, ""))
|
2019-01-06 06:44:33 +08:00
|
|
|
|
|
|
|
users := api.PathPrefix("/users").Subrouter()
|
2019-01-08 18:29:09 +08:00
|
|
|
users.Handle("", monkey(usersGetHandler, "")).Methods("GET")
|
|
|
|
users.Handle("", monkey(userPostHandler, "")).Methods("POST")
|
|
|
|
users.Handle("/{id:[0-9]+}", monkey(userPutHandler, "")).Methods("PUT")
|
|
|
|
users.Handle("/{id:[0-9]+}", monkey(userGetHandler, "")).Methods("GET")
|
|
|
|
users.Handle("/{id:[0-9]+}", monkey(userDeleteHandler, "")).Methods("DELETE")
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-01-08 18:29:09 +08:00
|
|
|
api.PathPrefix("/resources").Handler(monkey(resourceGetHandler, "/api/resources")).Methods("GET")
|
2020-07-28 17:57:26 +08:00
|
|
|
api.PathPrefix("/resources").Handler(monkey(resourceDeleteHandler(fileCache), "/api/resources")).Methods("DELETE")
|
2021-03-10 23:14:01 +08:00
|
|
|
api.PathPrefix("/resources").Handler(monkey(resourcePostHandler(fileCache), "/api/resources")).Methods("POST")
|
2021-03-10 21:32:11 +08:00
|
|
|
api.PathPrefix("/resources").Handler(monkey(resourcePutHandler, "/api/resources")).Methods("PUT")
|
2021-04-14 23:20:38 +08:00
|
|
|
api.PathPrefix("/resources").Handler(monkey(resourcePatchHandler(fileCache), "/api/resources")).Methods("PATCH")
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2022-06-02 18:52:24 +08:00
|
|
|
api.PathPrefix("/usage").Handler(monkey(diskUsage, "/api/usage")).Methods("GET")
|
|
|
|
|
2020-12-25 02:02:28 +08:00
|
|
|
api.Path("/shares").Handler(monkey(shareListHandler, "/api/shares")).Methods("GET")
|
2019-01-08 18:29:09 +08:00
|
|
|
api.PathPrefix("/share").Handler(monkey(shareGetsHandler, "/api/share")).Methods("GET")
|
|
|
|
api.PathPrefix("/share").Handler(monkey(sharePostHandler, "/api/share")).Methods("POST")
|
|
|
|
api.PathPrefix("/share").Handler(monkey(shareDeleteHandler, "/api/share")).Methods("DELETE")
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-01-08 18:29:09 +08:00
|
|
|
api.Handle("/settings", monkey(settingsGetHandler, "")).Methods("GET")
|
|
|
|
api.Handle("/settings", monkey(settingsPutHandler, "")).Methods("PUT")
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-01-08 18:29:09 +08:00
|
|
|
api.PathPrefix("/raw").Handler(monkey(rawHandler, "/api/raw")).Methods("GET")
|
2020-07-23 18:38:03 +08:00
|
|
|
api.PathPrefix("/preview/{size}/{path:.*}").
|
2020-07-28 01:01:02 +08:00
|
|
|
Handler(monkey(previewHandler(imgSvc, fileCache, server.EnableThumbnails, server.ResizePreview), "/api/preview")).Methods("GET")
|
2019-01-08 18:29:09 +08:00
|
|
|
api.PathPrefix("/command").Handler(monkey(commandsHandler, "/api/command")).Methods("GET")
|
|
|
|
api.PathPrefix("/search").Handler(monkey(searchHandler, "/api/search")).Methods("GET")
|
2019-01-06 07:01:16 +08:00
|
|
|
|
2019-01-06 06:44:33 +08:00
|
|
|
public := api.PathPrefix("/public").Subrouter()
|
2019-01-08 18:29:09 +08:00
|
|
|
public.PathPrefix("/dl").Handler(monkey(publicDlHandler, "/api/public/dl/")).Methods("GET")
|
|
|
|
public.PathPrefix("/share").Handler(monkey(publicShareHandler, "/api/public/share/")).Methods("GET")
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-05-12 16:04:09 +08:00
|
|
|
return stripPrefix(server.BaseURL, r), nil
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|