12345678910111213141516171819202122232425262728 |
- /**
- * 生成随机文件名称
- * 规则八位随机字符,加下划线连接时间戳
- */
- export const buildDir = () => {
- // 以日期作为目录
- const date = new Date()
- const dir = date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate()
- function rx() {
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
- }
- return `${dir}/${date.getTime()}-${rx()}${rx()}`
- }
- /**
- * 获取文件名并构建新的文件名称
- * 必须包含后缀
- */
- export const buildName = (name) => {
- const index = name.lastIndexOf('.') + 1
- const format = name.substring(
- index,
- name.length
- )
- return buildDir() + '.' + format
- }
|