Files
grpc_picture/util/file.js

38 lines
881 B
JavaScript
Raw Normal View History

2026-01-09 11:21:29 +08:00
const fs = require('fs')
const getFileBuffer = (path) => fs.readFileSync(path)
const getFilePath = (prefixStr = '', postfixStr = '') => {
// 文件名称
const uuid = () => {
function s4 () {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1)
}
return s4() + s4() + s4() + s4() + s4() + s4() + s4() + s4()
}
// 文件扩展名 toLowerCase 转小写
let prefix = prefixStr
if (prefixStr && prefix.substr(prefix.length - 1, 1) !== '/') { prefix += '/' }
// 后缀
let postfix = ''
if (postfixStr) { postfix = '_' + postfixStr }
return prefix + uuid() + postfix + '.jpg'
}
const uuid = () => {
function s4 () {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1)
}
return s4() + s4() + s4()
}
module.exports = {
getFileBuffer,
getFilePath,
uuid
}