Skip to content

Commit

Permalink
change type of src/utils/http_utils.go#staticFilesFile.data to byte a…
Browse files Browse the repository at this point in the history
…rray
  • Loading branch information
genshen committed Feb 1, 2018
1 parent 7163e0b commit 7ac226d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Binary file modified Screenshots/shot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 12 additions & 12 deletions src/utils/http_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func GetQueryInt32(r *http.Request, key string, defaultValue uint32) uint32 {
return uint32(value)
}

// serve all views files
// serve all views files from memory storage.
// basic idea: https://github.com/bouk/staticfiles
type staticFilesFile struct {
data string
data []byte
mime string
mtime time.Time
// size is the size before compression. If 0, it means the data is uncompressed
Expand All @@ -69,7 +69,7 @@ var staticFiles = make(map[string]*staticFilesFile)
// It defaults to http.NotFound but can be overwritten
var NotFound = http.NotFound

// read all files in views directory to map "staticFiles"
// read all files in views directory and map to "staticFiles"
func initHttpUtils() {
files := processDir(Config.Site.ViewsDir, "")
for _, file := range files {
Expand Down Expand Up @@ -99,15 +99,15 @@ func initHttpUtils() {
file = strings.Replace(file, "\\", "/", -1)
if b2.Len() < b.Len() {
staticFiles[file] = &staticFilesFile{
data: b2.String(),
data: b2.Bytes(),
mime: mime.TypeByExtension(filepath.Ext(file)),
mtime: time.Unix(stat.ModTime().Unix(), 0),
size: stat.Size(),
hash: hex.EncodeToString(hash.Sum(nil)),
}
} else {
staticFiles[file] = &staticFilesFile{
data: b.String(),
data: b.Bytes(),
mime: mime.TypeByExtension(filepath.Ext(file)),
mtime: time.Unix(stat.ModTime().Unix(), 0),
hash: hex.EncodeToString(hash.Sum(nil)),
Expand All @@ -119,7 +119,7 @@ func initHttpUtils() {
}
}

// todo memory!!
// todo large memory!!
func processDir(prefix, dir string) (fileSlice []string) {
files, err := ioutil.ReadDir(filepath.Join(prefix, dir))
var allFiles []string
Expand Down Expand Up @@ -177,17 +177,17 @@ func ServeHTTPByName(rw http.ResponseWriter, req *http.Request, filename string)
header.Set("Content-Type", f.mime)

// Check if the asset is compressed in the binary
if f.size == 0 {
if f.size == 0 { // not compressed
header.Set("Content-Length", strconv.Itoa(len(f.data)))
io.WriteString(rw, f.data)
rw.Write(f.data)
} else {
if header.Get("Content-Encoding") == "" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
header.Set("Content-Encoding", "gzip")
header.Set("Content-Length", strconv.Itoa(len(f.data)))
io.WriteString(rw, f.data)
rw.Write(f.data)
} else {
header.Set("Content-Length", strconv.Itoa(int(f.size)))
reader, _ := gzip.NewReader(strings.NewReader(f.data))
reader, _ := gzip.NewReader(bytes.NewReader(f.data))
io.Copy(rw, reader)
reader.Close()
}
Expand All @@ -207,9 +207,9 @@ func Open(name string) (io.ReadCloser, error) {
}

if f.size == 0 {
return ioutil.NopCloser(strings.NewReader(f.data)), nil
return ioutil.NopCloser(bytes.NewReader(f.data)), nil
}
return gzip.NewReader(strings.NewReader(f.data))
return gzip.NewReader(bytes.NewReader(f.data))
}

// ModTime returns the modification time of the original file.
Expand Down

0 comments on commit 7ac226d

Please sign in to comment.