1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-12-27 00:06:29 +00:00
This commit is contained in:
Vincent Batts 2013-01-14 08:46:02 -05:00
parent 1d83869269
commit 9fdcddcfb6

View file

@ -55,13 +55,11 @@ var (
FileKeywords = "" FileKeywords = ""
) )
type Image struct { type Info struct {
Filename string // name for the given data
Keywords []string // tags Keywords []string // tags
MimeType string // maybe preserve the type provided? MimeType string // maybe preserve the type provided?
Ip string // who uploaded it Ip string // who uploaded it
Date time.Time // when? Date time.Time // when?
//Name string // not sure that this is different than Filename
} }
/* Check whether this Image filename is on Mongo */ /* Check whether this Image filename is on Mongo */
@ -186,6 +184,10 @@ func getSmallHash() (small_hash string) {
return fmt.Sprintf("%X", h.Sum(nil)) return fmt.Sprintf("%X", h.Sum(nil))
} }
/*
GET /i/
GET /i/:name
*/
// Show a page of most recent images, and tags, and uploaders ... // Show a page of most recent images, and tags, and uploaders ...
func routeImagesGET(w http.ResponseWriter, r *http.Request) { func routeImagesGET(w http.ResponseWriter, r *http.Request) {
uriChunks := chunkURI(r.URL.Path) uriChunks := chunkURI(r.URL.Path)
@ -195,23 +197,22 @@ func routeImagesGET(w http.ResponseWriter, r *http.Request) {
return return
} }
// preliminary checks, if they've passed an image name
if (len(uriChunks) == 2 && len(uriChunks[1]) > 0) { if (len(uriChunks) == 2 && len(uriChunks[1]) > 0) {
exists, err := hasImageByFilename(uriChunks[1]) query, err := Mgfs.Find(bson.M{"metadata": uriChunks[1] })
// preliminary checks, if they've passed an image name
if (err != nil) { if (err != nil) {
LogRequest(r,503) LogRequest(r,503)
fmt.Fprintf(w,"Error fetching image: %s", err) fmt.Fprintf(w,"Error fetching image: %s", err)
http.Error(w, "Service Unavailable", 503) http.Error(w, "Service Unavailable", 503)
return return
} }
if (!exists) { if (query.Count() == 0) {
LogRequest(r,404) LogRequest(r,404)
http.NotFound(w,r) http.NotFound(w,r)
return return
} }
}
if (len(uriChunks) == 2 && len(uriChunks[1]) > 0) {
ext := filepath.Ext(uriChunks[1]) ext := filepath.Ext(uriChunks[1])
w.Header().Set("Content-Type", mime.TypeByExtension(ext)) w.Header().Set("Content-Type", mime.TypeByExtension(ext))
w.Header().Set("Cache-Control", "max-age=315360000") w.Header().Set("Cache-Control", "max-age=315360000")
@ -234,6 +235,9 @@ func routeImagesGET(w http.ResponseWriter, r *http.Request) {
LogRequest(r,200) LogRequest(r,200)
} }
/*
POST /i/[:name][?k=v&k=v]
*/
// Create the file by the name in the path and/or parameter? // Create the file by the name in the path and/or parameter?
// add keywords from the parameters // add keywords from the parameters
// look for an image in the r.Body // look for an image in the r.Body
@ -247,7 +251,7 @@ func routeImagesPOST(w http.ResponseWriter, r *http.Request) {
return return
} }
var i Image var i Info
i.Date = bson.Now() i.Date = bson.Now()
i.Ip = r.RemoteAddr i.Ip = r.RemoteAddr
@ -284,6 +288,7 @@ func routeImagesPOST(w http.ResponseWriter, r *http.Request) {
i.Filename = fmt.Sprintf("%s%s", str, p_ext) i.Filename = fmt.Sprintf("%s%s", str, p_ext)
} }
} }
err := saveImage(i) err := saveImage(i)
if (err != nil && err.Error() != "Image Filename Exists") { if (err != nil && err.Error() != "Image Filename Exists") {
file, err := Mgfs.Create(i.Filename) file, err := Mgfs.Create(i.Filename)
@ -310,6 +315,7 @@ func routeImagesPOST(w http.ResponseWriter, r *http.Request) {
} else { } else {
log.Printf("%s: %s", err, i.Filename) log.Printf("%s: %s", err, i.Filename)
} }
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, i.Filename))