新增: 电子相册全屏模式+开机启动+Win10兼容
This commit is contained in:
72
photo.go
72
photo.go
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -14,12 +13,15 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
photoMu sync.Mutex
|
||||
photoFiles []string
|
||||
photoIdx int
|
||||
photoDir string
|
||||
photoStop chan struct{}
|
||||
photoDone chan struct{}
|
||||
photoMu sync.Mutex
|
||||
photoFiles []string
|
||||
photoIdx int
|
||||
photoDir string
|
||||
photoStop chan struct{}
|
||||
photoDone chan struct{}
|
||||
photoCacheMu sync.Mutex
|
||||
photoCacheMap map[string]string
|
||||
photoCacheDir string
|
||||
)
|
||||
|
||||
func scanPhotoDir(dir string) []string {
|
||||
@@ -42,25 +44,31 @@ func scanPhotoDir(dir string) []string {
|
||||
return files
|
||||
}
|
||||
|
||||
func photoToDataURI(dir, name string) string {
|
||||
path := filepath.Join(dir, name)
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return ""
|
||||
func preCachePhotos(dir string, files []string) {
|
||||
cache := make(map[string]string, len(files))
|
||||
for _, name := range files {
|
||||
uri := imageToDataURI(filepath.Join(dir, name))
|
||||
if uri != "" {
|
||||
cache[name] = uri
|
||||
}
|
||||
}
|
||||
ext := strings.ToLower(filepath.Ext(name))
|
||||
mime := "image/jpeg"
|
||||
switch ext {
|
||||
case ".png":
|
||||
mime = "image/png"
|
||||
case ".gif":
|
||||
mime = "image/gif"
|
||||
case ".webp":
|
||||
mime = "image/webp"
|
||||
case ".bmp":
|
||||
mime = "image/bmp"
|
||||
photoCacheMu.Lock()
|
||||
photoCacheMap = cache
|
||||
photoCacheDir = dir
|
||||
photoCacheMu.Unlock()
|
||||
log.Printf("相册: 预缓存 %d/%d 张", len(cache), len(files))
|
||||
}
|
||||
|
||||
func getCachedPhotoURI(dir, name string) string {
|
||||
photoCacheMu.Lock()
|
||||
if photoCacheMap != nil && photoCacheDir == dir {
|
||||
if uri, ok := photoCacheMap[name]; ok {
|
||||
photoCacheMu.Unlock()
|
||||
return uri
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("data:%s;base64,%s", mime, base64.StdEncoding.EncodeToString(data))
|
||||
photoCacheMu.Unlock()
|
||||
return imageToDataURI(filepath.Join(dir, name))
|
||||
}
|
||||
|
||||
func pushCurrentPhoto(interval int) {
|
||||
@@ -78,7 +86,7 @@ func pushCurrentPhoto(interval int) {
|
||||
idx = 0
|
||||
}
|
||||
|
||||
src := photoToDataURI(dir, files[idx])
|
||||
src := getCachedPhotoURI(dir, files[idx])
|
||||
if src == "" {
|
||||
return
|
||||
}
|
||||
@@ -118,7 +126,13 @@ func startPhotoLoop() {
|
||||
photoDone = done
|
||||
photoMu.Unlock()
|
||||
|
||||
photoCacheMu.Lock()
|
||||
photoCacheMap = nil
|
||||
photoCacheDir = cfg.PhotoDir
|
||||
photoCacheMu.Unlock()
|
||||
|
||||
log.Printf("相册: 共 %d 张, 间隔 %ds", len(files), interval)
|
||||
go preCachePhotos(cfg.PhotoDir, files)
|
||||
pushCurrentPhoto(interval)
|
||||
|
||||
go func() {
|
||||
@@ -149,8 +163,16 @@ func stopPhotoLoop() {
|
||||
done := photoDone
|
||||
photoStop = nil
|
||||
photoDone = nil
|
||||
photoFiles = nil
|
||||
photoIdx = 0
|
||||
photoDir = ""
|
||||
photoMu.Unlock()
|
||||
|
||||
photoCacheMu.Lock()
|
||||
photoCacheMap = nil
|
||||
photoCacheDir = ""
|
||||
photoCacheMu.Unlock()
|
||||
|
||||
if stop != nil {
|
||||
close(stop)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user