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 .}}
|
{{if .}}
|
||||||
<ul>
|
<ul>
|
||||||
{{range .}}
|
{{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}}]
|
[keywords:{{range $key := .Metadata.Keywords}} <a href="/k/{{$key}}">{{$key}}</a>{{end}}]
|
||||||
[md5: <a href="/md5/{{.Md5}}">{{.Md5 | printf "%8.8s"}}...</a>]</li>
|
[md5: <a href="/md5/{{.Md5}}">{{.Md5 | printf "%8.8s"}}...</a>]</li>
|
||||||
{{end}}
|
{{end}}
|
||||||
</ul>
|
</ul>
|
||||||
{{end}}
|
{{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 .}}
|
||||||
|
{{if .IsImage}}
|
||||||
<a href="/f/{{.Filename}}"><img src="/f/{{.Filename}}"></a>
|
<a href="/f/{{.Filename}}"><img src="/f/{{.Filename}}"></a>
|
||||||
|
{{else}}
|
||||||
|
<a href="/f/{{.Filename}}">{{.Filename}}</a>
|
||||||
|
{{end}}
|
||||||
<br/>
|
<br/>
|
||||||
[keywords:{{range $key := .Metadata.Keywords}} <a href="/k/{{$key}}">{{$key}}</a>{{end}}]
|
[keywords:{{range $key := .Metadata.Keywords}} <a href="/k/{{$key}}">{{$key}}</a>{{end}}]
|
||||||
<br/>
|
<br/>
|
||||||
|
@ -204,7 +210,7 @@ func ImageViewPage(w io.Writer, file File) (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = imageViewTemplate.Execute(w, file)
|
err = fileViewTemplate.Execute(w, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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")
|
w.Header().Set("Content-Type", "text/html")
|
||||||
if len(uriChunks) == 2 && len(uriChunks[1]) > 0 {
|
if len(uriChunks) == 2 && len(uriChunks[1]) > 0 {
|
||||||
var file File
|
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 {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ImageViewPage(w, file)
|
file.SetIsImage()
|
||||||
|
err = ImageViewPage(w, file)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// no filename given, show them the full listing
|
// no filename given, show them the full listing
|
||||||
http.Redirect(w, r, "/all", 302)
|
http.Redirect(w, r, "/all", 302)
|
||||||
|
@ -383,7 +388,10 @@ func routeRoot(w http.ResponseWriter, r *http.Request) {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ListFilesPage(w, files)
|
err = ListFilesPage(w, files)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error: %s", err)
|
||||||
|
}
|
||||||
LogRequest(r, 200)
|
LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -403,7 +411,10 @@ func routeAll(w http.ResponseWriter, r *http.Request) {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ListFilesPage(w, files)
|
err = ListFilesPage(w, files)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error: %s", err)
|
||||||
|
}
|
||||||
LogRequest(r, 200)
|
LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -449,7 +460,10 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println(len(files))
|
log.Println(len(files))
|
||||||
ListFilesPage(w, files)
|
err = ListFilesPage(w, files)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
LogRequest(r, 200)
|
LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
@ -472,7 +486,10 @@ func routeMD5s(w http.ResponseWriter, r *http.Request) {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ListFilesPage(w, files)
|
err = ListFilesPage(w, files)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
LogRequest(r, 200)
|
LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
@ -538,7 +555,11 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exists, err := HasFileByFilename(local_filename)
|
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))
|
file, err := gfs.Create(filepath.Base(local_filename))
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -569,7 +590,10 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else if r.Method == "GET" {
|
} else if r.Method == "GET" {
|
||||||
UrliePage(w)
|
err := UrliePage(w)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error: %s", err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
LogRequest(r, 404)
|
LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
|
@ -583,6 +607,16 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
||||||
POST /upload
|
POST /upload
|
||||||
*/
|
*/
|
||||||
func routeUpload(w http.ResponseWriter, r *http.Request) {
|
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" {
|
if r.Method == "POST" {
|
||||||
info := Info{
|
info := Info{
|
||||||
Ip: r.RemoteAddr,
|
Ip: r.RemoteAddr,
|
||||||
|
@ -596,56 +630,57 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
useRandName := false
|
||||||
log.Printf("%q", r.MultipartForm.Value)
|
log.Printf("%q", r.MultipartForm.Value)
|
||||||
for k, v := range r.MultipartForm.Value {
|
for k, v := range r.MultipartForm.Value {
|
||||||
if k == "keywords" {
|
if k == "keywords" {
|
||||||
info.Keywords = append(info.Keywords, strings.Split(v[0], ",")...)
|
info.Keywords = append(info.Keywords, strings.Split(v[0], ",")...)
|
||||||
|
} else if k == "rand" {
|
||||||
|
useRandName = true
|
||||||
} else {
|
} else {
|
||||||
log.Printf("WARN: not sure what to do with param [%s = %s]", k, v)
|
log.Printf("WARN: not sure what to do with param [%s = %s]", k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
filehdr := r.MultipartForm.File["filename"][0]
|
filehdr := r.MultipartForm.File["filename"][0]
|
||||||
exists, err := HasFileByFilename(filehdr.Filename)
|
filename := filehdr.Filename
|
||||||
|
exists, err := HasFileByFilename(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
} else if err == nil && !exists {
|
}
|
||||||
file, err := gfs.Create(filehdr.Filename)
|
if exists || useRandName {
|
||||||
defer file.Close()
|
ext := filepath.Ext(filename)
|
||||||
if err != nil {
|
str := GetSmallHash()
|
||||||
serverErr(w, r, err)
|
filename = fmt.Sprintf("%s%s", str, ext)
|
||||||
return
|
}
|
||||||
}
|
|
||||||
file.SetMeta(&info)
|
|
||||||
|
|
||||||
multiFile, err := filehdr.Open()
|
file, err := gfs.Create(filename)
|
||||||
if err != nil {
|
defer file.Close()
|
||||||
log.Println(err)
|
if err != nil {
|
||||||
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 {
|
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else if r.Method == "GET" {
|
file.SetMeta(&info)
|
||||||
// Show the upload form
|
|
||||||
UploadPage(w)
|
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 {
|
} else {
|
||||||
LogRequest(r, 404)
|
LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
|
|
15
types.go
15
types.go
|
@ -1,6 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"mime"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
type Info struct {
|
type Info struct {
|
||||||
Keywords []string // tags
|
Keywords []string // tags
|
||||||
|
@ -16,5 +22,12 @@ type File struct {
|
||||||
UploadDate time.Time
|
UploadDate time.Time
|
||||||
Length int64
|
Length int64
|
||||||
Filename string ",omitempty"
|
Filename string ",omitempty"
|
||||||
|
IsImage bool
|
||||||
ContentType string "contentType,omitempty"
|
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