rewrote cache to work better
This commit is contained in:
		
							
								
								
									
										97
									
								
								internal/cache/cache.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								internal/cache/cache.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,97 @@ | ||||
| package cache | ||||
|  | ||||
| import ( | ||||
| 	"crypto/sha1" | ||||
| 	"encoding/hex" | ||||
| 	"errors" | ||||
| 	"io" | ||||
| 	"net/http" | ||||
| 	"net/url" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
|  | ||||
| 	"github.com/labstack/echo/v4" | ||||
| ) | ||||
|  | ||||
| type Cache struct { | ||||
| 	CacheDir string | ||||
| 	Disabled bool | ||||
| } | ||||
|  | ||||
| func getCacheDir() (string, error) { | ||||
| 	baseDir, err := os.UserCacheDir() | ||||
| 	if err != nil { | ||||
| 		baseDir = "/tmp" | ||||
| 	} | ||||
| 	cacheDir := filepath.Join(baseDir, "startpage") | ||||
| 	err = os.MkdirAll(cacheDir, 0o755) | ||||
| 	if err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	return cacheDir, nil | ||||
| } | ||||
|  | ||||
| func getProfileCacheDir(profile string) (string, error) { | ||||
| 	var profileCacheDir string | ||||
|  | ||||
| 	cacheDir, err := getCacheDir() | ||||
| 	if err != nil { | ||||
| 		return profileCacheDir, err | ||||
| 	} | ||||
|  | ||||
| 	profileCacheDir = filepath.Join(cacheDir, profile) | ||||
| 	err = os.MkdirAll(cacheDir, 0o755) | ||||
| 	return profileCacheDir, err | ||||
| } | ||||
|  | ||||
| func NewCache(profile string) Cache { | ||||
| 	cacheDir, err := getProfileCacheDir(profile) | ||||
|  | ||||
| 	return Cache{ | ||||
| 		CacheDir: cacheDir, | ||||
| 		Disabled: err != nil, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| const baseCacheUrl = "cache" | ||||
|  | ||||
| func (c Cache) StartStaticServer(e *echo.Echo) error { | ||||
| 	e.Static("/"+baseCacheUrl, c.CacheDir) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func hashUrl(url string) string { | ||||
| 	h := sha1.New() | ||||
| 	io.WriteString(h, url) | ||||
| 	return hex.EncodeToString(h.Sum(nil)) | ||||
| } | ||||
|  | ||||
| func (c Cache) CacheUrl(urlString string) (string, error) { | ||||
| 	filename := hashUrl(urlString) + filepath.Ext(urlString) | ||||
| 	targetPath := filepath.Join(c.CacheDir, filename) | ||||
|  | ||||
| 	// if the file was already downloaded it doesn't need to be downloaded again | ||||
| 	if _, err := os.Stat(targetPath); errors.Is(err, os.ErrNotExist) { | ||||
| 		resp, err := http.Get(urlString) | ||||
| 		if !errors.Is(err, os.ErrNotExist) { | ||||
| 			return urlString, err | ||||
| 		} | ||||
| 		defer resp.Body.Close() | ||||
|  | ||||
| 		file, err := os.Create(targetPath) | ||||
| 		if err != nil { | ||||
| 			return urlString, err | ||||
| 		} | ||||
| 		defer file.Close() | ||||
|  | ||||
| 		_, err = io.Copy(file, resp.Body) | ||||
|  | ||||
| 		if err != nil { | ||||
| 			return urlString, err | ||||
| 		} | ||||
| 	} else { | ||||
| 		return url.JoinPath(baseCacheUrl, filename) | ||||
| 	} | ||||
|  | ||||
| 	return url.JoinPath(baseCacheUrl, filename) | ||||
| } | ||||
| @@ -138,12 +138,3 @@ func (rc *Config) LoadConfigFile(file string) error { | ||||
|  | ||||
| 	return toml.Unmarshal(content, rc) | ||||
| } | ||||
|  | ||||
| func (c *Config) Init() error { | ||||
| 	log.Println("downloading website icons...") | ||||
| 	for i := range c.Template.Websites { | ||||
| 		c.Template.Websites[i].Cache() | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
| @@ -5,6 +5,7 @@ import ( | ||||
| 	"net/http" | ||||
| 	"strconv" | ||||
|  | ||||
| 	"gitea.elara.ws/Hazel/transfem-startpage/internal/cache" | ||||
| 	"gitea.elara.ws/Hazel/transfem-startpage/internal/rendering" | ||||
| 	"github.com/labstack/echo/v4" | ||||
| ) | ||||
| @@ -17,11 +18,6 @@ func Start(profile string) error { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	err = Config.Init() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	err = Config.FetchDiyHrt() | ||||
| 	if err != nil { | ||||
| 		log.Println(err) | ||||
| @@ -30,11 +26,19 @@ func Start(profile string) error { | ||||
| 	e := echo.New() | ||||
|  | ||||
| 	// statically serve the file | ||||
| 	cacheDir, err := rendering.GetCacheDir() | ||||
| 	if err == nil { | ||||
| 		e.Static("/cache", cacheDir) | ||||
| 	} else { | ||||
| 		log.Println("didn't enable cache dir", err) | ||||
| 	cache := cache.NewCache(profile) | ||||
| 	if !cache.Disabled { | ||||
| 		cache.StartStaticServer(e) | ||||
|  | ||||
| 		log.Println("downloading website icons...") | ||||
| 		for i, w := range Config.Template.Websites { | ||||
| 			u, err := cache.CacheUrl(w.ImageUrl) | ||||
| 			if err != nil { | ||||
| 				log.Println(err) | ||||
| 			} | ||||
| 			Config.Template.Websites[i].ImageUrl = u | ||||
| 			Config.Template.Websites[i].IsFetched = true | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// https://echo.labstack.com/docs/cookbook/embed-resources | ||||
|   | ||||
		Reference in New Issue
	
	Block a user