-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (40 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const helper = require('think-helper');
const path = require('path');
const fs = require('fs');
const config = require('./config');
class ThinkUeditor {
constructor(ctx) {
this.ctx = ctx;
}
init() {
const param = this.ctx.param();
if (param.action === 'config') {
return config;
}
if (param.action === 'uploadimage' || param.action === 'uploadfile' || param.action === 'uploadvideo') {
const file = this.ctx.file('upfile');
const filepath = file.path;
const nameArr = file.name.split('.');
const basename = path.basename(filepath) + '.' + nameArr[nameArr.length - 1];
const YYYYMMDD = helper.datetime(Date.now(), 'YYYYMMDD');
const staticPath = path.resolve(think.ROOT_PATH, 'www/static');
const uploadPath = path.resolve(staticPath, 'upload');
const relativePath = path.resolve(uploadPath, YYYYMMDD);
// 文件夹不存在则创建
if (!fs.existsSync(uploadPath)) {
fs.mkdirSync(uploadPath);
}
if (!fs.existsSync(relativePath)) {
fs.mkdirSync(relativePath);
}
fs.renameSync(filepath, path.resolve(relativePath, `${basename}`));
return {
state: 'SUCCESS',
url: `/static/upload/${YYYYMMDD}/${basename}`,
title: basename,
original: file.name
};
}
}
}
module.exports = ThinkUeditor;