2021-02-17 08:36:17 +01:00
|
|
|
// Copyright 2021 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package static // import "miniflux.app/ui/static"
|
|
|
|
|
|
|
|
import (
|
2021-02-18 06:58:04 +01:00
|
|
|
"bytes"
|
2021-02-17 08:36:17 +01:00
|
|
|
"crypto/sha256"
|
|
|
|
"embed"
|
|
|
|
"fmt"
|
2021-02-18 06:58:04 +01:00
|
|
|
|
|
|
|
"github.com/tdewolff/minify/v2"
|
|
|
|
"github.com/tdewolff/minify/v2/css"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Static assets.
|
|
|
|
var (
|
|
|
|
StylesheetBundleChecksums map[string]string
|
|
|
|
StylesheetBundles map[string][]byte
|
2021-02-17 08:36:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed bin/*
|
|
|
|
var binaryFiles embed.FS
|
|
|
|
|
2021-02-18 06:58:04 +01:00
|
|
|
//go:embed css/*.css
|
|
|
|
var stylesheetFiles embed.FS
|
|
|
|
|
2021-02-17 08:36:17 +01:00
|
|
|
var binaryFileChecksums map[string]string
|
|
|
|
|
2021-02-18 06:58:04 +01:00
|
|
|
// CalculateBinaryFileChecksums generates hash of embed binary files.
|
|
|
|
func CalculateBinaryFileChecksums() error {
|
2021-02-17 08:36:17 +01:00
|
|
|
binaryFileChecksums = make(map[string]string)
|
|
|
|
|
|
|
|
dirEntries, err := binaryFiles.ReadDir("bin")
|
|
|
|
if err != nil {
|
2021-02-18 06:58:04 +01:00
|
|
|
return err
|
2021-02-17 08:36:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, dirEntry := range dirEntries {
|
|
|
|
data, err := LoadBinaryFile(dirEntry.Name())
|
|
|
|
if err != nil {
|
2021-02-18 06:58:04 +01:00
|
|
|
return err
|
2021-02-17 08:36:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
binaryFileChecksums[dirEntry.Name()] = fmt.Sprintf("%x", sha256.Sum256(data))
|
|
|
|
}
|
2021-02-18 06:58:04 +01:00
|
|
|
|
|
|
|
return nil
|
2021-02-17 08:36:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadBinaryFile loads an embed binary file.
|
|
|
|
func LoadBinaryFile(filename string) ([]byte, error) {
|
|
|
|
return binaryFiles.ReadFile(fmt.Sprintf(`bin/%s`, filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBinaryFileChecksum returns a binary file checksum.
|
|
|
|
func GetBinaryFileChecksum(filename string) (string, error) {
|
|
|
|
if _, found := binaryFileChecksums[filename]; !found {
|
|
|
|
return "", fmt.Errorf(`static: unable to find checksum for %q`, filename)
|
|
|
|
}
|
|
|
|
return binaryFileChecksums[filename], nil
|
|
|
|
}
|
2021-02-18 06:58:04 +01:00
|
|
|
|
|
|
|
// GenerateStylesheetsBundles creates CSS bundles.
|
|
|
|
func GenerateStylesheetsBundles() error {
|
|
|
|
var bundles = map[string][]string{
|
|
|
|
"light_serif": {"css/light.css", "css/serif.css", "css/common.css"},
|
|
|
|
"light_sans_serif": {"css/light.css", "css/sans_serif.css", "css/common.css"},
|
|
|
|
"dark_serif": {"css/dark.css", "css/serif.css", "css/common.css"},
|
|
|
|
"dark_sans_serif": {"css/dark.css", "css/sans_serif.css", "css/common.css"},
|
|
|
|
"system_serif": {"css/system.css", "css/serif.css", "css/common.css"},
|
|
|
|
"system_sans_serif": {"css/system.css", "css/sans_serif.css", "css/common.css"},
|
|
|
|
}
|
|
|
|
|
|
|
|
StylesheetBundles = make(map[string][]byte)
|
|
|
|
StylesheetBundleChecksums = make(map[string]string)
|
|
|
|
|
|
|
|
minifier := minify.New()
|
|
|
|
minifier.AddFunc("text/css", css.Minify)
|
|
|
|
|
|
|
|
for bundle, srcFiles := range bundles {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
|
|
|
for _, srcFile := range srcFiles {
|
|
|
|
fileData, err := stylesheetFiles.ReadFile(srcFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.Write(fileData)
|
|
|
|
}
|
|
|
|
|
|
|
|
minifiedData, err := minifier.Bytes("text/css", buffer.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
StylesheetBundles[bundle] = minifiedData
|
|
|
|
StylesheetBundleChecksums[bundle] = fmt.Sprintf("%x", sha256.Sum256(minifiedData))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|