优化网盘图片读取逻辑

优化网盘图片读取逻辑
This commit is contained in:
truewhile
2026-08-21 22:21:14 +08:00
parent cbe0d7e88f
commit 0bcb1fec87
4 changed files with 10 additions and 29 deletions
+4 -18
View File
@@ -86,9 +86,8 @@ func (p *ImageProxy) serveRemoteImage(ctx context.Context, w http.ResponseWriter
if !forceRefresh && serveCachedImageFile(w, r, key, cachePath) {
return nil
}
if !forceRefresh && p.serveFreshRemoteFailure(w, failPath) {
return nil
}
// No negative caching: a previously failed fetch is retried on every
// subsequent request, so the image recovers as soon as upstream does.
data, ctype, contentLength, err := p.fetchAndCacheRemoteImage(ctx, raw, host, cachePath, failPath)
if err != nil {
if forceRefresh && serveCachedImageFile(w, r, key, cachePath) {
@@ -128,16 +127,6 @@ func (p *ImageProxy) removeUnusableImageCache(cachePath, failPath string) {
_ = os.Remove(failPath)
}
func (p *ImageProxy) serveFreshRemoteFailure(w http.ResponseWriter, failPath string) bool {
if stat, err := os.Stat(failPath); err == nil && time.Since(stat.ModTime()) < imageNegativeCacheTTL {
serveCachedPlaceholder(w)
return true
} else if err == nil {
_ = os.Remove(failPath)
}
return false
}
func (p *ImageProxy) fetchAndCacheRemoteImage(ctx context.Context, raw, host, cachePath, failPath string) ([]byte, string, string, error) {
if err := os.MkdirAll(p.cacheDir, 0o750); err != nil {
p.log.Warn("imageproxy: mkdir failed", zap.String("dir", p.cacheDir), zap.Error(err))
@@ -188,11 +177,8 @@ func (p *ImageProxy) Fetch(ctx context.Context, raw string) ([]byte, string, err
_ = os.Remove(cachePath)
_ = os.Remove(failPath)
}
if stat, err := os.Stat(failPath); err == nil && time.Since(stat.ModTime()) < imageNegativeCacheTTL {
return nil, "", errors.New("recent image fetch failure")
} else if err == nil {
_ = os.Remove(failPath)
}
// No negative caching: a previously failed fetch is retried on every
// subsequent request, so the image recovers as soon as upstream does.
data, ctype, _, err := p.fetchAndCacheRemoteImage(ctx, raw, host, cachePath, failPath)
return data, ctype, err
}