forked from ungerik/go-start
/
staticfile.go
47 lines (39 loc) · 1.05 KB
/
staticfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package view
import (
"io/ioutil"
"path"
"time"
"github.com/ungerik/go-start/errs"
)
func NewStaticFile(filename string) *StaticFile {
return &StaticFile{Filename: filename}
}
// StaticFile renders a static file.
// The output is cached in memory but changes to the file on the filesystem
// cause the the cache to be rebuilt.
type StaticFile struct {
ViewWithURLBase
Filename string
// Will be set automatically from Filename if empty
ContentTypeExt string
modifiedTime time.Time
cachedFileData []byte
}
func (self *StaticFile) Render(ctx *Context) (err error) {
filePath, found, modified := FindStaticFile(self.Filename)
if !found {
return errs.Format("Static file not found: %s", self.Filename)
}
if self.ContentTypeExt == "" {
self.ContentTypeExt = path.Ext(filePath)
}
if self.cachedFileData == nil || modified.After(self.modifiedTime) {
self.cachedFileData, err = ioutil.ReadFile(filePath)
if err != nil {
return err
}
}
ctx.Response.SetContentTypeByExt(self.ContentTypeExt)
ctx.Response.Write(self.cachedFileData)
return nil
}