fix: normalize upload objects prefix rustfs/rustfs#2768

This commit is contained in:
马登山
2026-05-06 08:26:55 +08:00
parent ed447f5bb0
commit 88eddfae3e
4 changed files with 42 additions and 2 deletions
+3 -2
View File
@@ -8,6 +8,7 @@ import { Progress } from "@/components/ui/progress"
import { useAddUploadFiles, useTaskPanelOpen } from "@/contexts/task-context"
import { formatBytes } from "@/lib/functions"
import { useMessage } from "@/lib/feedback/message"
import { buildUploadObjectKey, normalizeUploadPrefix } from "@/lib/object-upload"
import { RiDeleteBinLine, RiFileAddLine, RiFolderAddLine, RiUploadCloudLine } from "@remixicon/react"
import { useVirtualizer } from "@tanstack/react-virtual"
import * as React from "react"
@@ -228,7 +229,7 @@ export function ObjectUploadPicker({
}
}, [canUpload, onShowChange, show])
const effectivePrefix = editablePrefix.replace(/\/$/, "") || ""
const effectivePrefix = normalizeUploadPrefix(editablePrefix)
const ensureCapacity = React.useCallback(
(incoming: number): boolean => {
@@ -416,7 +417,7 @@ export function ObjectUploadPicker({
try {
const tasks = items.map(({ relativePath, file }) => ({
file,
key: effectivePrefix ? `${effectivePrefix}/${relativePath}` : relativePath,
key: buildUploadObjectKey(effectivePrefix, relativePath),
}))
const batchSize = 50
let processed = 0
+9
View File
@@ -0,0 +1,9 @@
export function normalizeUploadPrefix(prefix) {
return prefix.trim().replace(/^\/+/, "").replace(/\/+$/, "")
}
export function buildUploadObjectKey(prefix, relativePath) {
const normalizedPrefix = normalizeUploadPrefix(prefix)
const normalizedRelativePath = relativePath.replace(/^\/+/, "")
return normalizedPrefix ? `${normalizedPrefix}/${normalizedRelativePath}` : normalizedRelativePath
}
+9
View File
@@ -0,0 +1,9 @@
export function normalizeUploadPrefix(prefix: string): string {
return prefix.trim().replace(/^\/+/, "").replace(/\/+$/, "")
}
export function buildUploadObjectKey(prefix: string, relativePath: string): string {
const normalizedPrefix = normalizeUploadPrefix(prefix)
const normalizedRelativePath = relativePath.replace(/^\/+/, "")
return normalizedPrefix ? `${normalizedPrefix}/${normalizedRelativePath}` : normalizedRelativePath
}
+21
View File
@@ -0,0 +1,21 @@
import test from "node:test"
import assert from "node:assert/strict"
import { buildUploadObjectKey, normalizeUploadPrefix } from "../../lib/object-upload.js"
test("normalizeUploadPrefix removes leading and trailing slashes", () => {
assert.equal(normalizeUploadPrefix("/Normal/"), "Normal")
assert.equal(normalizeUploadPrefix("//Normal//"), "Normal")
})
test("buildUploadObjectKey avoids leading slash when prefix starts with slash", () => {
assert.equal(buildUploadObjectKey("/Normal/", "file.txt"), "Normal/file.txt")
})
test("buildUploadObjectKey keeps nested relative paths under the normalized prefix", () => {
assert.equal(buildUploadObjectKey("Normal/", "folder/file.txt"), "Normal/folder/file.txt")
})
test("buildUploadObjectKey uses relative path when prefix is empty or slash only", () => {
assert.equal(buildUploadObjectKey("", "file.txt"), "file.txt")
assert.equal(buildUploadObjectKey("/", "file.txt"), "file.txt")
})