2018-12-08 19:18:51 +00:00
|
|
|
package goldsmith
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"hash/crc32"
|
2023-11-11 19:04:37 +00:00
|
|
|
"io"
|
2018-12-08 19:18:51 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2023-12-10 17:41:50 +00:00
|
|
|
"strings"
|
2018-12-08 19:18:51 +00:00
|
|
|
)
|
|
|
|
|
2018-12-10 01:00:46 +00:00
|
|
|
type cache struct {
|
2018-12-08 19:18:51 +00:00
|
|
|
baseDir string
|
|
|
|
}
|
|
|
|
|
2022-01-09 02:08:36 +00:00
|
|
|
func (self *cache) retrieveFile(context *Context, outputPath string, inputFiles []*File) (*File, error) {
|
|
|
|
cachePath, err := self.buildCachePath(context, outputPath, inputFiles)
|
2018-12-08 19:18:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
outputFile, err := context.CreateFileFromAsset(outputPath, cachePath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return outputFile, nil
|
|
|
|
}
|
|
|
|
|
2022-01-09 02:08:36 +00:00
|
|
|
func (self *cache) storeFile(context *Context, outputFile *File, inputFiles []*File) error {
|
|
|
|
cachePath, err := self.buildCachePath(context, outputFile.Path(), inputFiles)
|
2018-12-08 19:18:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-09 02:08:36 +00:00
|
|
|
if err := os.MkdirAll(self.baseDir, 0755); err != nil {
|
2018-12-08 19:18:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fp, err := os.Create(cachePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fp.Close()
|
|
|
|
|
2023-11-11 19:04:37 +00:00
|
|
|
offset, err := outputFile.Seek(0, io.SeekCurrent)
|
2018-12-08 19:18:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-11 19:04:37 +00:00
|
|
|
if _, err := outputFile.Seek(0, io.SeekStart); err != nil {
|
2018-12-08 19:18:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := outputFile.WriteTo(fp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-11 19:04:37 +00:00
|
|
|
if _, err := outputFile.Seek(offset, io.SeekStart); err != nil {
|
2018-12-08 19:18:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-09 02:08:36 +00:00
|
|
|
func (self *cache) buildCachePath(context *Context, outputPath string, inputFiles []*File) (string, error) {
|
2018-12-08 19:18:51 +00:00
|
|
|
hasher := crc32.NewIEEE()
|
|
|
|
hasher.Write([]byte(outputPath))
|
|
|
|
|
2023-12-10 17:41:50 +00:00
|
|
|
sort.Slice(inputFiles, func(i, j int) bool {
|
|
|
|
return strings.Compare(inputFiles[i].Path(), inputFiles[j].Path()) < 0
|
|
|
|
})
|
2018-12-08 19:18:51 +00:00
|
|
|
|
2022-01-09 02:08:36 +00:00
|
|
|
for _, inputFile := range inputFiles {
|
|
|
|
modTimeBuff := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(modTimeBuff, uint64(inputFile.ModTime().UnixNano()))
|
2018-12-08 19:18:51 +00:00
|
|
|
hasher.Write([]byte(inputFile.Path()))
|
2022-01-09 02:08:36 +00:00
|
|
|
hasher.Write(modTimeBuff)
|
2018-12-08 19:18:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-09 02:08:36 +00:00
|
|
|
cachePath := filepath.Join(self.baseDir, fmt.Sprintf(
|
2018-12-08 19:18:51 +00:00
|
|
|
"gs_%.8x%s",
|
|
|
|
hasher.Sum32(),
|
|
|
|
filepath.Ext(outputPath),
|
|
|
|
))
|
|
|
|
|
|
|
|
return cachePath, nil
|
|
|
|
}
|