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;
box-shadow: 0 1px 3px rgba(0, 0, 0, .12), 0 1px 2px rgba(0, 0, 0, .24);
padding: .5em;
width: 10em;
width: 22em;
border-radius: .2em;
}
@ -1166,4 +1166,4 @@ i.spin {
column-count: 1;
column-gap: 0;
}
}
}

View File

@ -431,7 +431,9 @@ var newDirEvent = function(event) {
let button = document.getElementById('new');
let html = button.changeToLoading();
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.send();
request.onreadystatechange = function() {

View File

@ -38,7 +38,7 @@
{{ end }}
{{ 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="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>

View File

@ -69,6 +69,7 @@ func Parse(c *caddy.Controller) ([]Config, error) {
cfg.AllowEdit = true
cfg.AllowNew = true
cfg.Commands = []string{"git", "svn", "hg"}
cfg.WebDav = true
cfg.Rules = []*Rule{&Rule{
Regex: true,
Allow: false,
@ -85,6 +86,7 @@ func Parse(c *caddy.Controller) ([]Config, error) {
cfg.BaseURL = strings.TrimPrefix(cfg.BaseURL, "/")
cfg.BaseURL = strings.TrimSuffix(cfg.BaseURL, "/")
cfg.BaseURL = "/" + cfg.BaseURL
cfg.WebDavURL = cfg.BaseURL + "webdav"
if cfg.BaseURL == "/" {
cfg.BaseURL = ""
@ -105,23 +107,15 @@ func Parse(c *caddy.Controller) ([]Config, error) {
return configs, c.Err("frontmatter type not supported")
}
case "webdav":
cfg.WebDav = true
prefix := "webdav"
if c.NextArg() {
prefix = c.Val()
if !c.NextArg() {
return configs, c.ArgErr()
}
prefix := c.Val()
prefix = strings.TrimPrefix(prefix, "/")
prefix = strings.TrimSuffix(prefix, "/")
prefix = cfg.BaseURL + "/" + prefix
cfg.WebDavURL = prefix
cfg.WebDavHandler = &webdav.Handler{
Prefix: prefix,
FileSystem: webdav.Dir(cfg.PathScope),
LockSystem: webdav.NewMemLS(),
}
case "show":
if !c.NextArg() {
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)
cfg.AbsoluteURL = strings.TrimSuffix(caddyConf.Addr.Path, "/") + "/" + cfg.BaseURL
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
}
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 {
_, err = fi.Update(w, r, c, user)
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)
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.
switch r.Method {
case http.MethodGet:
if r.Method == http.MethodGet {
// Read and show directory or file.
if serveAssets {
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 {
query := r.URL.Query()
if val, ok := query["raw"]; ok && val[0] == "true" {
// TODO: change URL to webdav and continue as webdav
return fi.ServeRawFile(w, r, c)
}
if val, ok := query["download"]; ok && val[0] == "true" {
w.Header().Set("Content-Disposition", "attachment; filename="+fi.Name)
// TODO: change URL to webdav and continue as webdav
return fi.ServeRawFile(w, r, c)
}
// c.WebDavHandler.ServeHTTP(w, r)
}
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 code, err
case http.MethodPost:
}
if r.Method == http.MethodPost {
// Upload a new file.
if r.Header.Get("Upload") == "true" {
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)
}
fallthrough
default:
return http.StatusNotImplemented, nil
}
return http.StatusNotImplemented, nil
}
}