Browse files

Switch to use ServeContent instead of io.copy

  • Loading branch information...
1 parent 166e3bb commit 1dccfefce56554a69b37228f714fab81ec35ac41 @jgraham909 jgraham909 committed with jgraham909 Feb 25, 2013
Showing with 17 additions and 11 deletions.
  1. +8 −4 controller.go
  2. +9 −7 results.go
View
12 controller.go
@@ -10,6 +10,7 @@ import (
"runtime"
"runtime/debug"
"strings"
+ "time"
)
type Controller struct {
@@ -261,18 +262,21 @@ func (c *Controller) Forbidden(msg string, objs ...interface{}) Result {
// The name and size are taken from the file info.
func (c *Controller) RenderFile(file *os.File, delivery ContentDisposition) Result {
var length int64 = -1
+ modtime := time.Now()
fileInfo, err := file.Stat()
if err != nil {
WARN.Println("RenderFile error:", err)
}
if fileInfo != nil {
length = fileInfo.Size()
+ modtime = fileInfo.ModTime()
}
return &BinaryResult{
- Reader: file,
- Name: filepath.Base(file.Name()),
- Length: length,
- Delivery: delivery,
+ ReadSeeker: file,
+ Name: filepath.Base(file.Name()),
+ Length: length,
+ Delivery: delivery,
+ ModTime: modtime,
}
}
View
16 results.go
@@ -9,6 +9,7 @@ import (
"io"
"net/http"
"reflect"
+ "time"
)
type Result interface {
@@ -230,10 +231,11 @@ var (
)
type BinaryResult struct {
- Reader io.Reader
- Name string
- Length int64
- Delivery ContentDisposition
+ ReadSeeker io.ReadSeeker
+ Name string
+ Length int64
+ Delivery ContentDisposition
+ ModTime time.Time
}
func (r *BinaryResult) Apply(req *Request, resp *Response) {
@@ -246,11 +248,11 @@ func (r *BinaryResult) Apply(req *Request, resp *Response) {
if r.Length != -1 {
resp.Out.Header().Set("Content-Length", fmt.Sprintf("%d", r.Length))
}
- resp.WriteHeader(http.StatusOK, ContentTypeByFilename(r.Name))
- io.Copy(resp.Out, r.Reader)
+
+ http.ServeContent(resp.Out, req.Request, r.Name, r.ModTime, r.ReadSeeker)
// Close the Reader if we can
- v, ok := r.Reader.(io.Closer)
+ v, ok := r.ReadSeeker.(io.Closer)
if ok {
v.Close()
}

0 comments on commit 1dccfef

Please sign in to comment.