diff --git a/assets/embed/public/js/application.js b/assets/embed/public/js/application.js index 30246b62..e326f9b4 100644 --- a/assets/embed/public/js/application.js +++ b/assets/embed/public/js/application.js @@ -417,6 +417,7 @@ var addNewDirEvents = function() { // Handles the new directory event var newDirEvent = function(event) { + // TODO: create new dir button and new file button if (event.keyCode == 27) { document.getElementById('newdir').classList.toggle('enabled'); setTimeout(() => { @@ -831,13 +832,13 @@ document.addEventListener("editor", (event) => { let data = form2js(document.querySelector('form')); let html = button.changeToLoading(); let request = new XMLHttpRequest(); - request.open("PUT", window.location); + request.open("PUT", toWebDavURL(window.location.pathname)); request.setRequestHeader('Kind', kind); request.setRequestHeader('Token', token); request.send(JSON.stringify(data)); request.onreadystatechange = function() { if (request.readyState == 4) { - button.changeToDone((request.status != 200), html); + button.changeToDone((request.status != 201), html); } } } diff --git a/directory/file.go b/directory/file.go index a68936f1..240c809a 100644 --- a/directory/file.go +++ b/directory/file.go @@ -144,6 +144,8 @@ func (i *Info) serveSingleFile(w http.ResponseWriter, r *http.Request, c *config } page.Info.Data = editor + + // TODO: if serve Single File finds an error while parsing, show the raw content to edit instead of giving 500 return page.PrintAsHTML(w, "frontmatter", "editor") } diff --git a/directory/update.go b/directory/update.go index 7a0f5c3a..34d1ba15 100644 --- a/directory/update.go +++ b/directory/update.go @@ -16,48 +16,53 @@ import ( // Update is used to update a file that was edited func (i *Info) Update(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User) (int, error) { + // TODO: review this + var data map[string]interface{} kind := r.Header.Get("kind") - if kind == "" { - return http.StatusBadRequest, nil - } - - // Get the JSON information - rawBuffer := new(bytes.Buffer) - rawBuffer.ReadFrom(r.Body) - err := json.Unmarshal(rawBuffer.Bytes(), &data) - - if err != nil { - return http.StatusInternalServerError, err - } - var file []byte var code int - switch kind { - case "frontmatter-only": - if file, code, err = ParseFrontMatterOnlyFile(data, i.Name); err != nil { + rawBuffer := new(bytes.Buffer) + rawBuffer.ReadFrom(r.Body) + + if kind == "" { + file = rawBuffer.Bytes() + } else { + err := json.Unmarshal(rawBuffer.Bytes(), &data) + + if err != nil { return http.StatusInternalServerError, err } - case "content-only": - mainContent := data["content"].(string) - mainContent = strings.TrimSpace(mainContent) - file = []byte(mainContent) - case "complete": - if file, code, err = ParseCompleteFile(data, i.Name, u.FrontMatter); err != nil { - return http.StatusInternalServerError, err + + switch kind { + case "frontmatter-only": + if file, code, err = ParseFrontMatterOnlyFile(data, i.Name); err != nil { + return http.StatusInternalServerError, err + } + case "content-only": + mainContent := data["content"].(string) + mainContent = strings.TrimSpace(mainContent) + file = []byte(mainContent) + case "complete": + if file, code, err = ParseCompleteFile(data, i.Name, u.FrontMatter); err != nil { + return http.StatusInternalServerError, err + } + default: + return http.StatusBadRequest, nil } - default: - return http.StatusBadRequest, nil } + // Overwrite the Body + r.Body = ioutil.NopCloser(bytes.NewReader(file)) + // Write the file - err = ioutil.WriteFile(i.Path, file, 0666) + // err = ioutil.WriteFile(i.Path, file, 0666) - if err != nil { - return http.StatusInternalServerError, err - } + //if err != nil { + //return http.StatusInternalServerError, err + // } return code, nil } diff --git a/filemanager.go b/filemanager.go index 6ed614c5..3bf02019 100644 --- a/filemanager.go +++ b/filemanager.go @@ -57,6 +57,13 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err } if c.WebDav && strings.HasPrefix(r.URL.Path, c.WebDavURL) { + if r.Method == http.MethodPut { + _, err = fi.Update(w, r, c, user) + if err != nil { + return http.StatusInternalServerError, err + } + } + //url := strings.TrimPrefix(r.URL.Path, c.WebDavURL) /* @@ -143,17 +150,6 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err return errors.PrintHTML(w, code, err) } return code, err - case http.MethodPut: - if fi.IsDir { - return http.StatusNotAcceptable, nil - } - - if !user.AllowEdit { - return http.StatusForbidden, nil - } - - // Update a file. - return fi.Update(w, r, c, user) case http.MethodPost: // Upload a new file. if r.Header.Get("Upload") == "true" { @@ -166,7 +162,7 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err // Search and git commands. if r.Header.Get("Search") == "true" { - // TODO: search commands. + // TODO: search commands. USE PROPFIND? } // VCS commands.