package image import ( "crypto/md5" "fmt" "icloudapp.cn/tools/util" fileObj "icloudapp.cn/tools/util/file" "os" "strings" ) type Cache struct { Path string } func NewCache(path string) *Cache { return &Cache{Path: path} } // SetCache 设置缓存目录 func (c *Cache) SetCache(path string) { c.Path = path } // CacheFile Cache 获取缓存文件名 func (c *Cache) CacheFile(file string, format string) string { if !fileObj.IsDir(c.Path) { if err := os.Mkdir(c.Path, 0755); err != nil { panic(err.Error()) } } if file == "" { file = util.RandomStr(10) } destName := fmt.Sprintf("%x", md5.Sum([]byte(file))) destFile := fmt.Sprintf("%s/%s.%s", c.Path, destName, strings.ToLower(format)) return destFile }