Files
api-service/app/util/tool.js
T
2020-11-25 16:20:11 +08:00

47 lines
953 B
JavaScript

'use strict';
// 随机长度字符串
const randomString = len => {
len = len || 10;
const chars = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const maxPos = chars.length;
let str = '';
for (let i = 0; i < len; i++) {
str += chars.charAt(Math.floor(Math.random() * maxPos));
}
return str;
};
const getSuffix = fileName => {
const arr = fileName.split('.');
let suffix = '';
if (arr.length >= 2) {
suffix = arr[arr.length - 1];
}
if (suffix) suffix = `.${suffix}`;
return suffix;
};
const getFilePath = type => {
const now = new Date();
const fullYears = now.getFullYear();
const Month = now.getMonth();
let typeStr = '';
switch (type) {
case 'blog':
typeStr = 'blog';
break;
default:
typeStr = 'media';
break;
}
const path = `${typeStr}/${fullYears}/${Month}/`;
return path;
};
module.exports = {
randomString,
getSuffix,
getFilePath,
};