mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-24 00:55:41 +00:00
getting the embedded image view ironed out a bit more
Also, getting the randomize hash name worked out, to avoid file name collisions.
This commit is contained in:
parent
4fbbcd622d
commit
75ee3c520c
3 changed files with 100 additions and 46 deletions
14
layouts.go
14
layouts.go
|
@ -121,17 +121,23 @@ var listTemplateHTML = `
|
|||
{{if .}}
|
||||
<ul>
|
||||
{{range .}}
|
||||
<li><a href="/f/{{.Filename}}">{{.Filename}}</a>
|
||||
<li>
|
||||
<a href="/v/{{.Filename}}">{{.Filename}}</a>
|
||||
[keywords:{{range $key := .Metadata.Keywords}} <a href="/k/{{$key}}">{{$key}}</a>{{end}}]
|
||||
[md5: <a href="/md5/{{.Md5}}">{{.Md5 | printf "%8.8s"}}...</a>]</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
{{end}}
|
||||
`
|
||||
var imageViewTemplate = template.Must(template.New("image").Parse(imageViewTemplateHTML))
|
||||
var imageViewTemplateHTML = `
|
||||
|
||||
var fileViewTemplate = template.Must(template.New("file").Parse(fileViewTemplateHTML))
|
||||
var fileViewTemplateHTML = `
|
||||
{{if .}}
|
||||
{{if .IsImage}}
|
||||
<a href="/f/{{.Filename}}"><img src="/f/{{.Filename}}"></a>
|
||||
{{else}}
|
||||
<a href="/f/{{.Filename}}">{{.Filename}}</a>
|
||||
{{end}}
|
||||
<br/>
|
||||
[keywords:{{range $key := .Metadata.Keywords}} <a href="/k/{{$key}}">{{$key}}</a>{{end}}]
|
||||
<br/>
|
||||
|
@ -204,7 +210,7 @@ func ImageViewPage(w io.Writer, file File) (err error) {
|
|||
return err
|
||||
}
|
||||
|
||||
err = imageViewTemplate.Execute(w, file)
|
||||
err = fileViewTemplate.Execute(w, file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
77
server.go
77
server.go
|
@ -128,7 +128,12 @@ func routeViewsGET(w http.ResponseWriter, r *http.Request) {
|
|||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
ImageViewPage(w, file)
|
||||
file.SetIsImage()
|
||||
err = ImageViewPage(w, file)
|
||||
if err != nil {
|
||||
log.Printf("error: %s", err)
|
||||
}
|
||||
|
||||
} else {
|
||||
// no filename given, show them the full listing
|
||||
http.Redirect(w, r, "/all", 302)
|
||||
|
@ -383,7 +388,10 @@ func routeRoot(w http.ResponseWriter, r *http.Request) {
|
|||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
ListFilesPage(w, files)
|
||||
err = ListFilesPage(w, files)
|
||||
if err != nil {
|
||||
log.Printf("error: %s", err)
|
||||
}
|
||||
LogRequest(r, 200)
|
||||
}
|
||||
|
||||
|
@ -403,7 +411,10 @@ func routeAll(w http.ResponseWriter, r *http.Request) {
|
|||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
ListFilesPage(w, files)
|
||||
err = ListFilesPage(w, files)
|
||||
if err != nil {
|
||||
log.Printf("error: %s", err)
|
||||
}
|
||||
LogRequest(r, 200)
|
||||
}
|
||||
|
||||
|
@ -449,7 +460,10 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
log.Println(len(files))
|
||||
ListFilesPage(w, files)
|
||||
err = ListFilesPage(w, files)
|
||||
if err != nil {
|
||||
log.Printf("error: %s", err)
|
||||
}
|
||||
|
||||
LogRequest(r, 200)
|
||||
}
|
||||
|
@ -472,7 +486,10 @@ func routeMD5s(w http.ResponseWriter, r *http.Request) {
|
|||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
ListFilesPage(w, files)
|
||||
err = ListFilesPage(w, files)
|
||||
if err != nil {
|
||||
log.Printf("error: %s", err)
|
||||
}
|
||||
|
||||
LogRequest(r, 200)
|
||||
}
|
||||
|
@ -538,7 +555,11 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
exists, err := HasFileByFilename(local_filename)
|
||||
if err == nil && !exists {
|
||||
if err != nil {
|
||||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
file, err := gfs.Create(filepath.Base(local_filename))
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
|
@ -569,7 +590,10 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
} else if r.Method == "GET" {
|
||||
UrliePage(w)
|
||||
err := UrliePage(w)
|
||||
if err != nil {
|
||||
log.Printf("error: %s", err)
|
||||
}
|
||||
} else {
|
||||
LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
|
@ -583,6 +607,16 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
|||
POST /upload
|
||||
*/
|
||||
func routeUpload(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
// Show the upload form
|
||||
LogRequest(r, 200) // if we make it this far, then log success
|
||||
err := UploadPage(w)
|
||||
if err != nil {
|
||||
log.Printf("error: %s", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == "POST" {
|
||||
info := Info{
|
||||
Ip: r.RemoteAddr,
|
||||
|
@ -596,22 +630,32 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
|
|||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
useRandName := false
|
||||
log.Printf("%q", r.MultipartForm.Value)
|
||||
for k, v := range r.MultipartForm.Value {
|
||||
if k == "keywords" {
|
||||
info.Keywords = append(info.Keywords, strings.Split(v[0], ",")...)
|
||||
} else if k == "rand" {
|
||||
useRandName = true
|
||||
} else {
|
||||
log.Printf("WARN: not sure what to do with param [%s = %s]", k, v)
|
||||
}
|
||||
}
|
||||
|
||||
filehdr := r.MultipartForm.File["filename"][0]
|
||||
exists, err := HasFileByFilename(filehdr.Filename)
|
||||
filename := filehdr.Filename
|
||||
exists, err := HasFileByFilename(filename)
|
||||
if err != nil {
|
||||
serverErr(w, r, err)
|
||||
return
|
||||
} else if err == nil && !exists {
|
||||
file, err := gfs.Create(filehdr.Filename)
|
||||
}
|
||||
if exists || useRandName {
|
||||
ext := filepath.Ext(filename)
|
||||
str := GetSmallHash()
|
||||
filename = fmt.Sprintf("%s%s", str, ext)
|
||||
}
|
||||
|
||||
file, err := gfs.Create(filename)
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
serverErr(w, r, err)
|
||||
|
@ -631,21 +675,12 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
if n != r.ContentLength {
|
||||
log.Printf("WARNING: [%s] content-length (%d), content written (%d)",
|
||||
filehdr.Filename,
|
||||
filename,
|
||||
r.ContentLength,
|
||||
n)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, fmt.Sprintf("/v/%s", filehdr.Filename), 302)
|
||||
} else if exists {
|
||||
// print some message about the file already existing
|
||||
} else {
|
||||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
} else if r.Method == "GET" {
|
||||
// Show the upload form
|
||||
UploadPage(w)
|
||||
http.Redirect(w, r, fmt.Sprintf("/v/%s", filename), 302)
|
||||
} else {
|
||||
LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
|
|
15
types.go
15
types.go
|
@ -1,6 +1,12 @@
|
|||
package main
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"mime"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Info struct {
|
||||
Keywords []string // tags
|
||||
|
@ -16,5 +22,12 @@ type File struct {
|
|||
UploadDate time.Time
|
||||
Length int64
|
||||
Filename string ",omitempty"
|
||||
IsImage bool
|
||||
ContentType string "contentType,omitempty"
|
||||
}
|
||||
|
||||
func (f *File) SetIsImage() {
|
||||
m_type := mime.TypeByExtension(filepath.Ext(f.Filename))
|
||||
f.IsImage = strings.Contains(m_type, "image")
|
||||
fmt.Println(f.Filename,f.IsImage)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue