diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-11-15 00:39:41 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-11-15 00:39:41 -0500 |
commit | 17b0b282a2a17da1ed54c028fdf18b6760c7b129 (patch) | |
tree | 664aea81c28415d8092a58bdc56c5de86428547d | |
parent | 06001af5fe64466bb2f26897ca2d72152d9ac06b (diff) |
add support for creating files
-rw-r--r-- | got/view_blob.got | 4 | ||||
-rw-r--r-- | src/edit/dir.go | 42 | ||||
-rw-r--r-- | src/edit/views.go | 15 |
3 files changed, 42 insertions, 19 deletions
diff --git a/got/view_blob.got b/got/view_blob.got index 752d5c7..16d7ff1 100644 --- a/got/view_blob.got +++ b/got/view_blob.got @@ -1,6 +1,10 @@ <!-- -*- Mode: HTML -*- --> <h1>{{.path | html}}</h1> <p>Content-Type: {{.ctype | html}}</p> +{{if not .exists}} +<aside><p>This file does not exist, but it can be created by + submitting this page.</p></aside> +{{end}} {{if istext .ctype}} <form method="POST" enctype="multipart/form-data"> <input type="hidden" name="_method" value="PUT"> diff --git a/src/edit/dir.go b/src/edit/dir.go index d07eb78..8315dc0 100644 --- a/src/edit/dir.go +++ b/src/edit/dir.go @@ -22,20 +22,14 @@ func ServeGit(out http.ResponseWriter, in *http.Request) { tree, err := GitLsTree() errcheck(err) file, fileExists := tree[upath] - if in.Method != http.MethodPut { - if !fileExists { - http.NotFound(out, in) + if file.Type == "tree" && in.Method != http.MethodPut { + if !strings.HasSuffix(in.URL.Path, "/") { + out.Header().Set("Location", path.Base(upath)+"/") + out.WriteHeader(http.StatusMovedPermanently) return } - if file.Type == "tree" { - if !strings.HasSuffix(in.URL.Path, "/") { - out.Header().Set("Location", path.Base(upath)+"/") - out.WriteHeader(http.StatusMovedPermanently) - return - } - if !strings.HasSuffix(upath, "/") { - upath = upath + "/" - } + if !strings.HasSuffix(upath, "/") { + upath = upath + "/" } } @@ -45,9 +39,17 @@ func ServeGit(out http.ResponseWriter, in *http.Request) { if file.Type == "tree" { errcheck(renderViewTree(out, upath, tree)) } else { - errcheck(renderViewBlob(out, upath, file)) + if fileExists { + errcheck(renderViewBlob(out, upath, &file)) + } else { + errcheck(renderViewBlob(out, upath, nil)) + } + } + if fileExists { + out.WriteHeader(http.StatusOK) + } else { + out.WriteHeader(http.StatusNotFound) } - out.WriteHeader(http.StatusOK) case http.MethodPut: username := in.Header.Get("X-Nginx-User") userinfo, err := user.Lookup(username) @@ -76,6 +78,10 @@ func ServeGit(out http.ResponseWriter, in *http.Request) { out.WriteHeader(http.StatusCreated) } case http.MethodDelete: + if !fileExists { + http.NotFound(out, in) + return + } username := in.Header.Get("X-Nginx-User") userinfo, err := user.Lookup(username) errcheck(err) @@ -97,10 +103,18 @@ func ServeGit(out http.ResponseWriter, in *http.Request) { errcheck(renderDeleted(out, upath)) out.WriteHeader(http.StatusOK) case http.MethodOptions: + if !fileExists { + http.NotFound(out, in) + return + } // POST because PostHack out.Header().Set("Allow", "GET, HEAD, PUT, POST, DELETE, OPTIONS") out.WriteHeader(http.StatusOK) default: + if !fileExists { + http.NotFound(out, in) + return + } out.Header().Set("Allow", "GET, HEAD, PUT, POST, DELETE, OPTIONS") out.WriteHeader(http.StatusMethodNotAllowed) } diff --git a/src/edit/views.go b/src/edit/views.go index a2890d4..3f55fa1 100644 --- a/src/edit/views.go +++ b/src/edit/views.go @@ -61,19 +61,24 @@ func renderViewTree(w io.Writer, upath string, tree GitTree) error { return renderPage(w, upath, "", body.String(), "") } -func renderViewBlob(w io.Writer, upath string, file GitFile) error { +func renderViewBlob(w io.Writer, upath string, file *GitFile) error { + var err error + var content []byte // Pre-processing - content, err := file.Cat() - if err != nil { - return err + if file != nil { + content, err = file.Cat() + if err != nil { + return err + } } ctype := getctype(upath, content) // Component render var body bytes.Buffer - err = tmplViewBlob.Execute(&body, map[string]string{ + err = tmplViewBlob.Execute(&body, map[string]interface{}{ "path": upath, "ctype": ctype, "content": string(content), + "exists": file != nil, }) if err != nil { return err |