Add a cache for gallery thumbnails (#496)

This commit is contained in:
bnkai
2020-05-11 10:20:08 +03:00
committed by GitHub
parent 8ba76783b0
commit bd45daacf3
36 changed files with 319 additions and 1200 deletions

View File

@@ -1,10 +1,12 @@
package utils
import (
"archive/zip"
"fmt"
"github.com/h2non/filetype"
"github.com/h2non/filetype/types"
"io/ioutil"
"math"
"os"
"os/user"
"path/filepath"
@@ -66,7 +68,12 @@ func EnsureDir(path string) error {
return err
}
// RemoveDir removes the given file path along with all of its contents
// EnsureDirAll will create a directory at the given path along with any necessary parents if they don't already exist
func EnsureDirAll(path string) error {
return os.MkdirAll(path, 0755)
}
// RemoveDir removes the given dir (if it exists) along with all of its contents
func RemoveDir(path string) error {
return os.RemoveAll(path)
}
@@ -125,6 +132,75 @@ func GetHomeDirectory() string {
return currentUser.HomeDir
}
// IsZipFileUnmcompressed returns true if zip file in path is using 0 compression level
func IsZipFileUncompressed(path string) (bool, error) {
r, err := zip.OpenReader(path)
if err != nil {
fmt.Printf("Error reading zip file %s: %s\n", path, err)
return false, err
} else {
defer r.Close()
for _, f := range r.File {
if f.FileInfo().IsDir() { // skip dirs, they always get store level compression
continue
}
return f.Method == 0, nil // check compression level of first actual file
}
}
return false, nil
}
// humanize code taken from https://github.com/dustin/go-humanize and adjusted
func logn(n, b float64) float64 {
return math.Log(n) / math.Log(b)
}
// HumanizeBytes returns a human readable bytes string of a uint
func HumanizeBytes(s uint64) string {
sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
if s < 10 {
return fmt.Sprintf("%d B", s)
}
e := math.Floor(logn(float64(s), 1024))
suffix := sizes[int(e)]
val := math.Floor(float64(s)/math.Pow(1024, e)*10+0.5) / 10
f := "%.0f %s"
if val < 10 {
f = "%.1f %s"
}
return fmt.Sprintf(f, val, suffix)
}
// WriteFile writes file to path creating parent directories if needed
func WriteFile(path string, file []byte) error {
pathErr := EnsureDirAll(filepath.Dir(path))
if pathErr != nil {
return fmt.Errorf("Cannot ensure path %s", pathErr)
}
err := ioutil.WriteFile(path, file, 0755)
if err != nil {
return fmt.Errorf("Write error for thumbnail %s: %s ", path, err)
}
return nil
}
// GetIntraDir returns a string that can be added to filepath.Join to implement directory depth, "" on error
//eg given a pattern of 0af63ce3c99162e9df23a997f62621c5 and a depth of 2 length of 3
//returns 0af/63c or 0af\63c ( dependin on os) that can be later used like this filepath.Join(directory, intradir, basename)
func GetIntraDir(pattern string, depth, length int) string {
if depth < 1 || length < 1 || (depth*length > len(pattern)) {
return ""
}
intraDir := pattern[0:length] // depth 1 , get length number of characters from pattern
for i := 1; i < depth; i++ { // for every extra depth: move to the right of the pattern length positions, get length number of chars
intraDir = filepath.Join(intraDir, pattern[length*i:length*(i+1)]) // adding each time to intradir the extra characters with a filepath join
}
return intraDir
}
func GetDir(path string) string {
if path == "" {
path = GetHomeDirectory()