2
0

fix(mediakit): improve unsplash image download reliability
All checks were successful
Build and Release / Create Release (push) Successful in 0s
Build and Release / Integration Tests (PostgreSQL) (push) Successful in 3m2s
Build and Release / Unit Tests (push) Successful in 4m54s
Build and Release / Lint (push) Successful in 5m19s
Build and Release / Build Binaries (amd64, linux, linux-latest) (push) Successful in 2m54s
Build and Release / Build Binaries (amd64, windows, windows-latest) (push) Successful in 8h4m46s
Build and Release / Build Binaries (amd64, darwin, macos) (push) Successful in 7m9s
Build and Release / Build Binaries (arm64, darwin, macos) (push) Successful in 7m46s
Build and Release / Build Binary (linux/arm64) (push) Successful in 8m11s

Increase max background image size from 2MB to 5MB to support higher quality images. Extend download timeout from 15s to 30s and use context-aware HTTP requests. Add detailed error logging for download failures. Improve client-side error messages by displaying server error details and exception messages.
This commit is contained in:
2026-01-31 00:56:57 -05:00
parent d3aa4508e7
commit 8452363218
2 changed files with 18 additions and 6 deletions

View File

@@ -24,7 +24,7 @@ import (
const tplMediaKit templates.TplName = "repo/settings/media_kit"
const maxBgImageSize = 2 * 1024 * 1024 // 2MB
const maxBgImageSize = 5 * 1024 * 1024 // 5MB
// MediaKit shows the media kit settings page.
func MediaKit(ctx *context.Context) {
@@ -276,21 +276,30 @@ func MediaKitUnsplashSelect(ctx *context.Context) {
}()
// Download the regular-size image
client := &http.Client{Timeout: 15 * time.Second}
resp, err := client.Get(regularURL)
client := &http.Client{Timeout: 30 * time.Second}
imgReq, err := http.NewRequestWithContext(ctx, http.MethodGet, regularURL, nil)
if err != nil {
ctx.ServerError("Download Unsplash image", err)
log.Error("Unsplash select: failed to create request for %s: %v", regularURL, err)
ctx.JSON(http.StatusBadGateway, map[string]string{"error": "Failed to download image"})
return
}
resp, err := client.Do(imgReq)
if err != nil {
log.Error("Unsplash select: failed to download %s: %v", regularURL, err)
ctx.JSON(http.StatusBadGateway, map[string]string{"error": "Failed to download image"})
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Error("Unsplash select: image download returned status %d for %s", resp.StatusCode, regularURL)
ctx.JSON(http.StatusBadGateway, map[string]string{"error": "Failed to download image"})
return
}
data, err := io.ReadAll(io.LimitReader(resp.Body, maxBgImageSize+1))
if err != nil {
log.Error("Unsplash select: failed to read image body: %v", err)
ctx.ServerError("ReadAll", err)
return
}

View File

@@ -217,11 +217,14 @@
statusEl.textContent = 'Image saved! Reloading…';
window.location.reload();
} else {
statusEl.textContent = 'Failed to save image. Please try again.';
let detail = resp.status + ' ' + resp.statusText;
try { const j = await resp.json(); if (j.error) detail = j.error; } catch (_) {}
statusEl.textContent = 'Failed to save image: ' + detail;
statusEl.style.color = 'var(--color-error)';
}
} catch (e) {
statusEl.textContent = 'Network error. Please try again.';
console.error('Unsplash select error:', e);
statusEl.textContent = 'Network error: ' + e.message;
statusEl.style.color = 'var(--color-error)';
} finally {
el.style.opacity = '1';