diff --git a/server/gridfs.go b/server/gridfs.go index 0badfc8..d9b89ee 100644 --- a/server/gridfs.go +++ b/server/gridfs.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "log" "time" "gopkg.in/mgo.v2" @@ -168,7 +169,11 @@ func (g GridfsStorage) StoreChildImage( original Cacheable, entry *Entry, ) (Cacheable, error) { - gridfs := g.Connection.DB(database).GridFS("fs") + con := g.Connection.Copy() + defer con.Close() + con.EnsureSafe(&mgo.Safe{W: 1, J: true}) + + gridfs := con.DB(database).GridFS("fs") targetfile, err := gridfs.Create(getRandomFilename(imageFormat)) if err != nil { @@ -180,6 +185,8 @@ func (g GridfsStorage) StoreChildImage( _, err = io.Copy(targetfile, reader) if err != nil { + log.Printf("Error for filename %s with size %dx%d\n", original.Name(), entry.Width, entry.Height) + log.Printf("Could not write file completely, cleaning %s\n", targetfile.Name()) targetfile.Abort() return nil, err } diff --git a/server/server.go b/server/server.go index e97e193..b331ede 100644 --- a/server/server.go +++ b/server/server.go @@ -165,7 +165,7 @@ func imageHandler( return } - // we found a image but did not want resizing + // we found an image but did not want resizing if found { if !isModified(foundImage, &r.Header) { w.WriteHeader(http.StatusNotModified) @@ -201,7 +201,7 @@ func imageHandler( controller, err := paint.NewController(foundImage.Data(), customResizers) if err != nil { - log.Printf("%d image could not be decoded Reason %s.\n", http.StatusNotFound, err.Error()) + log.Printf("%d image could not be decoded. Reason: [%s].\n", http.StatusNotFound, err.Error()) w.WriteHeader(http.StatusNotFound) return } @@ -218,11 +218,12 @@ func imageHandler( buffer := bufio.NewWriter(&b) controller.Encode(buffer) buffer.Flush() + data := b.Bytes() targetfile, err := storage.StoreChildImage( requestConfig.Database, controller.Format(), - bytes.NewReader(b.Bytes()), + bytes.NewReader(data), controller.Image().Bounds().Dx(), controller.Image().Bounds().Dy(), foundImage, @@ -235,8 +236,8 @@ func imageHandler( return } - w.Write(b.Bytes()) setCacheHeaders(targetfile, w) + w.Write(data) log.Printf("%d image succesfully resized and returned.\n", http.StatusOK) }