mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-23 16:45:39 +00:00
many updates, and cleanup, but the /upload form is not quite working yet
This commit is contained in:
parent
3a899e1daa
commit
683b4465e1
7 changed files with 169 additions and 55 deletions
|
@ -2,7 +2,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"labix.org/v2/mgo/bson"
|
||||
)
|
||||
|
||||
|
@ -17,7 +16,7 @@ func GetFileByFilename(filename string) (this_file File, err error) {
|
|||
}
|
||||
|
||||
func GetFileRandom() (this_file File, err error) {
|
||||
r := rand.Int63()
|
||||
r := Rand64()
|
||||
err = gfs.Find(bson.M{"random": bson.M{"$gt" : r } }).One(&this_file)
|
||||
if (err != nil) {
|
||||
return this_file, err
|
7
get.go
7
get.go
|
@ -1,7 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"labix.org/v2/mgo/bson"
|
||||
)
|
||||
|
5
hash.go
5
hash.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"hash/adler32"
|
||||
|
@ -8,6 +9,10 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func Rand64() int64 {
|
||||
return rand.Int63()
|
||||
}
|
||||
|
||||
/* Convinience method for getting md5 sum of a string */
|
||||
func GetMd5FromString(blob string) (sum []byte) {
|
||||
h := md5.New()
|
||||
|
|
17
imgsrv.go
17
imgsrv.go
|
@ -15,7 +15,6 @@ import (
|
|||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -48,22 +47,6 @@ var (
|
|||
FileKeywords = ""
|
||||
)
|
||||
|
||||
type Info struct {
|
||||
Keywords []string // tags
|
||||
Ip string // who uploaded it
|
||||
Random int64
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Metadata Info ",omitempty"
|
||||
Md5 string
|
||||
ChunkSize int
|
||||
UploadDate time.Time
|
||||
Length int64
|
||||
Filename string ",omitempty"
|
||||
ContentType string "contentType,omitempty"
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
for _, arg := range flag.Args() {
|
||||
|
|
65
layouts.go
65
layouts.go
|
@ -5,21 +5,76 @@ import (
|
|||
"io"
|
||||
)
|
||||
|
||||
var index = `
|
||||
var emptyInterface interface{}
|
||||
|
||||
var headTemplate = template.Must(template.New("head").Parse(headTemplateHTML))
|
||||
var headTemplateHTML = `
|
||||
<html>
|
||||
<head>
|
||||
<title>{{.title}}</title>
|
||||
</head>
|
||||
<body>
|
||||
`
|
||||
|
||||
|
||||
var tailTemplate = template.Must(template.New("tail").Parse(tailTemplateHTML))
|
||||
var tailTemplateHTML = `
|
||||
{{if .}}
|
||||
<footer>{{.footer}}</footer>
|
||||
{{end}}
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
func IndexPage(w io.Writer) (err error) {
|
||||
t, err := template.New("index").Parse(index)
|
||||
|
||||
var formFileUploadTemplate = template.Must(template.New("formFileUpload").Parse(formFileUploadTemplateHTML))
|
||||
var formFileUploadTemplateHTML = `
|
||||
<form enctype="multipart/form-data" action="" method="POST">
|
||||
<input type="text" name="keywords" placeholder="keywords"><br/>
|
||||
<input type="file" name="filename" placeholder="filename"><br/>
|
||||
<input type="submit" value="Upload File"><br/>
|
||||
</form>
|
||||
`
|
||||
|
||||
var listTemplate = template.Must(template.New("list").Parse(listTemplateHTML))
|
||||
var listTemplateHTML = `
|
||||
{{if .}}
|
||||
<ul>
|
||||
{{range .}}
|
||||
<li><a href="/f/{{.Filename}}">{{.Filename}}</a> - {{.Md5}}</li>
|
||||
{{end}}
|
||||
{{end}}
|
||||
</ul>
|
||||
`
|
||||
|
||||
func UploadPage(w io.Writer) (err error) {
|
||||
err = headTemplate.Execute(w, map[string]string{"title" : "FileSrv :: Upload"})
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
return t.Execute(w, index)
|
||||
err = formFileUploadTemplate.Execute(w, &emptyInterface)
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
err = tailTemplate.Execute(w, map[string]string{})
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ListFilesPage(w io.Writer, files []File) (err error) {
|
||||
err = headTemplate.Execute(w, map[string]string{"title" : "FileSrv"})
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
err = listTemplate.Execute(w, files)
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
//err = tailTemplate.Execute(w, map[string]string{"footer" : "herp til you derp 2013"})
|
||||
err = tailTemplate.Execute(w, &emptyInterface)
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
108
server.go
108
server.go
|
@ -6,7 +6,6 @@ import (
|
|||
"labix.org/v2/mgo"
|
||||
"labix.org/v2/mgo/bson"
|
||||
"log"
|
||||
"math/rand"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
@ -153,29 +152,35 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
|||
var filename string
|
||||
info := Info{
|
||||
Ip: r.RemoteAddr,
|
||||
Random: rand.Int63(),
|
||||
Random: Rand64(),
|
||||
}
|
||||
|
||||
if (len(uriChunks) == 2 && len(uriChunks[1]) != 0) {
|
||||
filename = uriChunks[1]
|
||||
}
|
||||
params := parseRawQuery(r.URL.RawQuery)
|
||||
var p_ext string
|
||||
for k,v := range params {
|
||||
switch {
|
||||
case (k == "filename"):
|
||||
filename = v
|
||||
case (k == "ext"):
|
||||
if (v[0] != '.') {
|
||||
p_ext = fmt.Sprintf(".%s", v)
|
||||
} else {
|
||||
p_ext = v
|
||||
if (len(filename) == 0) {
|
||||
filename = r.FormValue("filename")
|
||||
log.Printf("%s", filename)
|
||||
}
|
||||
case (k == "k" || k == "key" || k == "keyword"):
|
||||
info.Keywords = append(info.Keywords[:], v)
|
||||
case (k == "keys" || k == "keywords"):
|
||||
for _, key := range strings.Split(v, ",") {
|
||||
info.Keywords = append(info.Keywords[:], key)
|
||||
|
||||
p_ext := r.FormValue("ext")
|
||||
log.Printf("%t", p_ext)
|
||||
if (len(filename) > 0 && len(p_ext) == 0) {
|
||||
p_ext = filepath.Ext(filename)
|
||||
}// else if (len(p_ext) > 0 && p_ext[0] != ".") {
|
||||
//p_ext = fmt.Sprintf(".%s", p_ext)
|
||||
//}
|
||||
|
||||
for _, word := range []string{
|
||||
"k", "key", "keyword",
|
||||
"keys", "keywords",
|
||||
} {
|
||||
v := r.FormValue(word)
|
||||
if (len(v) > 0) {
|
||||
if (strings.Contains(v, ",")) {
|
||||
info.Keywords = append(info.Keywords, strings.Split(v,",")...)
|
||||
} else {
|
||||
info.Keywords = append(info.Keywords, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -192,24 +197,44 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
|||
exists, err := HasFileByFilename(filename)
|
||||
if (err == nil && !exists) {
|
||||
file, err := gfs.Create(filename)
|
||||
defer file.Close()
|
||||
if (err != nil) {
|
||||
serverErr(w,r,err)
|
||||
return
|
||||
}
|
||||
|
||||
file.SetMeta(&info)
|
||||
|
||||
// copy the request body into the gfs file
|
||||
n, err := io.Copy(file, r.Body)
|
||||
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)
|
||||
}
|
||||
file.Close()
|
||||
} else if (exists) {
|
||||
log.Printf("[%s] already exists", filename)
|
||||
file, err := gfs.Open(filename)
|
||||
defer file.Close()
|
||||
if (err != nil) {
|
||||
serverErr(w,r,err)
|
||||
return
|
||||
}
|
||||
|
||||
var mInfo Info
|
||||
err = file.GetMeta(&mInfo)
|
||||
if (err != nil) {
|
||||
log.Printf("ERROR: failed to get metadata for %s. %s\n", filename, err)
|
||||
}
|
||||
mInfo.Keywords = append(mInfo.Keywords, info.Keywords...)
|
||||
file.SetMeta(&mInfo)
|
||||
|
||||
} else {
|
||||
serverErr(w,r,err)
|
||||
return
|
||||
|
@ -282,8 +307,13 @@ func routeRoot(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
//iter := gfs.Find(bson.M{"uploadDate": bson.M{"$gt": time.Now().Add(-time.Hour)}}).Limit(10).Iter()
|
||||
iter := gfs.Find(nil).Sort("-uploadDate").Limit(10).Iter()
|
||||
writeList(w, iter)
|
||||
var files []File
|
||||
err := gfs.Find(nil).Sort("-uploadDate").Limit(10).All(&files)
|
||||
if (err != nil) {
|
||||
serverErr(w,r,err)
|
||||
return
|
||||
}
|
||||
ListFilesPage(w,files)
|
||||
LogRequest(r,200)
|
||||
}
|
||||
|
||||
|
@ -297,8 +327,13 @@ func routeAll(w http.ResponseWriter, r *http.Request) {
|
|||
w.Header().Set("Content-Type", "text/html")
|
||||
|
||||
// Show a page of all the images
|
||||
iter := gfs.Find(nil).Iter()
|
||||
writeList(w, iter)
|
||||
var files []File
|
||||
err := gfs.Find(nil).All(&files)
|
||||
if (err != nil) {
|
||||
serverErr(w,r,err)
|
||||
return
|
||||
}
|
||||
ListFilesPage(w,files)
|
||||
LogRequest(r,200)
|
||||
}
|
||||
|
||||
|
@ -338,7 +373,13 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
writeList(w, iter)
|
||||
var files []File
|
||||
err := iter.All(&files)
|
||||
if (err != nil) {
|
||||
serverErr(w,r,err)
|
||||
return
|
||||
}
|
||||
ListFilesPage(w, files)
|
||||
|
||||
LogRequest(r,200)
|
||||
}
|
||||
|
@ -377,6 +418,24 @@ func routeIPs(w http.ResponseWriter, r *http.Request) {
|
|||
LogRequest(r,200)
|
||||
}
|
||||
|
||||
func routeUpload(w http.ResponseWriter, r *http.Request) {
|
||||
if (r.Method == "POST") {
|
||||
// handle the form posting to this route
|
||||
routeFilesPOST(w,r)
|
||||
return
|
||||
}
|
||||
|
||||
if (r.Method != "GET") {
|
||||
LogRequest(r,404)
|
||||
http.NotFound(w,r)
|
||||
return
|
||||
}
|
||||
|
||||
// Show the upload form
|
||||
UploadPage(w)
|
||||
LogRequest(r,200)
|
||||
}
|
||||
|
||||
func initMongo() {
|
||||
mongo_session, err := mgo.Dial(MongoHost)
|
||||
if err != nil {
|
||||
|
@ -400,6 +459,7 @@ func runServer(ip, port string) {
|
|||
defer mongo_session.Close()
|
||||
|
||||
http.HandleFunc("/", routeRoot)
|
||||
http.HandleFunc("/upload", routeUpload)
|
||||
http.HandleFunc("/all", routeAll)
|
||||
http.HandleFunc("/f/", routeFiles)
|
||||
http.HandleFunc("/k/", routeKeywords)
|
||||
|
|
19
types.go
Normal file
19
types.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import "time"
|
||||
|
||||
type Info struct {
|
||||
Keywords []string // tags
|
||||
Ip string // who uploaded it
|
||||
Random int64
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Metadata Info ",omitempty"
|
||||
Md5 string
|
||||
ChunkSize int
|
||||
UploadDate time.Time
|
||||
Length int64
|
||||
Filename string ",omitempty"
|
||||
ContentType string "contentType,omitempty"
|
||||
}
|
Loading…
Reference in a new issue