mirror of
https://github.com/filebrowser/filebrowser.git
synced 2026-01-23 02:35:10 +00:00
FS as an interface, close #205
Former-commit-id: 9bfcbeaf9c407044fb8eb3142f2eca65f42623d1 [formerly 2fb1a0292e825b5b86e506c70b548bc823050f7d] [formerly 8f70cdc0db6d4328d61769685bb39806999f475b [formerly 20818dca93]]
Former-commit-id: ec8e8d96356d56863d8a330451dc78dbf838a7ef [formerly 216fffad3e0d2cdc4632e8de9299666a74e44375]
Former-commit-id: 230c7bc974a86225064211178250ec072c0a525a
This commit is contained in:
parent
600723c224
commit
d0cf6c08e8
8 changed files with 64 additions and 36 deletions
|
|
@ -30,7 +30,7 @@ func authHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, er
|
|||
}
|
||||
|
||||
// Checks if the user exists.
|
||||
u, err := c.Store.Users.Get(cred.ID)
|
||||
u, err := c.Store.Users.Get(cred.ID, c.NewFS)
|
||||
if err != nil {
|
||||
return http.StatusForbidden, nil
|
||||
}
|
||||
|
|
@ -137,7 +137,7 @@ func validateAuth(c *fm.Context, r *http.Request) (bool, *fm.User) {
|
|||
return false, nil
|
||||
}
|
||||
|
||||
u, err := c.Store.Users.Get(claims.User.ID)
|
||||
u, err := c.Store.Users.Get(claims.User.ID, c.NewFS)
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ func resourceHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int
|
|||
return resourceDeleteHandler(c, w, r)
|
||||
case http.MethodPut:
|
||||
// Before save command handler.
|
||||
path := filepath.Join(string(c.User.FileSystem), r.URL.Path)
|
||||
path := filepath.Join(c.User.Scope, r.URL.Path)
|
||||
if err := c.Runner("before_save", path); err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
|
@ -253,7 +253,7 @@ func resourcePublishSchedule(c *fm.Context, w http.ResponseWriter, r *http.Reque
|
|||
}
|
||||
|
||||
func resourcePublish(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
path := filepath.Join(string(c.User.FileSystem), r.URL.Path)
|
||||
path := filepath.Join(c.User.Scope, r.URL.Path)
|
||||
|
||||
// Before save command handler.
|
||||
if err := c.Runner("before_publish", path); err != nil {
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ func shareHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, e
|
|||
}
|
||||
|
||||
func shareGetHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
path := filepath.Join(string(c.User.FileSystem), r.URL.Path)
|
||||
path := filepath.Join(c.User.Scope, r.URL.Path)
|
||||
s, err := c.Store.Share.GetByPath(path)
|
||||
if err == storm.ErrNotFound {
|
||||
return http.StatusNotFound, nil
|
||||
|
|
@ -49,7 +49,7 @@ func shareGetHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int
|
|||
}
|
||||
|
||||
func sharePostHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
path := filepath.Join(string(c.User.FileSystem), r.URL.Path)
|
||||
path := filepath.Join(c.User.Scope, r.URL.Path)
|
||||
|
||||
var s *fm.ShareLink
|
||||
expire := r.URL.Query().Get("expires")
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ func getUserID(r *http.Request) (int, error) {
|
|||
// getUser returns the user which is present in the request
|
||||
// body. If the body is empty or the JSON is invalid, it
|
||||
// returns an fm.Error.
|
||||
func getUser(r *http.Request) (*fm.User, string, error) {
|
||||
func getUser(c *fm.Context, r *http.Request) (*fm.User, string, error) {
|
||||
// Checks if the request body is empty.
|
||||
if r.Body == nil {
|
||||
return nil, "", fm.ErrEmptyRequest
|
||||
|
|
@ -82,6 +82,7 @@ func getUser(r *http.Request) (*fm.User, string, error) {
|
|||
return nil, "", fm.ErrWrongDataType
|
||||
}
|
||||
|
||||
mod.Data.FileSystem = c.NewFS(mod.Data.Scope)
|
||||
return mod.Data, mod.Which, nil
|
||||
}
|
||||
|
||||
|
|
@ -93,7 +94,7 @@ func usersGetHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int
|
|||
|
||||
// Request for the listing of users.
|
||||
if r.URL.Path == "/" {
|
||||
users, err := c.Store.Users.Gets()
|
||||
users, err := c.Store.Users.Gets(c.NewFS)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
|
@ -116,7 +117,7 @@ func usersGetHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int
|
|||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
u, err := c.Store.Users.Get(id)
|
||||
u, err := c.Store.Users.Get(id, c.NewFS)
|
||||
if err == fm.ErrExist {
|
||||
return http.StatusNotFound, err
|
||||
}
|
||||
|
|
@ -134,7 +135,7 @@ func usersPostHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (in
|
|||
return http.StatusMethodNotAllowed, nil
|
||||
}
|
||||
|
||||
u, _, err := getUser(r)
|
||||
u, _, err := getUser(c, r)
|
||||
if err != nil {
|
||||
return http.StatusBadRequest, err
|
||||
}
|
||||
|
|
@ -144,8 +145,8 @@ func usersPostHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (in
|
|||
return http.StatusBadRequest, fm.ErrEmptyUsername
|
||||
}
|
||||
|
||||
// Checks if filesystem isn't empty.
|
||||
if u.FileSystem == "" {
|
||||
// Checks if scope isn't empty.
|
||||
if u.Scope == "" {
|
||||
return http.StatusBadRequest, fm.ErrEmptyScope
|
||||
}
|
||||
|
||||
|
|
@ -154,11 +155,6 @@ func usersPostHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (in
|
|||
return http.StatusBadRequest, fm.ErrEmptyPassword
|
||||
}
|
||||
|
||||
// The username, password and scope cannot be empty.
|
||||
if u.Username == "" || u.Password == "" || u.FileSystem == "" {
|
||||
return http.StatusBadRequest, errors.New("username, password or scope is empty")
|
||||
}
|
||||
|
||||
// Initialize rules if they're not initialized.
|
||||
if u.Rules == nil {
|
||||
u.Rules = []*fm.Rule{}
|
||||
|
|
@ -175,7 +171,7 @@ func usersPostHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (in
|
|||
}
|
||||
|
||||
// Checks if the scope exists.
|
||||
if code, err := checkFS(string(u.FileSystem)); err != nil {
|
||||
if code, err := checkFS(u.Scope); err != nil {
|
||||
return code, err
|
||||
}
|
||||
|
||||
|
|
@ -267,7 +263,7 @@ func usersPutHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int
|
|||
}
|
||||
|
||||
// Gets the user from the request body.
|
||||
u, which, err := getUser(r)
|
||||
u, which, err := getUser(c, r)
|
||||
if err != nil {
|
||||
return http.StatusBadRequest, err
|
||||
}
|
||||
|
|
@ -315,12 +311,12 @@ func usersPutHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int
|
|||
}
|
||||
|
||||
// Checks if filesystem isn't empty.
|
||||
if u.FileSystem == "" {
|
||||
if u.Scope == "" {
|
||||
return http.StatusBadRequest, fm.ErrEmptyScope
|
||||
}
|
||||
|
||||
// Checks if the scope exists.
|
||||
if code, err := checkFS(string(u.FileSystem)); err != nil {
|
||||
if code, err := checkFS(u.Scope); err != nil {
|
||||
return code, err
|
||||
}
|
||||
|
||||
|
|
@ -335,7 +331,7 @@ func usersPutHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int
|
|||
}
|
||||
|
||||
// Gets the current saved user from the in-memory map.
|
||||
suser, err := c.Store.Users.Get(id)
|
||||
suser, err := c.Store.Users.Get(id, c.NewFS)
|
||||
if err == fm.ErrNotExist {
|
||||
return http.StatusNotFound, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ func command(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error)
|
|||
}
|
||||
|
||||
// Gets the path and initializes a buffer.
|
||||
path := string(c.User.FileSystem) + "/" + r.URL.Path
|
||||
path := c.User.Scope + "/" + r.URL.Path
|
||||
path = filepath.Clean(path)
|
||||
buff := new(bytes.Buffer)
|
||||
|
||||
|
|
@ -270,7 +270,7 @@ func search(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error)
|
|||
search = parseSearch(value)
|
||||
scope := strings.TrimPrefix(r.URL.Path, "/")
|
||||
scope = "/" + scope
|
||||
scope = string(c.User.FileSystem) + scope
|
||||
scope = c.User.Scope + scope
|
||||
scope = strings.Replace(scope, "\\", "/", -1)
|
||||
scope = filepath.Clean(scope)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue