fix(admin): respect urlPrefix for root redirect and JS API calls (#8975)

* fix(admin): respect urlPrefix for root redirect and JS API calls (#8967)

Two issues when running admin UI behind a reverse proxy with -urlPrefix:

1. Visiting the prefix path without trailing slash (e.g. /s3-admin) caused
   a redirect to / instead of /s3-admin/ because http.StripPrefix produced
   an empty path that the router redirected to root.

2. Several JavaScript API calls in admin.js used hardcoded paths instead
   of basePath(), causing file upload, download, and preview to fail.

* fix(admin): preserve query params in prefix redirect and use 302

Use http.StatusFound instead of 301 to avoid aggressive browser caching
of a configuration-dependent redirect, and preserve query parameters.
This commit is contained in:
Chris Lu
2026-04-07 14:12:05 -07:00
committed by GitHub
parent 2919bb27e5
commit b0e79ad207
2 changed files with 17 additions and 5 deletions
+4 -4
View File
@@ -1267,7 +1267,7 @@ async function submitUploadFile() {
});
// Send request
xhr.open('POST', '/api/files/upload');
xhr.open('POST', basePath('/api/files/upload'));
xhr.send(formData);
} catch (error) {
@@ -1320,7 +1320,7 @@ function exportFileList() {
// Download file
function downloadFile(filePath) {
// Create download link using admin API
const downloadUrl = `/api/files/download?path=${encodeURIComponent(filePath)}`;
const downloadUrl = basePath(`/api/files/download?path=${encodeURIComponent(filePath)}`);
window.open(downloadUrl, '_blank');
}
@@ -1786,7 +1786,7 @@ function createFileViewerContent(file, content) {
if (file.mime.startsWith('image/')) {
return `
<div class="text-center">
<img src="/api/files/download?path=${encodeURIComponent(file.full_path)}"
<img src="${basePath('/api/files/download?path=' + encodeURIComponent(file.full_path))}"
class="img-fluid" alt="${file.name}" style="max-height: 500px;">
</div>
`;
@@ -1804,7 +1804,7 @@ function createFileViewerContent(file, content) {
} else if (file.mime === 'application/pdf') {
return `
<div class="text-center">
<embed src="/api/files/download?path=${encodeURIComponent(file.full_path)}"
<embed src="${basePath('/api/files/download?path=' + encodeURIComponent(file.full_path))}"
type="application/pdf" width="100%" height="500px">
</div>
`;
+13 -1
View File
@@ -392,7 +392,19 @@ func startAdminServer(ctx context.Context, options AdminOptions, enableUI bool,
addr := fmt.Sprintf(":%d", *options.port)
var handler http.Handler = r
if urlPrefix != "" {
handler = http.StripPrefix(urlPrefix, r)
stripped := http.StripPrefix(urlPrefix, r)
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// Redirect /prefix (no trailing slash) to /prefix/
if req.URL.Path == urlPrefix {
target := urlPrefix + "/"
if req.URL.RawQuery != "" {
target += "?" + req.URL.RawQuery
}
http.Redirect(w, req, target, http.StatusFound)
return
}
stripped.ServeHTTP(w, req)
})
}
server := &http.Server{
Addr: addr,