mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-12-25 23:36:30 +00:00
* 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
This commit is contained in:
parent
9a5ded48a6
commit
4fbbcd622d
2 changed files with 88 additions and 5 deletions
50
layouts.go
50
layouts.go
|
@ -36,6 +36,7 @@ var navbarTemplateHTML = `
|
|||
<li><a href="/">Home</a></li>
|
||||
<li><a href="/upload">Upload</a></li>
|
||||
<li><a href="/urlie">URLie</a></li>
|
||||
<li><a href="/all">All</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -73,7 +74,8 @@ var formGetUrlTemplateHTML = `
|
|||
<tr>
|
||||
<td>
|
||||
<input type="text" name="url" placeholder="file URL"><br/>
|
||||
<input type="text" name="keywords" placeholder="keywords"><i>(comma seperatated, no spaces)</i>
|
||||
<input type="text" name="keywords" placeholder="keywords"><i>(comma seperatated, no spaces)</i><br/>
|
||||
<input type="checkbox" name="rand" value="true">Randomize filename
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
@ -98,7 +100,8 @@ var formFileUploadTemplateHTML = `
|
|||
<tr>
|
||||
<td>
|
||||
<input type="file" name="filename" placeholder="filename"><br/>
|
||||
<input type="text" name="keywords" placeholder="keywords"><i>(comma seperatated, no spaces)</i>
|
||||
<input type="text" name="keywords" placeholder="keywords"><i>(comma seperatated, no spaces)</i><br/>
|
||||
<input type="checkbox" name="rand" value="true">Randomize filename
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
@ -125,6 +128,16 @@ var listTemplateHTML = `
|
|||
</ul>
|
||||
{{end}}
|
||||
`
|
||||
var imageViewTemplate = template.Must(template.New("image").Parse(imageViewTemplateHTML))
|
||||
var imageViewTemplateHTML = `
|
||||
{{if .}}
|
||||
<a href="/f/{{.Filename}}"><img src="/f/{{.Filename}}"></a>
|
||||
<br/>
|
||||
[keywords:{{range $key := .Metadata.Keywords}} <a href="/k/{{$key}}">{{$key}}</a>{{end}}]
|
||||
<br/>
|
||||
[md5: <a href="/md5/{{.Md5}}">{{.Md5 | printf "%8.8s"}}...</a>]</li>
|
||||
{{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
|
||||
|
|
43
server.go
43
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 {
|
||||
|
|
Loading…
Reference in a new issue