From f49dd8e205149f779f26fe89d6d56f00e7cfc9d1 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 18 Oct 2016 15:41:04 +0100 Subject: [PATCH] moving to go webdav --- assets/embed/public/js/application.js | 26 ++++++++++------ directory/file.go | 36 ---------------------- filemanager.go | 43 ++++++++++----------------- 3 files changed, 32 insertions(+), 73 deletions(-) diff --git a/assets/embed/public/js/application.js b/assets/embed/public/js/application.js index fb79554d..58145e82 100644 --- a/assets/embed/public/js/application.js +++ b/assets/embed/public/js/application.js @@ -86,6 +86,11 @@ Element.prototype.changeToDone = function(error, html) { return false; } +var toWebDavURL = function(url) { + url = url.replace("/", "/webdav/") + return window.location.origin + url +} + // Handles the open file button event var openEvent = function(event) { if (this.classList.contains('disabled')) { @@ -115,12 +120,12 @@ var deleteEvent = function(event) { let html = document.getElementById("delete").changeToLoading(); let request = new XMLHttpRequest(); - request.open('DELETE', link); + request.open('DELETE', toWebDavURL(link)); request.setRequestHeader('Token', token); request.send(); request.onreadystatechange = function() { if (request.readyState == 4) { - if (request.status == 200) { + if (request.status == 204) { if (single) { window.location.pathname = RemoveLastDirectoryPartOf(window.location.pathname); } else { @@ -128,7 +133,7 @@ var deleteEvent = function(event) { selectedItems.removeElement(link); } } - document.getElementById('delete').changeToDone((request.status != 200), html); + document.getElementById('delete').changeToDone((request.status != 204), html); } } }); @@ -233,19 +238,22 @@ var renameEvent = function(event) { let keyDownEvent = (event) => { if (event.keyCode == 13) { let newName = span.innerHTML; + let newLink = toWebDavURL(link).replace(name, newName) let html = document.getElementById('rename').changeToLoading(); let request = new XMLHttpRequest(); - request.open('PATCH', link); - request.setRequestHeader('Rename-To', newName); + request.open('MOVE', toWebDavURL(link)); + request.setRequestHeader('Destination', newLink); request.setRequestHeader('Token', token); request.send(); request.onreadystatechange = function() { + // TODO: redirect if it's moved to another folder + if (request.readyState == 4) { - if (request.status != 200) { + if (request.status != 201 && request.status != 204) { span.innerHTML = name; } else { let newLink = encodeURI(link.replace(name, newName)); - console.log(newLink) + console.log(request.body) reloadListing(() => { let newLink = encodeURI(link.replace(name, newName)); selectedItems = [newLink]; @@ -255,7 +263,7 @@ var renameEvent = function(event) { }); } - document.getElementById('rename').changeToDone((request.status != 200), html); + document.getElementById('rename').changeToDone((request.status != 201 && request.status != 204), html); } } } @@ -900,4 +908,4 @@ document.addEventListener("DOMContentLoaded", function(event) { } return false; -}); \ No newline at end of file +}); diff --git a/directory/file.go b/directory/file.go index cfcd728f..a68936f1 100644 --- a/directory/file.go +++ b/directory/file.go @@ -106,42 +106,6 @@ func (i Info) HumanModTime(format string) string { return i.ModTime.Format(format) } -// Delete handles the delete requests -func (i *Info) Delete() (int, error) { - var err error - - // If it's a directory remove all the contents inside - if i.IsDir { - err = os.RemoveAll(i.Path) - } else { - err = os.Remove(i.Path) - } - - if err != nil { - return errors.ToHTTPCode(err), err - } - - return http.StatusOK, nil -} - -// Rename function is used tor rename a file or a directory -func (i *Info) Rename(w http.ResponseWriter, r *http.Request) (int, error) { - newname := r.Header.Get("Rename-To") - if newname == "" { - return http.StatusBadRequest, nil - } - - newpath := filepath.Clean(newname) - newpath = strings.Replace(i.Path, i.Name, newname, 1) - - if err := os.Rename(i.Path, newpath); err != nil { - return errors.ToHTTPCode(err), err - } - - i.Path = newpath - return http.StatusOK, nil -} - // ServeAsHTML is used to serve single file pages func (i *Info) ServeAsHTML(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User) (int, error) { if i.IsDir { diff --git a/filemanager.go b/filemanager.go index 70f09069..97811360 100644 --- a/filemanager.go +++ b/filemanager.go @@ -58,22 +58,23 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err } if c.WebDav && strings.HasPrefix(r.URL.Path, c.WebDavURL) { - url := strings.TrimPrefix(r.URL.Path, c.WebDavURL) + //url := strings.TrimPrefix(r.URL.Path, c.WebDavURL) - if !user.Allowed(url) { - return http.StatusForbidden, nil - } - - switch r.Method { - case "PROPPATCH", "MOVE", "PATCH", "PUT", "DELETE": - if !user.AllowEdit { + /* + if !user.Allowed(url) { return http.StatusForbidden, nil } - case "MKCOL", "COPY": - if !user.AllowNew { - return http.StatusForbidden, nil - } - } + + switch r.Method { + case "PROPPATCH", "MOVE", "PATCH", "PUT", "DELETE": + if !user.AllowEdit { + return http.StatusForbidden, nil + } + case "MKCOL", "COPY": + if !user.AllowNew { + return http.StatusForbidden, nil + } + } */ c.WebDavHandler.ServeHTTP(w, r) return 0, nil @@ -149,7 +150,7 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err } if !user.AllowEdit { - return http.StatusUnauthorized, nil + return http.StatusForbidden, nil } // Update a file. @@ -180,20 +181,6 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err // Creates a new folder. return newDirectory(w, r, c) - case http.MethodDelete: - if !user.AllowEdit { - return http.StatusUnauthorized, nil - } - - // Delete a file or a directory - return fi.Delete() - case http.MethodPatch: - if !user.AllowEdit { - return http.StatusUnauthorized, nil - } - - // Rename a file or directory - return fi.Rename(w, r) default: return http.StatusNotImplemented, nil }