fix(serve): check cover write errors, clean tmp only on failure path (B11)
This commit is contained in:
@@ -2,6 +2,7 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@@ -60,14 +61,18 @@ func (h *H) ServeCover(c *gin.Context) {
|
|||||||
if e == nil {
|
if e == nil {
|
||||||
dst := filepath.Join(dir, "cover"+ext)
|
dst := filepath.Join(dir, "cover"+ext)
|
||||||
if e := os.MkdirAll(dir, 0o755); e == nil {
|
if e := os.MkdirAll(dir, 0o755); e == nil {
|
||||||
tmp := fmt.Sprintf("%s.tmp-%d", dst, time.Now().UnixNano()) // 并发幂等:唯一 tmp + rename 原子
|
tmp := fmt.Sprintf("%s.tmp-%d", dst, time.Now().UnixNano())
|
||||||
if e := os.WriteFile(tmp, img, 0o644); e == nil {
|
// B11: check all write errors; clean tmp only on failure.
|
||||||
if e := os.Rename(tmp, dst); e == nil {
|
if we := os.WriteFile(tmp, img, 0o644); we != nil {
|
||||||
|
log.Printf("serve: write cover tmp: %v", we)
|
||||||
|
os.Remove(tmp)
|
||||||
|
} else if re := os.Rename(tmp, dst); re != nil {
|
||||||
|
log.Printf("serve: rename cover: %v", re)
|
||||||
|
os.Remove(tmp)
|
||||||
|
} else {
|
||||||
http.ServeFile(c.Writer, c.Request, dst)
|
http.ServeFile(c.Writer, c.Request, dst)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
os.Remove(tmp)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user