1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-11-27 18:45:40 +00:00

refactored for Mongo::GridFS. It almost works

This commit is contained in:
Vincent Batts 2013-01-14 16:45:16 -05:00
parent 9fdcddcfb6
commit a2f39ef4d1

178
imgsrv.go
View file

@ -19,7 +19,7 @@ import (
"os" "os"
"strings" "strings"
"time" "time"
"errors" //"errors"
"mime" "mime"
"crypto/md5" "crypto/md5"
@ -43,10 +43,9 @@ var (
MongoCollectionData = "data" MongoCollectionData = "data"
MongoCollectionImages = "data" MongoCollectionImages = "data"
MSession *mgo.Session mongo_session *mgo.Session
MongoImagesDb *mgo.Database images_db *mgo.Database
Mgfs *mgo.GridFS gfs *mgo.GridFS
McImages *mgo.Collection
DefaultRemoteHost = "" DefaultRemoteHost = ""
RemoteHost = DefaultRemoteHost RemoteHost = DefaultRemoteHost
@ -57,44 +56,35 @@ var (
type Info struct { type Info struct {
Keywords []string // tags Keywords []string // tags
MimeType string // maybe preserve the type provided?
Ip string // who uploaded it Ip string // who uploaded it
Date time.Time // when? }
type ImgFile struct {
Metadata Info ",omitempty"
Md5 string
ChunkSize int
UploadDate time.Time
Length int64
Filename string ",omitempty"
ContentType string "contentType,omitempty"
} }
/* Check whether this Image filename is on Mongo */ /* Check whether this Image filename is on Mongo */
func hasImageByFilename(filename string) (exists bool, err error) { func hasImageByFilename(filename string) (exists bool, err error) {
query := McImages.Find(bson.M{"filename": filename}) c, err := gfs.Find(bson.M{"filename": filename}).Count()
c, err := query.Count()
if (err != nil) { if (err != nil) {
return false, err return false, err
} }
exists = (c > 0) exists = (c > 0)
return exists, nil return exists, nil
} }
func hasImage(i Image) (exists bool, err error) {
exists, err = hasImageByFilename(i.Filename)
return
}
func saveImage(i Image) (err error) { func getImage(filename string) (img ImgFile, err error) {
exists, err := hasImage(i) err = gfs.Find(bson.M{"filename":filename}).One(&img)
if (err != nil) { if (err != nil) {
return return img, err
} }
if (exists) { return img, nil
return errors.New("Image Filename Exists")
}
err = McImages.Insert(i)
return
}
func getImage(filename string) (i Image, err error) {
err = McImages.Find(bson.M{"filename":filename}).One(&i)
if (err != nil) {
return i, err
}
return i, nil
} }
@ -184,6 +174,13 @@ func getSmallHash() (small_hash string) {
return fmt.Sprintf("%X", h.Sum(nil)) return fmt.Sprintf("%X", h.Sum(nil))
} }
func returnErr(w http.ResponseWriter, r *http.Request, e error) {
LogRequest(r,503)
fmt.Fprintf(w,"Error fetching image: %s", e)
http.Error(w, "Service Unavailable", 503)
return
}
/* /*
GET /i/ GET /i/
GET /i/:name GET /i/:name
@ -198,16 +195,17 @@ func routeImagesGET(w http.ResponseWriter, r *http.Request) {
} }
if (len(uriChunks) == 2 && len(uriChunks[1]) > 0) { if (len(uriChunks) == 2 && len(uriChunks[1]) > 0) {
query, err := Mgfs.Find(bson.M{"metadata": uriChunks[1] }) log.Printf("Searching for [%s] ...", uriChunks[1])
query := gfs.Find(bson.M{"metadata": bson.M{"filename": uriChunks[1] } })
c, err := query.Count()
// preliminary checks, if they've passed an image name // preliminary checks, if they've passed an image name
if (err != nil) { if (err != nil) {
LogRequest(r,503) returnErr(w,r,err)
fmt.Fprintf(w,"Error fetching image: %s", err)
http.Error(w, "Service Unavailable", 503)
return return
} }
if (query.Count() == 0) { log.Printf("Results for [%s] = %d", uriChunks[1], c)
if (c == 0) {
LogRequest(r,404) LogRequest(r,404)
http.NotFound(w,r) http.NotFound(w,r)
return return
@ -218,12 +216,9 @@ func routeImagesGET(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=315360000") w.Header().Set("Cache-Control", "max-age=315360000")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
file, err := Mgfs.Open(uriChunks[1]) file, err := gfs.Open(uriChunks[1])
if (err != nil) { if (err != nil) {
log.Println(err) returnErr(w,r,err)
LogRequest(r,503)
fmt.Fprintf(w,"Error fetching image: %s", uriChunks[1])
http.Error(w, "Service Unavailable", 503)
return return
} }
@ -251,20 +246,20 @@ func routeImagesPOST(w http.ResponseWriter, r *http.Request) {
return return
} }
var i Info var filename string
info := Info{
i.Date = bson.Now() Ip: r.RemoteAddr,
i.Ip = r.RemoteAddr }
if (len(uriChunks) == 2 && len(uriChunks[1]) != 0) { if (len(uriChunks) == 2 && len(uriChunks[1]) != 0) {
i.Filename = uriChunks[1] filename = uriChunks[1]
} }
params := parseRawQuery(r.URL.RawQuery) params := parseRawQuery(r.URL.RawQuery)
var p_ext string var p_ext string
for k,v := range params { for k,v := range params {
switch { switch {
case (k == "filename"): case (k == "filename"):
i.Filename = v filename = v
case (k == "ext"): case (k == "ext"):
if (v[0] != '.') { if (v[0] != '.') {
p_ext = fmt.Sprintf(".%s", v) p_ext = fmt.Sprintf(".%s", v)
@ -272,52 +267,51 @@ func routeImagesPOST(w http.ResponseWriter, r *http.Request) {
p_ext = v p_ext = v
} }
case (k == "k" || k == "key" || k == "keyword"): case (k == "k" || k == "key" || k == "keyword"):
i.Keywords = append(i.Keywords[:], v) info.Keywords = append(info.Keywords[:], v)
case (k == "keys" || k == "keywords"): case (k == "keys" || k == "keywords"):
for _, key := range strings.Split(v, ",") { for _, key := range strings.Split(v, ",") {
i.Keywords = append(i.Keywords[:], key) info.Keywords = append(info.Keywords[:], key)
} }
} }
} }
if (len(i.Filename) == 0) { if (len(filename) == 0) {
str := getSmallHash() str := getSmallHash()
if (len(p_ext) == 0) { if (len(p_ext) == 0) {
i.Filename = fmt.Sprintf("%s.jpg", str) filename = fmt.Sprintf("%s.jpg", str)
} else { } else {
i.Filename = fmt.Sprintf("%s%s", str, p_ext) filename = fmt.Sprintf("%s%s", str, p_ext)
} }
} }
err := saveImage(i) exists, err := hasImageByFilename(filename)
if (err != nil && err.Error() != "Image Filename Exists") { if (err == nil && !exists) {
file, err := Mgfs.Create(i.Filename) file, err := gfs.Create(filename)
if (err != nil) { if (err != nil) {
LogRequest(r,503) returnErr(w,r,err)
fmt.Fprintf(w,"Error fetching image: %s", err)
http.Error(w, "Service Unavailable", 503)
return return
} }
n, err := io.Copy(file, r.Body) n, err := io.Copy(file, r.Body)
if (err != nil) { if (err != nil) {
LogRequest(r,503) returnErr(w,r,err)
fmt.Fprintf(w,"Error fetching image: %s", err)
http.Error(w, "Service Unavailable", 503)
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)",
i.Filename, filename,
r.ContentLength, r.ContentLength,
n) n)
} }
file.Close() file.Close()
} else if (exists) {
log.Printf("[%s] already exists", filename)
} else { } else {
log.Printf("%s: %s", err, i.Filename) returnErr(w,r,err)
return
} }
io.WriteString(w, io.WriteString(w,
fmt.Sprintf("%s%s/i/%s\n", r.URL.Scheme, r.URL.Host, i.Filename)) fmt.Sprintf("%s%s/i/%s\n", r.URL.Scheme, r.URL.Host, filename))
LogRequest(r,200) LogRequest(r,200)
} }
@ -358,14 +352,14 @@ func routeRoot(w http.ResponseWriter, r *http.Request) {
// Show a page of most recent images, and tags, and uploaders ... // Show a page of most recent images, and tags, and uploaders ...
w.Header().Set("Content-Type", "text/html") w.Header().Set("Content-Type", "text/html")
var i Image var img ImgFile
iter := McImages.Find(bson.M{"date": 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()
fmt.Fprintf(w, "<li>\n") fmt.Fprintf(w, "<ul>\n")
for iter.Next(&i) { for iter.Next(&img) {
log.Println(i.Filename) log.Println(img.Filename)
fmt.Fprintf(w, "<ul>%s</ul>\n", linkToImage("", i.Filename)) fmt.Fprintf(w, "<li>%s</li>\n", linkToImage("", img.Filename))
} }
fmt.Fprintf(w, "</li>\n") fmt.Fprintf(w, "</ul>\n")
} }
func routeAll(w http.ResponseWriter, r *http.Request) { func routeAll(w http.ResponseWriter, r *http.Request) {
@ -374,16 +368,18 @@ func routeAll(w http.ResponseWriter, r *http.Request) {
http.NotFound(w,r) http.NotFound(w,r)
return return
} }
// Show a page of all the images
var i Image w.Header().Set("Content-Type", "text/html")
iter := McImages.Find(bson.M{"date": bson.M{"$gt": time.Now().Add(-time.Hour)}}).Limit(10).Iter() var img ImgFile
fmt.Fprintf(w, "<li>\n")
for iter.Next(&i) { // Show a page of all the images
log.Println(i.Filename) iter := gfs.Find(nil).Iter()
fmt.Fprintf(w, "<ul>%s</ul>\n", linkToImage("", i.Filename)) fmt.Fprintf(w, "<ul>\n")
for iter.Next(&img) {
log.Println(img.Filename)
fmt.Fprintf(w, "<li>%s</li>\n", linkToImage("", img.Filename))
} }
fmt.Fprintf(w, "</li>\n") fmt.Fprintf(w, "</ul>\n")
} }
/* /*
@ -409,23 +405,23 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
params := parseRawQuery(r.URL.RawQuery) params := parseRawQuery(r.URL.RawQuery)
log.Printf("K: params: %s", params) log.Printf("K: params: %s", params)
var i Image var img ImgFile
if (len(uriChunks) == 1) { if (len(uriChunks) == 1) {
// show a sorted list of tag name links // show a sorted list of tag name links
iter := McImages.Find(bson.M{"keywords": uriChunks[1]}).Sort("$natural").Limit(100).Iter() iter := gfs.Find(bson.M{"metadata": bson.M{"keywords": uriChunks[1] } }).Sort("$natural").Limit(100).Iter()
fmt.Fprintf(w, "<li>\n") fmt.Fprintf(w, "<li>\n")
for iter.Next(&i) { for iter.Next(&img) {
log.Println(i.Filename) log.Println(img.Filename)
fmt.Fprintf(w, "<ul>%s</ul>\n", linkToImage("", i.Filename)) fmt.Fprintf(w, "<ul>%s</ul>\n", linkToImage("", img.Filename))
} }
fmt.Fprintf(w, "</li>\n") fmt.Fprintf(w, "</li>\n")
} else if (len(uriChunks) == 2) { } else if (len(uriChunks) == 2) {
iter := McImages.Find(bson.M{"keywords": uriChunks[1]}).Limit(10).Iter() iter := gfs.Find(bson.M{"metadata": bson.M{"keywords": uriChunks[1] } }).Limit(10).Iter()
fmt.Fprintf(w, "<li>\n") fmt.Fprintf(w, "<li>\n")
for iter.Next(&i) { for iter.Next(&img) {
log.Println(i.Filename) log.Println(img.Filename)
fmt.Fprintf(w, "<ul>%s</ul>\n", linkToImage("", i.Filename)) fmt.Fprintf(w, "<ul>%s</ul>\n", linkToImage("", img.Filename))
} }
fmt.Fprintf(w, "</li>\n") fmt.Fprintf(w, "</li>\n")
} else if (uriChunks[2] == "r") { } else if (uriChunks[2] == "r") {
@ -445,14 +441,12 @@ func routeIPs(w http.ResponseWriter, r *http.Request) {
} }
func initMongo() { func initMongo() {
MSession, err := mgo.Dial(MongoHost) mongo_session, err := mgo.Dial(MongoHost)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
images_db = mongo_session.DB(MongoDB)
MongoImagesDb = MSession.DB(MongoDB) gfs = images_db.GridFS("fs")
Mgfs = MongoImagesDb.GridFS("fs")
McImages = MongoImagesDb.C(MongoCollectionImages)
} }
/* Run as the image server */ /* Run as the image server */
@ -460,7 +454,7 @@ func runServer(ip, port string) {
var addr = fmt.Sprintf("%s:%s", ip, port) var addr = fmt.Sprintf("%s:%s", ip, port)
initMongo() initMongo()
defer MSession.Close() defer mongo_session.Close()
http.HandleFunc("/", routeRoot) http.HandleFunc("/", routeRoot)
http.HandleFunc("/all", routeAll) http.HandleFunc("/all", routeAll)