mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-06 05:16:38 +08:00
完善应用功能与界面交互
This commit is contained in:
@@ -55,26 +55,32 @@ func (p *ImageProxy) RemoveFailed(raw string) error {
|
||||
// Serve writes the requested image to w. Caller is expected to validate
|
||||
// the JWT before invoking it.
|
||||
func (p *ImageProxy) Serve(ctx context.Context, w http.ResponseWriter, r *http.Request, raw string) error {
|
||||
// Emby 客户端用 maxWidth / maxHeight / quality 请求缩略图。不解析这些
|
||||
// 参数就会把多兆字节的原图发给客户端,移动端往往在下载中途超时。
|
||||
opts := parseImageResizeOptions(r)
|
||||
if isLocalImagePath(raw) {
|
||||
return p.serveLocalImage(w, r, raw)
|
||||
return p.serveLocalImage(w, r, raw, opts)
|
||||
}
|
||||
return p.serveRemoteImage(ctx, w, r, raw)
|
||||
return p.serveRemoteImage(ctx, w, r, raw, opts)
|
||||
}
|
||||
|
||||
func (p *ImageProxy) serveLocalImage(w http.ResponseWriter, r *http.Request, raw string) error {
|
||||
func (p *ImageProxy) serveLocalImage(w http.ResponseWriter, r *http.Request, raw string, opts imageResizeOptions) error {
|
||||
path := filepath.Clean(raw)
|
||||
abs, err := filepath.Abs(path)
|
||||
if err != nil || !p.isAllowedLocalPath(abs) {
|
||||
servePlaceholder(w)
|
||||
return nil
|
||||
}
|
||||
if opts.active() && p.serveResizedFromFile(w, r, abs, opts) {
|
||||
return nil
|
||||
}
|
||||
if !serveImageFile(w, r, filepath.Base(abs), abs, imageBrowserCacheControl) {
|
||||
servePlaceholder(w)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ImageProxy) serveRemoteImage(ctx context.Context, w http.ResponseWriter, r *http.Request, raw string) error {
|
||||
func (p *ImageProxy) serveRemoteImage(ctx context.Context, w http.ResponseWriter, r *http.Request, raw string, opts imageResizeOptions) error {
|
||||
u, err := p.validateURL(raw)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -83,14 +89,14 @@ func (p *ImageProxy) serveRemoteImage(ctx context.Context, w http.ResponseWriter
|
||||
key, cachePath, failPath := p.remoteImageCachePathsForValidated(raw)
|
||||
forceRefresh := r.URL.Query().Get("refresh") != ""
|
||||
p.removeUnusableImageCache(cachePath, failPath)
|
||||
if !forceRefresh && serveCachedImageFile(w, r, key, cachePath) {
|
||||
if !forceRefresh && p.serveCachedImage(w, r, key, cachePath, opts) {
|
||||
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) {
|
||||
if forceRefresh && p.serveCachedImage(w, r, key, cachePath, opts) {
|
||||
return nil
|
||||
}
|
||||
if errors.Is(err, errImageProxyRequestSetup) {
|
||||
@@ -100,6 +106,10 @@ func (p *ImageProxy) serveRemoteImage(ctx context.Context, w http.ResponseWriter
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// 上游原图已落盘,缩放结果复用同一条缓存流水线。
|
||||
if opts.active() && p.serveResizedFromFile(w, r, cachePath, opts) {
|
||||
return nil
|
||||
}
|
||||
w.Header().Set("Content-Type", ctype)
|
||||
if contentLength != "" {
|
||||
w.Header().Set("Content-Length", contentLength)
|
||||
@@ -114,6 +124,15 @@ func (p *ImageProxy) serveRemoteImage(ctx context.Context, w http.ResponseWriter
|
||||
return nil
|
||||
}
|
||||
|
||||
// serveCachedImage 在远程原图已缓存时提供服务。请求带缩放参数时优先命中
|
||||
// 缩放缓存,未命中则从已缓存的原图生成一份;缩放不可用时退回原图直出。
|
||||
func (p *ImageProxy) serveCachedImage(w http.ResponseWriter, r *http.Request, key, cachePath string, opts imageResizeOptions) bool {
|
||||
if opts.active() && p.serveResizedFromFile(w, r, cachePath, opts) {
|
||||
return true
|
||||
}
|
||||
return serveCachedImageFile(w, r, key, cachePath)
|
||||
}
|
||||
|
||||
func (p *ImageProxy) removeUnusableImageCache(cachePath, failPath string) {
|
||||
data, err := os.ReadFile(cachePath) // #nosec G304 -- cachePath is SHA-derived under cacheDir.
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user