Support replace feature; close #188; multiple bug fixes on upload

Former-commit-id: 30b18e418111df00903965a7e4135441cd1767a1 [formerly 60393b5e1a291d1aab2313e7dd8cd2e1e203c03b] [formerly c372bc1355db36d2d7b0bc602977616ae52a141c [formerly 6ee846ef0e]]
Former-commit-id: c6c5aa9646c78fe86af74aac00cc1c7c307ad678 [formerly 6d3accd032458203105a474b0f8204d8206c2440]
Former-commit-id: ab35a8effb0b01406bef44328ef3c45f5ea1cbfd
This commit is contained in:
Henrique Dias
2017-08-07 14:44:32 +01:00
parent 2f60562143
commit d6ca579519
10 changed files with 118 additions and 25 deletions

View File

@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
@@ -155,6 +156,12 @@ func resourcePostPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Re
return http.StatusForbidden, nil
}
// Discard any invalid upload before returning to avoid connection
// reset error.
defer func() {
io.Copy(ioutil.Discard, r.Body)
}()
// 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
@@ -171,7 +178,7 @@ func resourcePostPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Re
// 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 r.Method == http.MethodPost && r.Header.Get("Action") != "override" {
if _, err := c.User.FileSystem.Stat(r.URL.Path); err == nil {
return http.StatusConflict, errors.New("There is already a file on that path")
}