From d03ab083d7d337daf81ee1e822eb43856eaa02df Mon Sep 17 00:00:00 2001 From: NI Date: Fri, 13 Sep 2019 14:21:24 +0800 Subject: [PATCH] Statically save Content-Type in generated files as well. --- application/controller/failure.go | 2 +- application/controller/home.go | 2 +- application/controller/static.go | 22 ++------- .../controller/static_page_generater/main.go | 46 +++++++++++++++++-- 4 files changed, 48 insertions(+), 24 deletions(-) diff --git a/application/controller/failure.go b/application/controller/failure.go index c7e9d1c..2075906 100644 --- a/application/controller/failure.go +++ b/application/controller/failure.go @@ -29,5 +29,5 @@ func serveFailure( r *http.Request, l log.Logger, ) error { - return serveStaticPage("error.html", ".html", err.Code(), w, r, l) + return serveStaticPage("error.html", err.Code(), w, r, l) } diff --git a/application/controller/home.go b/application/controller/home.go index dbe0a0a..d9996cb 100644 --- a/application/controller/home.go +++ b/application/controller/home.go @@ -29,5 +29,5 @@ type home struct { } func (h home) Get(w http.ResponseWriter, r *http.Request, l log.Logger) error { - return serveStaticCachePage("index.html", ".html", w, r, l) + return serveStaticCachePage("index.html", w, r, l) } diff --git a/application/controller/static.go b/application/controller/static.go index 8e89720..e85d08e 100644 --- a/application/controller/static.go +++ b/application/controller/static.go @@ -18,7 +18,6 @@ package controller import ( - "mime" "net/http" "strconv" "strings" @@ -33,6 +32,7 @@ type staticData struct { compressd []byte compressdHash string created time.Time + contentType string } func (s staticData) hasCompressed() bool { @@ -60,12 +60,11 @@ func serveStaticCacheData( return ErrNotFound } - return serveStaticCachePage(dataName, fileExt, w, r, l) + return serveStaticCachePage(dataName, w, r, l) } func serveStaticCachePage( dataName string, - fileExt string, w http.ResponseWriter, r *http.Request, l log.Logger, @@ -110,13 +109,7 @@ func serveStaticCachePage( w.Header().Add("Cache-Control", "public, max-age=31536000") w.Header().Add("ETag", "\""+selectedDataHash+"\"") - mimeType := mime.TypeByExtension(fileExt) - - if len(mimeType) > 0 { - w.Header().Add("Content-Type", mimeType) - } else { - w.Header().Add("Content-Type", "application/binary") - } + w.Header().Add("Content-Type", d.contentType) if compressEnabled { w.Header().Add("Content-Encoding", "gzip") @@ -132,7 +125,6 @@ func serveStaticCachePage( func serveStaticPage( dataName string, - fileExt string, code int, w http.ResponseWriter, r *http.Request, @@ -157,13 +149,7 @@ func serveStaticPage( w.Header().Add("Vary", "Accept-Encoding") } - mimeType := mime.TypeByExtension(fileExt) - - if len(mimeType) > 0 { - w.Header().Add("Content-Type", mimeType) - } else { - w.Header().Add("Content-Type", "application/binary") - } + w.Header().Add("Content-Type", d.contentType) if compressEnabled { w.Header().Add("Content-Encoding", "gzip") diff --git a/application/controller/static_page_generater/main.go b/application/controller/static_page_generater/main.go index f31d7c9..63637fd 100644 --- a/application/controller/static_page_generater/main.go +++ b/application/controller/static_page_generater/main.go @@ -27,6 +27,7 @@ import ( "fmt" "io" "io/ioutil" + "mime" "os" "path/filepath" "strconv" @@ -61,6 +62,7 @@ func parseStaticData( compressedHash string, creation time.Time, data []byte, + contentType string, ) staticData { return staticData{ data: data[fileStart:fileEnd], @@ -68,6 +70,7 @@ func parseStaticData( compressd: data[compressedStart:compressedEnd], compressdHash: compressedHash, created: creation, + contentType: contentType, } } ` @@ -79,11 +82,13 @@ import "compress/gzip" import "encoding/base64" import "time" import "crypto/sha256" +import "mime" +import "strings" // WARNING: THIS GENERATION IS FOR DEBUG / DEVELOPMENT ONLY, DO NOT // USE IT IN PRODUCTION! -func staticFileGen(filePath string) staticData { +func staticFileGen(fileName, filePath string) staticData { content, readErr := ioutil.ReadFile(filePath) if readErr != nil { @@ -93,7 +98,7 @@ func staticFileGen(filePath string) staticData { compressed := bytes.NewBuffer(make([]byte, 0, 1024)) compresser, compresserBuildErr := gzip.NewWriterLevel( - compressed, gzip.BestCompression) + compressed, gzip.BestSpeed) if compresserBuildErr != nil { panic(fmt.Sprintln("Cannot build data compresser:", compresserBuildErr)) @@ -122,8 +127,22 @@ func staticFileGen(filePath string) staticData { return h.Sum(nil) } + fileExtDotIdx := strings.LastIndex(fileName, ".") + fileExt := "" + + if fileExtDotIdx >= 0 { + fileExt = fileName[fileExtDotIdx:len(fileName)] + } + + mimeType := mime.TypeByExtension(fileExt) + + if len(mimeType) <= 0 { + mimeType = "application/binary" + } + return staticData{ data: content[0:contentLen], + contentType: mimeType, dataHash: base64.StdEncoding.EncodeToString( getHash(content[0:contentLen])[:8]), compressd: content[contentLen:], @@ -136,7 +155,7 @@ func staticFileGen(filePath string) staticData { var ( staticPages = map[string]staticData{ {{ range . }}"{{ .Name }}": staticFileGen( - "{{ .Path }}", + "{{ .Name }}", "{{ .Path }}", ), {{ end }} } @@ -171,6 +190,7 @@ func {{ .GOVariableName }}() ( string, // CompressedHash time.Time, // Time of creation []byte, // Data + string, // ContentType ) { created, createErr := time.Parse( time.RFC1123, "{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 MST" }}") @@ -188,10 +208,12 @@ func {{ .GOVariableName }}() ( shrinkToFit := make([]byte, len(data)) copy(shrinkToFit, data) + data = nil return {{ .FileStart }}, {{ .FileEnd }}, {{ .CompressedStart }}, {{ .CompressedEnd }}, - "{{ .ContentHash }}", "{{ .CompressedHash }}", created, shrinkToFit + "{{ .ContentHash }}", "{{ .CompressedHash }}", + created, shrinkToFit, "{{ .ContentType }}" } ` ) @@ -207,10 +229,12 @@ type parsedFile struct { GOPackage string Path string Data string + Type string FileStart int FileEnd int CompressedStart int CompressedEnd int + ContentType string ContentHash string CompressedHash string Date time.Time @@ -283,6 +307,19 @@ func parseFile( goFileName := "Static" + strconv.FormatInt(int64(id), 10) + fileExtDotIdx := strings.LastIndex(name, ".") + fileExt := "" + + if fileExtDotIdx >= 0 { + fileExt = name[fileExtDotIdx:len(name)] + } + + mimeType := mime.TypeByExtension(fileExt) + + if len(mimeType) <= 0 { + mimeType = "application/binary" + } + return parsedFile{ Name: name, GOVariableName: strings.Title(goFileName), @@ -294,6 +331,7 @@ func parseFile( FileEnd: contentLen, CompressedStart: contentLen, CompressedEnd: len(content), + ContentType: mimeType, ContentHash: base64.StdEncoding.EncodeToString( getHash(content[0:contentLen])[:8]), CompressedHash: base64.StdEncoding.EncodeToString(