feat: 优化文件处理逻辑,增强文件名提取功能并支持显示文件名
This commit is contained in:
parent
8548651d80
commit
de6a8266f7
@ -40,13 +40,23 @@ const files = computed(() => {
|
||||
if (Array.isArray(v)) {
|
||||
return v
|
||||
.map((i) => {
|
||||
if (typeof i === 'string') return { url: normalizeFileUrl(i) };
|
||||
if (i && typeof i === 'object' && i.url) return { url: normalizeFileUrl(String(i.url)) };
|
||||
if (typeof i === 'string') return { url: normalizeFileUrl(i), name: getFileNameFromUrl(i) };
|
||||
if (i && typeof i === 'object' && i.url) {
|
||||
const url = normalizeFileUrl(String(i.url));
|
||||
return {
|
||||
...i,
|
||||
url,
|
||||
name: i.name || i.fileName || getFileNameFromUrl(url),
|
||||
};
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.filter(Boolean);
|
||||
}
|
||||
if (typeof v === 'string' && v) return [{ url: normalizeFileUrl(v) }];
|
||||
if (typeof v === 'string' && v) {
|
||||
const url = normalizeFileUrl(v);
|
||||
return [{ url, name: getFileNameFromUrl(url) }];
|
||||
}
|
||||
return [];
|
||||
});
|
||||
|
||||
@ -74,7 +84,21 @@ async function add() {
|
||||
}
|
||||
const url = await chooseAndUploadImage({ count: 1 });
|
||||
if (!url) return;
|
||||
emitValue([...files.value, { url }]);
|
||||
emitValue([...files.value, { url, name: getFileNameFromUrl(url) }]);
|
||||
}
|
||||
|
||||
function getFileNameFromUrl(url) {
|
||||
const cleanUrl = String(url || '').split('?')[0].split('#')[0];
|
||||
const rawName = cleanUrl.split('/').pop() || '';
|
||||
if (!rawName) return '';
|
||||
|
||||
let fileName = rawName;
|
||||
try {
|
||||
fileName = decodeURIComponent(rawName);
|
||||
} catch (error) {
|
||||
fileName = rawName;
|
||||
}
|
||||
return fileName.replace(/^\d{10,}[-_]/, '');
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
@ -405,19 +405,40 @@ const normalizeCategory = (item, categoryType) => {
|
||||
const normalizeFiles = (files) => {
|
||||
if (!Array.isArray(files)) return [];
|
||||
return files
|
||||
.map((item) => {
|
||||
if (typeof item === "string") return { type: "image", url: normalizeFileUrl(item) };
|
||||
.map((item, index) => {
|
||||
if (typeof item === "string") {
|
||||
const url = normalizeFileUrl(item);
|
||||
return { type: "image", url, name: getFileDisplayName({ url }, index) };
|
||||
}
|
||||
const url = item?.url || item?.URL || item?.download_url;
|
||||
if (!url) return null;
|
||||
return {
|
||||
type: item.type || "image",
|
||||
url: normalizeFileUrl(url),
|
||||
name: item.name || item.fileName || "",
|
||||
name: getFileDisplayName({ ...item, url }, index),
|
||||
};
|
||||
})
|
||||
.filter(Boolean);
|
||||
};
|
||||
|
||||
const getFileNameFromUrl = (url) => {
|
||||
const cleanUrl = String(url || "").split("?")[0].split("#")[0];
|
||||
const rawName = cleanUrl.split("/").pop() || "";
|
||||
if (!rawName) return "";
|
||||
|
||||
let fileName = rawName;
|
||||
try {
|
||||
fileName = decodeURIComponent(rawName);
|
||||
} catch (error) {
|
||||
fileName = rawName;
|
||||
}
|
||||
return fileName.replace(/^\d{10,}[-_]/, "");
|
||||
};
|
||||
|
||||
const getFileDisplayName = (file, index = 0) => {
|
||||
return file?.name || file?.fileName || file?.originalname || getFileNameFromUrl(file?.url || file?.URL || file?.download_url) || `图片${index + 1}`;
|
||||
};
|
||||
|
||||
const normalizePhrase = (item, phraseType) => ({
|
||||
id: item._id || item.id,
|
||||
cateId: item.cateId || item.categoryId,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user