1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package main
- import (
- "encoding/hex"
- "fmt"
- "io"
- "os"
- "sync"
- "github.com/superp00t/etc"
- "github.com/superp00t/etc/yo"
- )
- var cacheLock = new(sync.Mutex)
- var cachePath etc.Path
- func initCache() {
- if !dirExists("cache") {
- mkdir("cache")
- }
- if !dirExists("cache") {
- yo.Fatal("could not create cache folder")
- }
- yo.Println("Initialized cache!")
- }
- func cacheLoad(host, path string) (io.Reader, bool, error) {
- h := etc.NewBuffer()
- h.WriteUString(host)
- h.WriteUString(path)
- chk := hex.EncodeToString(h.Sha512Digest())
- if !cachePath.Exists(chk[0:2]) {
- return nil, false, fmt.Errorf("no file found")
- }
- cpath := cachePath.GetSub(etc.Path{chk[0:2], chk}).Render()
- if !cachePath.Exists(cpath) {
- return nil, false, fmt.Errorf("no file found")
- }
- f, err := cachePath.Get(path)
- return f, false, err
- }
- func dirExists(path string) bool {
- i, err := os.Stat(path)
- if err == nil && i.IsDir() {
- return true
- }
- return false
- }
- func mkdir(path string) {
- err := os.MkdirAll(path, 0700)
- if err != nil {
- panic(err)
- }
- }
|