From 17b0b282a2a17da1ed54c028fdf18b6760c7b129 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 15 Nov 2016 00:39:41 -0500 Subject: add support for creating files --- got/view_blob.got | 4 ++++ src/edit/dir.go | 42 ++++++++++++++++++++++++++++-------------- 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 @@

{{.path | html}}

Content-Type: {{.ctype | html}}

+{{if not .exists}} + +{{end}} {{if istext .ctype}}
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 -- cgit v1.2.3-54-g00ecf