mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-12-25 15:26:30 +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
|
||||
}
|
||||
|
|
117
server.go
117
server.go
|
@ -123,12 +123,17 @@ func routeViewsGET(w http.ResponseWriter, r *http.Request) {
|
|||
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)
|
||||
err := gfs.Find(bson.M{"filename": uriChunks[1]}).One(&file)
|
||||
if err != nil {
|
||||
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,56 +630,57 @@ 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)
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
file.SetMeta(&info)
|
||||
}
|
||||
if exists || useRandName {
|
||||
ext := filepath.Ext(filename)
|
||||
str := GetSmallHash()
|
||||
filename = fmt.Sprintf("%s%s", str, ext)
|
||||
}
|
||||
|
||||
multiFile, err := filehdr.Open()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
n, err := io.Copy(file, multiFile)
|
||||
if err != nil {
|
||||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
if n != r.ContentLength {
|
||||
log.Printf("WARNING: [%s] content-length (%d), content written (%d)",
|
||||
filehdr.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 {
|
||||
file, err := gfs.Create(filename)
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
} else if r.Method == "GET" {
|
||||
// Show the upload form
|
||||
UploadPage(w)
|
||||
file.SetMeta(&info)
|
||||
|
||||
multiFile, err := filehdr.Open()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
n, err := io.Copy(file, multiFile)
|
||||
if err != nil {
|
||||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
if n != r.ContentLength {
|
||||
log.Printf("WARNING: [%s] content-length (%d), content written (%d)",
|
||||
filename,
|
||||
r.ContentLength,
|
||||
n)
|
||||
}
|
||||
|
||||
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