save file using webdav

pull/144/head
Henrique Dias 2016-10-18 16:17:01 +01:00
parent 10f7ae1d0d
commit 59f5109617
4 changed files with 47 additions and 43 deletions

View File

@ -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);
}
}
}

View File

@ -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")
}

View File

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

View File

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