save file using webdav
This commit is contained in:
parent
10f7ae1d0d
commit
59f5109617
|
@ -417,6 +417,7 @@ var addNewDirEvents = function() {
|
||||||
|
|
||||||
// Handles the new directory event
|
// Handles the new directory event
|
||||||
var newDirEvent = function(event) {
|
var newDirEvent = function(event) {
|
||||||
|
// TODO: create new dir button and new file button
|
||||||
if (event.keyCode == 27) {
|
if (event.keyCode == 27) {
|
||||||
document.getElementById('newdir').classList.toggle('enabled');
|
document.getElementById('newdir').classList.toggle('enabled');
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -831,13 +832,13 @@ document.addEventListener("editor", (event) => {
|
||||||
let data = form2js(document.querySelector('form'));
|
let data = form2js(document.querySelector('form'));
|
||||||
let html = button.changeToLoading();
|
let html = button.changeToLoading();
|
||||||
let request = new XMLHttpRequest();
|
let request = new XMLHttpRequest();
|
||||||
request.open("PUT", window.location);
|
request.open("PUT", toWebDavURL(window.location.pathname));
|
||||||
request.setRequestHeader('Kind', kind);
|
request.setRequestHeader('Kind', kind);
|
||||||
request.setRequestHeader('Token', token);
|
request.setRequestHeader('Token', token);
|
||||||
request.send(JSON.stringify(data));
|
request.send(JSON.stringify(data));
|
||||||
request.onreadystatechange = function() {
|
request.onreadystatechange = function() {
|
||||||
if (request.readyState == 4) {
|
if (request.readyState == 4) {
|
||||||
button.changeToDone((request.status != 200), html);
|
button.changeToDone((request.status != 201), html);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -144,6 +144,8 @@ func (i *Info) serveSingleFile(w http.ResponseWriter, r *http.Request, c *config
|
||||||
}
|
}
|
||||||
|
|
||||||
page.Info.Data = editor
|
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")
|
return page.PrintAsHTML(w, "frontmatter", "editor")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,25 +16,26 @@ import (
|
||||||
|
|
||||||
// Update is used to update a file that was edited
|
// 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) {
|
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{}
|
var data map[string]interface{}
|
||||||
kind := r.Header.Get("kind")
|
kind := r.Header.Get("kind")
|
||||||
|
|
||||||
if kind == "" {
|
var file []byte
|
||||||
return http.StatusBadRequest, nil
|
var code int
|
||||||
}
|
|
||||||
|
|
||||||
// Get the JSON information
|
|
||||||
rawBuffer := new(bytes.Buffer)
|
rawBuffer := new(bytes.Buffer)
|
||||||
rawBuffer.ReadFrom(r.Body)
|
rawBuffer.ReadFrom(r.Body)
|
||||||
|
|
||||||
|
if kind == "" {
|
||||||
|
file = rawBuffer.Bytes()
|
||||||
|
} else {
|
||||||
err := json.Unmarshal(rawBuffer.Bytes(), &data)
|
err := json.Unmarshal(rawBuffer.Bytes(), &data)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var file []byte
|
|
||||||
var code int
|
|
||||||
|
|
||||||
switch kind {
|
switch kind {
|
||||||
case "frontmatter-only":
|
case "frontmatter-only":
|
||||||
if file, code, err = ParseFrontMatterOnlyFile(data, i.Name); err != nil {
|
if file, code, err = ParseFrontMatterOnlyFile(data, i.Name); err != nil {
|
||||||
|
@ -51,13 +52,17 @@ func (i *Info) Update(w http.ResponseWriter, r *http.Request, c *config.Config,
|
||||||
default:
|
default:
|
||||||
return http.StatusBadRequest, nil
|
return http.StatusBadRequest, nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overwrite the Body
|
||||||
|
r.Body = ioutil.NopCloser(bytes.NewReader(file))
|
||||||
|
|
||||||
// Write the file
|
// Write the file
|
||||||
err = ioutil.WriteFile(i.Path, file, 0666)
|
// err = ioutil.WriteFile(i.Path, file, 0666)
|
||||||
|
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
return http.StatusInternalServerError, err
|
//return http.StatusInternalServerError, err
|
||||||
}
|
// }
|
||||||
|
|
||||||
return code, nil
|
return code, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 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)
|
//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 errors.PrintHTML(w, code, err)
|
||||||
}
|
}
|
||||||
return 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:
|
case http.MethodPost:
|
||||||
// Upload a new file.
|
// Upload a new file.
|
||||||
if r.Header.Get("Upload") == "true" {
|
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.
|
// Search and git commands.
|
||||||
if r.Header.Get("Search") == "true" {
|
if r.Header.Get("Search") == "true" {
|
||||||
// TODO: search commands.
|
// TODO: search commands. USE PROPFIND?
|
||||||
}
|
}
|
||||||
|
|
||||||
// VCS commands.
|
// VCS commands.
|
||||||
|
|
Loading…
Reference in New Issue