From 4fbbcd622d61282414846b417fa278603c566b82 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Wed, 8 May 2013 14:28:49 -0400 Subject: [PATCH] * including a navbar link for '/all' * building out a framed view page for images, to embed the image in * now having the upload and urlie, do a redirect to a /v/... page --- layouts.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- server.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 88 insertions(+), 5 deletions(-) diff --git a/layouts.go b/layouts.go index b2e7a03..203602d 100644 --- a/layouts.go +++ b/layouts.go @@ -36,6 +36,7 @@ var navbarTemplateHTML = `
  • Home
  • Upload
  • URLie
  • +
  • All
  • @@ -73,7 +74,8 @@ var formGetUrlTemplateHTML = `
    - (comma seperatated, no spaces) + (comma seperatated, no spaces)
    + Randomize filename @@ -98,7 +100,8 @@ var formFileUploadTemplateHTML = `
    - (comma seperatated, no spaces) + (comma seperatated, no spaces)
    + Randomize filename @@ -125,6 +128,16 @@ var listTemplateHTML = ` {{end}} ` +var imageViewTemplate = template.Must(template.New("image").Parse(imageViewTemplateHTML)) +var imageViewTemplateHTML = ` +{{if .}} + +
    +[keywords:{{range $key := .Metadata.Keywords}} {{$key}}{{end}}] +
    +[md5: {{.Md5 | printf "%8.8s"}}...] +{{end}} +` func UrliePage(w io.Writer) (err error) { err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: URLie"}) @@ -149,6 +162,7 @@ func UrliePage(w io.Writer) (err error) { } return } + func UploadPage(w io.Writer) (err error) { err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: Upload"}) if err != nil { @@ -162,10 +176,39 @@ func UploadPage(w io.Writer) (err error) { if err != nil { return err } + + // main context of this page err = formFileUploadTemplate.Execute(w, &emptyInterface) if err != nil { return err } + + err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)}) + if err != nil { + return err + } + return +} + +func ImageViewPage(w io.Writer, file File) (err error) { + err = headTemplate.Execute(w, map[string]string{"title": "FileSrv"}) + if err != nil { + return err + } + err = navbarTemplate.Execute(w, nil) + if err != nil { + return err + } + err = containerBeginTemplate.Execute(w, nil) + if err != nil { + return err + } + + err = imageViewTemplate.Execute(w, file) + if err != nil { + return err + } + err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)}) if err != nil { return err @@ -186,10 +229,13 @@ func ListFilesPage(w io.Writer, files []File) (err error) { if err != nil { return err } + + // main context of this page err = listTemplate.Execute(w, files) if err != nil { return err } + err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)}) if err != nil { return err diff --git a/server.go b/server.go index 6707b50..3fbb7af 100644 --- a/server.go +++ b/server.go @@ -29,6 +29,7 @@ func runServer(ip, port string) { http.HandleFunc("/urlie", routeGetFromUrl) http.HandleFunc("/all", routeAll) http.HandleFunc("/f/", routeFiles) + http.HandleFunc("/v/", routeViews) http.HandleFunc("/k/", routeKeywords) http.HandleFunc("/ip/", routeIPs) http.HandleFunc("/ext/", routeExt) @@ -111,6 +112,30 @@ func LogRequest(r *http.Request, statusCode int) { r.ContentLength) } +func routeViewsGET(w http.ResponseWriter, r *http.Request) { + uriChunks := chunkURI(r.URL.Path) + if len(uriChunks) > 2 { + LogRequest(r, 404) + http.NotFound(w, r) + return + } + + w.Header().Set("Content-Type", "text/html") + if len(uriChunks) == 2 && len(uriChunks[1]) > 0 { + var file File + err := gfs.Find(bson.M{"filename": uriChunks[1]}).One(&file) + if err != nil { + serverErr(w, r, err) + return + } + ImageViewPage(w, file) + } else { + // no filename given, show them the full listing + http.Redirect(w, r, "/all", 302) + } + LogRequest(r, 200) +} + /* GET /f/ GET /f/:name @@ -155,7 +180,8 @@ func routeFilesGET(w http.ResponseWriter, r *http.Request) { io.Copy(w, file) // send the contents of the file in the body } else { - // TODO show a list of recent uploads? ... + // no filename given, show them the full listing + http.Redirect(w, r, "/all", 302) } LogRequest(r, 200) } @@ -313,6 +339,17 @@ func routeFilesDELETE(w http.ResponseWriter, r *http.Request) { // delete the name in the path and/or parameter? } +func routeViews(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == "GET": + routeViewsGET(w, r) + default: + LogRequest(r, 404) + http.NotFound(w, r) + return + } +} + func routeFiles(w http.ResponseWriter, r *http.Request) { switch { case r.Method == "GET": @@ -526,7 +563,7 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) { } log.Printf("Wrote [%d] bytes from %s", n, local_filename) - http.Redirect(w, r, fmt.Sprintf("/f/%s", filepath.Base(local_filename)), 302) + http.Redirect(w, r, fmt.Sprintf("/v/%s", filepath.Base(local_filename)), 302) } else { serverErr(w, r, err) return @@ -599,7 +636,7 @@ func routeUpload(w http.ResponseWriter, r *http.Request) { n) } - http.Redirect(w, r, fmt.Sprintf("/f/%s", filehdr.Filename), 302) + http.Redirect(w, r, fmt.Sprintf("/v/%s", filehdr.Filename), 302) } else if exists { // print some message about the file already existing } else {