修复: 走查批修22项缺陷(样式常量/配置原子写/壁纸下载校验/前端泄漏)
- GWL_EXSTYLE 常量多写一个F致样式写入错误偏移 - 配置读写加锁+临时文件原子替换;更新窗口Promise化修白屏 - Bing壁纸: 裁剪后索引同步/下载校验状态码/超2000裁剪/隐藏图切换 - 天气假兜底不再入缓存; 相册循环重入保护; 中文日志按rune截断 - 主题壁纸rAF泄漏清理钩子; 知识卡热区复位; 照片清空指令透传 - 设置窗双开竞态+销毁后可重建; 消息循环-1退出; 重启失败不退出
This commit is contained in:
1 parent
5342c5e489
commit
5e93e04581
21 files changed
+217
-58
No files matched your search
@@ -112,6 +112,26 @@ func bingKey(market, date string) string {
|
||||
return market + "_" + date
|
||||
}
|
||||
|
||||
// fetchBingImage 下载图片字节,校验 HTTP 状态与读取错误,失败返回 error
|
||||
func fetchBingImage(imgURL string) ([]byte, error) {
|
||||
resp, err := httpClient.Get(imgURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, imgURL)
|
||||
}
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(data) == 0 {
|
||||
return nil, fmt.Errorf("empty body: %s", imgURL)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// downloadBingImage downloads a single Bing image from the record's URLBase
|
||||
func downloadBingImage(r *BingRecord) error {
|
||||
imgURL := r.URLBase
|
||||
@@ -184,8 +204,12 @@ func fetchBingHistory() {
|
||||
log.Printf("Bing API 请求失败 (market=%s, idx=%d): %v", market, idx, err)
|
||||
break
|
||||
}
|
||||
data, _ := io.ReadAll(resp.Body)
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if err != nil {
|
||||
log.Printf("Bing API 响应读取失败 (market=%s, idx=%d): %v", market, idx, err)
|
||||
break
|
||||
}
|
||||
|
||||
var br bingResponse
|
||||
if json.Unmarshal(data, &br) != nil || len(br.Images) == 0 {
|
||||
@@ -212,15 +236,10 @@ func fetchBingHistory() {
|
||||
imgURL = "https://www.bing.com" + imgURL
|
||||
}
|
||||
|
||||
imgResp, err := httpClient.Get(imgURL)
|
||||
imgData, err := fetchBingImage(imgURL)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
imgData, _ := io.ReadAll(imgResp.Body)
|
||||
imgResp.Body.Close()
|
||||
if len(imgData) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("%s_%s.jpg", market, date)
|
||||
localPath := filepath.Join(bingDir(), filename)
|
||||
@@ -255,8 +274,12 @@ func fetchBingHistory() {
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
data, _ := io.ReadAll(resp.Body)
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if err != nil {
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
|
||||
var bit biturlResponse
|
||||
if json.Unmarshal(data, &bit) != nil || bit.StartDate == "" {
|
||||
@@ -276,18 +299,11 @@ func fetchBingHistory() {
|
||||
}
|
||||
|
||||
// Download from biturl URL
|
||||
imgURL := bit.URL
|
||||
imgResp, err := httpClient.Get(imgURL)
|
||||
imgData, err := fetchBingImage(bit.URL)
|
||||
if err != nil {
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
imgData, _ := io.ReadAll(imgResp.Body)
|
||||
imgResp.Body.Close()
|
||||
if len(imgData) == 0 {
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("%s_%s.jpg", market, date)
|
||||
localPath := filepath.Join(bingDir(), filename)
|
||||
@@ -302,7 +318,7 @@ func fetchBingHistory() {
|
||||
Market: market,
|
||||
Source: "bing_biturl",
|
||||
}
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
break // 该 market 补充成功,不再重试
|
||||
}
|
||||
}
|
||||
|
||||
@@ -329,23 +345,36 @@ func fetchBingHistory() {
|
||||
})
|
||||
|
||||
// ---- Phase 3: Enforce 2000 cap ----
|
||||
currentIdx := existing.CurrentIdx
|
||||
if currentIdx >= len(records) {
|
||||
currentIdx = len(records) - 1
|
||||
}
|
||||
if currentIdx < 0 {
|
||||
currentIdx = 0
|
||||
}
|
||||
if len(records) > maxBingRecords {
|
||||
// Records sorted oldest-first, so oldest are at the head
|
||||
toRemove := records[:len(records)-maxBingRecords]
|
||||
removed := len(records) - maxBingRecords
|
||||
records = records[len(records)-maxBingRecords:]
|
||||
for _, r := range toRemove {
|
||||
p := filepath.Join(bingDir(), r.Filename)
|
||||
os.Remove(p)
|
||||
}
|
||||
log.Printf("Bing 壁纸: 裁剪到 %d 张, 删除 %d 张旧图", maxBingRecords, len(toRemove))
|
||||
// 当前索引左移裁剪掉的数量并夹取到有效范围
|
||||
currentIdx -= removed
|
||||
if currentIdx >= len(records) {
|
||||
currentIdx = len(records) - 1
|
||||
}
|
||||
if currentIdx < 0 {
|
||||
currentIdx = 0
|
||||
}
|
||||
}
|
||||
|
||||
history := &BingHistory{
|
||||
Records: records,
|
||||
CurrentIdx: 0,
|
||||
}
|
||||
if existing.CurrentIdx < len(records) {
|
||||
history.CurrentIdx = existing.CurrentIdx
|
||||
CurrentIdx: currentIdx,
|
||||
}
|
||||
saveBingHistory(history)
|
||||
|
||||
@@ -589,6 +618,7 @@ func bingWallpaperLoop() {
|
||||
}
|
||||
// Move to next (newer) image, skip hidden
|
||||
attempts := 0
|
||||
allHidden := false
|
||||
for {
|
||||
h.CurrentIdx = (h.CurrentIdx + 1) % len(h.Records)
|
||||
if !h.Records[h.CurrentIdx].Hidden {
|
||||
@@ -596,9 +626,14 @@ func bingWallpaperLoop() {
|
||||
}
|
||||
attempts++
|
||||
if attempts >= len(h.Records) {
|
||||
allHidden = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if allHidden {
|
||||
bingMu.Unlock()
|
||||
continue
|
||||
}
|
||||
saveBingHistory(h)
|
||||
bingMu.Unlock()
|
||||
bingReloadImage()
|
||||
|
||||
Reference in new issue
Block a user