You've already forked filebrowser
mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-11-26 14:25:26 +08:00
Correct method for new file/directory
Former-commit-id: ba718c759c20ea2fc0b6e443d410a026cdf731e7 [formerly 27ca216e8773da64bd81251d138661d95707bee6] [formerly 2dea38f7a513d75504ca881936853986892fb7ca [formerly d0e7631bc8]]
Former-commit-id: 92ef66d89f03a418479dfff7c8c1e0aca46318f6 [formerly 05be2809de4e8c28616feb41385135a21ce78420]
Former-commit-id: 39f3711265d5e945c0c7c6a88c80ad8603a1aa77
This commit is contained in:
31
api.go
31
api.go
@@ -77,9 +77,11 @@ func resourceHandler(c *requestContext, w http.ResponseWriter, r *http.Request)
|
||||
case http.MethodDelete:
|
||||
return deleteHandler(c, w, r)
|
||||
case http.MethodPut:
|
||||
return putHandler(c, w, r)
|
||||
return postPutHandler(c, w, r)
|
||||
case http.MethodPatch:
|
||||
return patchHandler(c, w, r)
|
||||
case http.MethodPost:
|
||||
return postHandler(c, w, r)
|
||||
return postPutHandler(c, w, r)
|
||||
}
|
||||
|
||||
/* // Execute beforeSave if it is a PUT request.
|
||||
@@ -183,12 +185,30 @@ func deleteHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (i
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
func putHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
func postPutHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// Checks if the current request is for a directory and not a file.
|
||||
if strings.HasSuffix(r.URL.Path, "/") {
|
||||
// If the method is PUT, we return 405 Method not Allowed, because
|
||||
// POST should be used instead.
|
||||
if r.Method == http.MethodPut {
|
||||
return http.StatusMethodNotAllowed, nil
|
||||
}
|
||||
|
||||
// Otherwise we try to create the directory.
|
||||
err := c.us.FileSystem.Mkdir(context.TODO(), r.URL.Path, 0666)
|
||||
return errorToHTTP(err, false), err
|
||||
}
|
||||
|
||||
// If using POST method, we are trying to create a new file so it is not
|
||||
// desirable to override an already existent file. Thus, we check
|
||||
// if the file already exists. If so, we just return a 409 Conflict.
|
||||
if r.Method == http.MethodPost {
|
||||
if _, err := c.us.FileSystem.Stat(context.TODO(), r.URL.Path); err == nil {
|
||||
return http.StatusConflict, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Create/Open the file.
|
||||
f, err := c.us.FileSystem.OpenFile(context.TODO(), r.URL.Path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
defer f.Close()
|
||||
|
||||
@@ -196,22 +216,25 @@ func putHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int,
|
||||
return errorToHTTP(err, false), err
|
||||
}
|
||||
|
||||
// Copies the new content for the file.
|
||||
_, err = io.Copy(f, r.Body)
|
||||
if err != nil {
|
||||
return errorToHTTP(err, false), err
|
||||
}
|
||||
|
||||
// Gets the info about the file.
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
return errorToHTTP(err, false), err
|
||||
}
|
||||
|
||||
// Writes the ETag Header.
|
||||
etag := fmt.Sprintf(`"%x%x"`, fi.ModTime().UnixNano(), fi.Size())
|
||||
w.Header().Set("ETag", etag)
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
func postHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
func patchHandler(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
dst := r.Header.Get("Destination")
|
||||
dst, err := url.QueryUnescape(dst)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user