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:
Henrique Dias 2017-07-04 21:02:46 +01:00
parent 8a71ad00fd
commit d5cd86da97
6 changed files with 54 additions and 9 deletions

View File

@ -164,7 +164,7 @@ export default {
let promises = []
for (let file of files) {
promises.push(api.put(this.$route.path + base + file.name, file))
promises.push(api.post(this.$route.path + base + file.name, file))
}
Promise.all(promises)

View File

@ -202,7 +202,7 @@ export default {
// Del!
if (event.keyCode === 46) {
if (this.showDeleteButton()) {
if (this.showDeleteButton) {
this.$store.commit('showHover', 'delete')
}
}
@ -215,7 +215,7 @@ export default {
// F2!
if (event.keyCode === 113) {
if (this.showRenameButton()) {
if (this.showRenameButton) {
this.$store.commit('showHover', 'rename')
}
}

View File

@ -34,7 +34,7 @@ export default {
uri += this.name + '/'
uri = uri.replace('//', '/')
api.put(uri)
api.post(uri)
.then(() => {
this.$router.push({ path: uri })
})

View File

@ -34,7 +34,7 @@ export default {
uri += this.name
uri = uri.replace('//', '/')
api.put(uri)
api.post(uri)
.then(() => {
this.$router.push({ path: uri })
})

View File

@ -57,6 +57,27 @@ function rm (url) {
})
}
function post (url, content = '') {
url = removePrefix(url)
return new Promise((resolve, reject) => {
let request = new window.XMLHttpRequest()
request.open('POST', `${store.state.baseURL}/api/resource${url}`, true)
request.setRequestHeader('Authorization', `Bearer ${store.state.jwt}`)
request.onload = () => {
if (request.status === 200) {
resolve(request.responseText)
} else {
reject(request.responseText)
}
}
request.onerror = (error) => reject(error)
request.send(content)
})
}
function put (url, content = '') {
url = removePrefix(url)
@ -175,6 +196,7 @@ export default {
checksum,
move,
put,
post,
command,
search,
download

31
api.go
View File

@ -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 {