mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-27 18:45:40 +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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
|
||||||
"labix.org/v2/mgo/bson"
|
"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) {
|
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)
|
err = gfs.Find(bson.M{"random": bson.M{"$gt" : r } }).One(&this_file)
|
||||||
if (err != nil) {
|
if (err != nil) {
|
||||||
return this_file, err
|
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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math/rand"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/adler32"
|
"hash/adler32"
|
||||||
|
@ -8,6 +9,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Rand64() int64 {
|
||||||
|
return rand.Int63()
|
||||||
|
}
|
||||||
|
|
||||||
/* Convinience method for getting md5 sum of a string */
|
/* Convinience method for getting md5 sum of a string */
|
||||||
func GetMd5FromString(blob string) (sum []byte) {
|
func GetMd5FromString(blob string) (sum []byte) {
|
||||||
h := md5.New()
|
h := md5.New()
|
||||||
|
|
17
imgsrv.go
17
imgsrv.go
|
@ -15,7 +15,6 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -48,22 +47,6 @@ var (
|
||||||
FileKeywords = ""
|
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() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
for _, arg := range flag.Args() {
|
for _, arg := range flag.Args() {
|
||||||
|
|
65
layouts.go
65
layouts.go
|
@ -5,21 +5,76 @@ import (
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
var index = `
|
var emptyInterface interface{}
|
||||||
|
|
||||||
|
var headTemplate = template.Must(template.New("head").Parse(headTemplateHTML))
|
||||||
|
var headTemplateHTML = `
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
<title>{{.title}}</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
`
|
||||||
|
|
||||||
|
var tailTemplate = template.Must(template.New("tail").Parse(tailTemplateHTML))
|
||||||
|
var tailTemplateHTML = `
|
||||||
|
{{if .}}
|
||||||
|
<footer>{{.footer}}</footer>
|
||||||
|
{{end}}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</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) {
|
if (err != nil) {
|
||||||
return err
|
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"
|
||||||
"labix.org/v2/mgo/bson"
|
"labix.org/v2/mgo/bson"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -153,29 +152,35 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
||||||
var filename string
|
var filename string
|
||||||
info := Info{
|
info := Info{
|
||||||
Ip: r.RemoteAddr,
|
Ip: r.RemoteAddr,
|
||||||
Random: rand.Int63(),
|
Random: Rand64(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len(uriChunks) == 2 && len(uriChunks[1]) != 0) {
|
if (len(uriChunks) == 2 && len(uriChunks[1]) != 0) {
|
||||||
filename = uriChunks[1]
|
filename = uriChunks[1]
|
||||||
}
|
}
|
||||||
params := parseRawQuery(r.URL.RawQuery)
|
if (len(filename) == 0) {
|
||||||
var p_ext string
|
filename = r.FormValue("filename")
|
||||||
for k,v := range params {
|
log.Printf("%s", filename)
|
||||||
switch {
|
}
|
||||||
case (k == "filename"):
|
|
||||||
filename = v
|
p_ext := r.FormValue("ext")
|
||||||
case (k == "ext"):
|
log.Printf("%t", p_ext)
|
||||||
if (v[0] != '.') {
|
if (len(filename) > 0 && len(p_ext) == 0) {
|
||||||
p_ext = fmt.Sprintf(".%s", v)
|
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 {
|
} else {
|
||||||
p_ext = v
|
info.Keywords = append(info.Keywords, v)
|
||||||
}
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -192,24 +197,44 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
||||||
exists, err := HasFileByFilename(filename)
|
exists, err := HasFileByFilename(filename)
|
||||||
if (err == nil && !exists) {
|
if (err == nil && !exists) {
|
||||||
file, err := gfs.Create(filename)
|
file, err := gfs.Create(filename)
|
||||||
|
defer file.Close()
|
||||||
if (err != nil) {
|
if (err != nil) {
|
||||||
serverErr(w,r,err)
|
serverErr(w,r,err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file.SetMeta(&info)
|
||||||
|
|
||||||
|
// copy the request body into the gfs file
|
||||||
n, err := io.Copy(file, r.Body)
|
n, err := io.Copy(file, r.Body)
|
||||||
if (err != nil) {
|
if (err != nil) {
|
||||||
serverErr(w,r,err)
|
serverErr(w,r,err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (n != r.ContentLength) {
|
if (n != r.ContentLength) {
|
||||||
log.Printf("WARNING: [%s] content-length (%d), content written (%d)",
|
log.Printf("WARNING: [%s] content-length (%d), content written (%d)",
|
||||||
filename,
|
filename,
|
||||||
r.ContentLength,
|
r.ContentLength,
|
||||||
n)
|
n)
|
||||||
}
|
}
|
||||||
file.Close()
|
|
||||||
} else if (exists) {
|
} else if (exists) {
|
||||||
log.Printf("[%s] already exists", filename)
|
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 {
|
} else {
|
||||||
serverErr(w,r,err)
|
serverErr(w,r,err)
|
||||||
return
|
return
|
||||||
|
@ -282,8 +307,13 @@ func routeRoot(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/html")
|
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(bson.M{"uploadDate": bson.M{"$gt": time.Now().Add(-time.Hour)}}).Limit(10).Iter()
|
||||||
iter := gfs.Find(nil).Sort("-uploadDate").Limit(10).Iter()
|
var files []File
|
||||||
writeList(w, iter)
|
err := gfs.Find(nil).Sort("-uploadDate").Limit(10).All(&files)
|
||||||
|
if (err != nil) {
|
||||||
|
serverErr(w,r,err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ListFilesPage(w,files)
|
||||||
LogRequest(r,200)
|
LogRequest(r,200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -297,8 +327,13 @@ func routeAll(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "text/html")
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
|
||||||
// Show a page of all the images
|
// Show a page of all the images
|
||||||
iter := gfs.Find(nil).Iter()
|
var files []File
|
||||||
writeList(w, iter)
|
err := gfs.Find(nil).All(&files)
|
||||||
|
if (err != nil) {
|
||||||
|
serverErr(w,r,err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ListFilesPage(w,files)
|
||||||
LogRequest(r,200)
|
LogRequest(r,200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -338,7 +373,13 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
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)
|
LogRequest(r,200)
|
||||||
}
|
}
|
||||||
|
@ -377,6 +418,24 @@ func routeIPs(w http.ResponseWriter, r *http.Request) {
|
||||||
LogRequest(r,200)
|
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() {
|
func initMongo() {
|
||||||
mongo_session, err := mgo.Dial(MongoHost)
|
mongo_session, err := mgo.Dial(MongoHost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -400,6 +459,7 @@ func runServer(ip, port string) {
|
||||||
defer mongo_session.Close()
|
defer mongo_session.Close()
|
||||||
|
|
||||||
http.HandleFunc("/", routeRoot)
|
http.HandleFunc("/", routeRoot)
|
||||||
|
http.HandleFunc("/upload", routeUpload)
|
||||||
http.HandleFunc("/all", routeAll)
|
http.HandleFunc("/all", routeAll)
|
||||||
http.HandleFunc("/f/", routeFiles)
|
http.HandleFunc("/f/", routeFiles)
|
||||||
http.HandleFunc("/k/", routeKeywords)
|
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