perf: 上传文件名处理

dev/timezone
Lei OT 1 year ago
parent 109b960891
commit 5317d77a41

@ -437,3 +437,15 @@ export const olog = (text, ...args) => {
'background:#fb923c ; padding: 1px; border-radius: 3px; color: #fff',...args
);
};
export const sanitizeFilename = (str) => {
// Remove whitespace and replace with hyphens
str = str.replace(/\s+/g, '-');
// Remove invalid characters and replace with hyphens
str = str.replace(/[^a-zA-Z0-9.-]/g, '-');
// Replace consecutive hyphens with a single hyphen
str = str.replace(/-+/g, '-');
// Trim leading and trailing hyphens
str = str.replace(/^-+|-+$/g, '');
return str;
}

@ -4,7 +4,7 @@ import { FileAddOutlined } from '@ant-design/icons';
import { v4 as uuid } from 'uuid';
import { API_HOST } from '@/config';
import { whatsappSupportFileTypes } from '@/channel/whatsappUtils';
import { isEmpty, } from '@/utils/commons';
import { isEmpty, sanitizeFilename } from '@/utils/commons';
// import useConversationStore from '@/stores/ConversationStore';
const aliOSSHost = `https://haina-sale-system.oss-cn-shenzhen.aliyuncs.com/WAMedia/`;
@ -33,7 +33,7 @@ const ImageUpload = ({ disabled, invokeUploadFileMessage, invokeSendUploadMessag
const lastDotIndex = file.name.lastIndexOf('.');
const suffix = file.name.slice(lastDotIndex+1);
const baseName = file.name.slice(0, lastDotIndex);
const rename = baseName + '-' + Date.now() + '.' + suffix;
const rename = sanitizeFilename(baseName) + '-' + Date.now() + '.' + suffix;
const dataUri = aliOSSHost + rename;
//
reader.onload = (event) => {

Loading…
Cancel
Save