Skip to content

Commit

Permalink
Merge pull request #15 from VoycerAG/stability_fixes
Browse files Browse the repository at this point in the history
Ensure no corrupt images will be stored
  • Loading branch information
sharpner committed Dec 21, 2015
2 parents 349cbd2 + 9993108 commit a91014c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
15 changes: 1 addition & 14 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"flag"
"fmt"
"log"
"net/http"

"gopkg.in/mgo.v2"
"gopkg.in/tylerb/graceful.v1"

"github.com/VoycerAG/gridfs-image-server/server"
)
Expand Down Expand Up @@ -52,7 +52,7 @@ func main() {

log.Printf("Server started. Listening on %d database host is %s\n", *serverPort, *host)

graceful.Run(fmt.Sprintf(":%d", *serverPort), 0, handler)
err = http.ListenAndServe(fmt.Sprintf(":%d", *serverPort), handler)
if err != nil {
log.Fatal(err)
}
Expand Down
8 changes: 7 additions & 1 deletion server/gridfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,15 @@ func (g GridfsStorage) StoreChildImage(
if err != nil {
return nil, err
}

defer targetfile.Close()

io.Copy(targetfile, reader)
_, err = io.Copy(targetfile, reader)

if err != nil {
targetfile.Abort()
return nil, err
}

metadata := bson.M{
"width": imageWidth,
Expand Down
4 changes: 2 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ func imageHandler(w http.ResponseWriter, r *http.Request, requestConfig *Configu

var b bytes.Buffer
buffer := bufio.NewWriter(&b)
writer := io.MultiWriter(w, buffer)
controller.Encode(writer)
controller.Encode(buffer)
buffer.Flush()

targetfile, err := storage.StoreChildImage(
Expand All @@ -230,6 +229,7 @@ func imageHandler(w http.ResponseWriter, r *http.Request, requestConfig *Configu
return
}

w.Write(b.Bytes())
setCacheHeaders(targetfile, w)

log.Printf("%d image succesfully resized and returned.\n", http.StatusOK)
Expand Down

0 comments on commit a91014c

Please sign in to comment.