Always using webdav

pull/144/head
Henrique Dias 2016-10-18 16:42:48 +01:00
parent 59f5109617
commit 3683c4c06a
5 changed files with 46 additions and 42 deletions

View File

@ -774,7 +774,7 @@ header .action span {
border: 0; border: 0;
box-shadow: 0 1px 3px rgba(0, 0, 0, .12), 0 1px 2px rgba(0, 0, 0, .24); box-shadow: 0 1px 3px rgba(0, 0, 0, .12), 0 1px 2px rgba(0, 0, 0, .24);
padding: .5em; padding: .5em;
width: 10em; width: 22em;
border-radius: .2em; border-radius: .2em;
} }

View File

@ -431,7 +431,9 @@ var newDirEvent = function(event) {
let button = document.getElementById('new'); let button = document.getElementById('new');
let html = button.changeToLoading(); let html = button.changeToLoading();
let request = new XMLHttpRequest(); let request = new XMLHttpRequest();
request.open("MKCOL", toWebDavURL(window.location.pathname + document.getElementById('newdir').value + "/")); let name = document.getElementById('newdir').value;
request.open((name.endsWith("/") ? "MKCOL" : "PUT"), toWebDavURL(window.location.pathname + name));
request.setRequestHeader('Token', token); request.setRequestHeader('Token', token);
request.send(); request.send();
request.onreadystatechange = function() { request.onreadystatechange = function() {

View File

@ -38,7 +38,7 @@
{{ end }} {{ end }}
{{ if .User.AllowNew }} {{ if .User.AllowNew }}
<input id="newdir" type="text" placeholder="Name..."> <input id="newdir" type="text" placeholder="Name. End with a trailing slash to create a dir.">
<div class="floating"> <div class="floating">
<div class="action" id="new"> <div class="action" id="new">
<i class="material-icons" title="New file or directory. If you don't write an extension, a directory will be created.">add</i> <i class="material-icons" title="New file or directory. If you don't write an extension, a directory will be created.">add</i>

View File

@ -69,6 +69,7 @@ func Parse(c *caddy.Controller) ([]Config, error) {
cfg.AllowEdit = true cfg.AllowEdit = true
cfg.AllowNew = true cfg.AllowNew = true
cfg.Commands = []string{"git", "svn", "hg"} cfg.Commands = []string{"git", "svn", "hg"}
cfg.WebDav = true
cfg.Rules = []*Rule{&Rule{ cfg.Rules = []*Rule{&Rule{
Regex: true, Regex: true,
Allow: false, Allow: false,
@ -85,6 +86,7 @@ func Parse(c *caddy.Controller) ([]Config, error) {
cfg.BaseURL = strings.TrimPrefix(cfg.BaseURL, "/") cfg.BaseURL = strings.TrimPrefix(cfg.BaseURL, "/")
cfg.BaseURL = strings.TrimSuffix(cfg.BaseURL, "/") cfg.BaseURL = strings.TrimSuffix(cfg.BaseURL, "/")
cfg.BaseURL = "/" + cfg.BaseURL cfg.BaseURL = "/" + cfg.BaseURL
cfg.WebDavURL = cfg.BaseURL + "webdav"
if cfg.BaseURL == "/" { if cfg.BaseURL == "/" {
cfg.BaseURL = "" cfg.BaseURL = ""
@ -105,23 +107,15 @@ func Parse(c *caddy.Controller) ([]Config, error) {
return configs, c.Err("frontmatter type not supported") return configs, c.Err("frontmatter type not supported")
} }
case "webdav": case "webdav":
cfg.WebDav = true if !c.NextArg() {
return configs, c.ArgErr()
prefix := "webdav"
if c.NextArg() {
prefix = c.Val()
} }
prefix := c.Val()
prefix = strings.TrimPrefix(prefix, "/") prefix = strings.TrimPrefix(prefix, "/")
prefix = strings.TrimSuffix(prefix, "/") prefix = strings.TrimSuffix(prefix, "/")
prefix = cfg.BaseURL + "/" + prefix prefix = cfg.BaseURL + "/" + prefix
cfg.WebDavURL = prefix cfg.WebDavURL = prefix
cfg.WebDavHandler = &webdav.Handler{
Prefix: prefix,
FileSystem: webdav.Dir(cfg.PathScope),
LockSystem: webdav.NewMemLS(),
}
case "show": case "show":
if !c.NextArg() { if !c.NextArg() {
return configs, c.ArgErr() return configs, c.ArgErr()
@ -240,6 +234,12 @@ func Parse(c *caddy.Controller) ([]Config, error) {
} }
} }
cfg.WebDavHandler = &webdav.Handler{
Prefix: cfg.WebDavURL,
FileSystem: webdav.Dir(cfg.PathScope),
LockSystem: webdav.NewMemLS(),
}
caddyConf := httpserver.GetConfig(c) caddyConf := httpserver.GetConfig(c)
cfg.AbsoluteURL = strings.TrimSuffix(caddyConf.Addr.Path, "/") + "/" + cfg.BaseURL cfg.AbsoluteURL = strings.TrimSuffix(caddyConf.Addr.Path, "/") + "/" + cfg.BaseURL
cfg.AbsoluteURL = strings.Replace(cfg.AbsoluteURL, "//", "/", -1) cfg.AbsoluteURL = strings.Replace(cfg.AbsoluteURL, "//", "/", -1)

View File

@ -56,7 +56,24 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
user = c.User user = c.User
} }
if c.WebDav && strings.HasPrefix(r.URL.Path, c.WebDavURL) { if strings.HasPrefix(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 {
return http.StatusForbidden, nil
}
case "MKCOL", "COPY":
if !user.AllowNew {
return http.StatusForbidden, nil
}
}
if r.Method == http.MethodPut { if r.Method == http.MethodPut {
_, err = fi.Update(w, r, c, user) _, err = fi.Update(w, r, c, user)
if err != nil { if err != nil {
@ -64,24 +81,6 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
} }
} }
//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 {
return http.StatusForbidden, nil
}
case "MKCOL", "COPY":
if !user.AllowNew {
return http.StatusForbidden, nil
}
} */
c.WebDavHandler.ServeHTTP(w, r) c.WebDavHandler.ServeHTTP(w, r)
return 0, nil return 0, nil
} }
@ -122,9 +121,7 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
} }
} }
// Route the request depending on the HTTP Method. if r.Method == http.MethodGet {
switch r.Method {
case http.MethodGet:
// Read and show directory or file. // Read and show directory or file.
if serveAssets { if serveAssets {
return assets.Serve(w, r, c) return assets.Serve(w, r, c)
@ -136,13 +133,18 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
if !fi.IsDir { if !fi.IsDir {
query := r.URL.Query() query := r.URL.Query()
if val, ok := query["raw"]; ok && val[0] == "true" { if val, ok := query["raw"]; ok && val[0] == "true" {
// TODO: change URL to webdav and continue as webdav
return fi.ServeRawFile(w, r, c) return fi.ServeRawFile(w, r, c)
} }
if val, ok := query["download"]; ok && val[0] == "true" { if val, ok := query["download"]; ok && val[0] == "true" {
w.Header().Set("Content-Disposition", "attachment; filename="+fi.Name) w.Header().Set("Content-Disposition", "attachment; filename="+fi.Name)
// TODO: change URL to webdav and continue as webdav
return fi.ServeRawFile(w, r, c) return fi.ServeRawFile(w, r, c)
} }
// c.WebDavHandler.ServeHTTP(w, r)
} }
code, err := fi.ServeAsHTML(w, r, c, user) code, err := fi.ServeAsHTML(w, r, c, user)
@ -150,7 +152,9 @@ 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.MethodPost: }
if r.Method == http.MethodPost {
// Upload a new file. // Upload a new file.
if r.Header.Get("Upload") == "true" { if r.Header.Get("Upload") == "true" {
if !user.AllowNew { if !user.AllowNew {
@ -173,11 +177,9 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
return command(w, r, c, user) return command(w, r, c, user)
} }
fallthrough
default:
return http.StatusNotImplemented, nil
} }
return http.StatusNotImplemented, nil
} }
} }