|
|
@ -19,98 +19,35 @@ module.exports = { |
|
|
|
entry: function (app, router, opts) { |
|
|
|
|
|
|
|
const getApiRoot = async function (ctx) { |
|
|
|
const { apiUrl, iotAuthWeb, iotVideoServer } = opts; |
|
|
|
const { apiUrl, } = opts; |
|
|
|
|
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = { |
|
|
|
root: apiUrl, |
|
|
|
iotAuthWeb, |
|
|
|
iotVideoServer |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
let download = async function (ctx, next) { |
|
|
|
const { fetchUrl } = opts.qiniu; |
|
|
|
if (ctx.path && ctx.path.includes(fetchUrl)) { |
|
|
|
try { |
|
|
|
const { filename } = ctx.request.query; |
|
|
|
const fkey = decodeURI(ctx.path.slice(fetchUrl.length + 1)).replace(/\.json$/, '.js'); |
|
|
|
const publicDownloadUrl = await app.fs.attachment.download(fkey); |
|
|
|
ctx.status = 200; |
|
|
|
if (filename) ctx.attachment(filename); |
|
|
|
ctx.body = request.get(publicDownloadUrl); |
|
|
|
} catch (err) { |
|
|
|
ctx.fs.logger.error(err); |
|
|
|
ctx.status = 404; |
|
|
|
ctx.body = { error: 'file not found.' } |
|
|
|
} |
|
|
|
} else { |
|
|
|
await next(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
let upload = async function (ctx, next) { |
|
|
|
try { |
|
|
|
const { files } = await parse(ctx.req); |
|
|
|
const file = files[0]; |
|
|
|
const extname = path.extname(file.filename).toLowerCase(); |
|
|
|
const fileType = ctx.query.type || "image"; |
|
|
|
const fileFolder = ctx.query.fileFolder || 'common'; |
|
|
|
if (ext[fileType].indexOf(extname) < 0) { |
|
|
|
ctx.status = 400; |
|
|
|
ctx.body = JSON.stringify({ name: 'UploadFailed', message: '文件格式无效' }); |
|
|
|
return; |
|
|
|
try { |
|
|
|
const { filename } = ctx.request.query; |
|
|
|
const fkey = decodeURI(ctx.path.slice(fetchUrl.length + 1)).replace(/\.json$/, '.js'); |
|
|
|
const publicDownloadUrl = await app.fs.attachment.download(fkey); |
|
|
|
ctx.status = 200; |
|
|
|
if (filename) ctx.attachment(filename); |
|
|
|
ctx.body = request.get(publicDownloadUrl); |
|
|
|
} catch (err) { |
|
|
|
ctx.fs.logger.error(err); |
|
|
|
ctx.status = 404; |
|
|
|
ctx.body = { error: 'file not found.' } |
|
|
|
} |
|
|
|
const date = new Date().toLocaleDateString(); |
|
|
|
const time = new Date().getTime(); |
|
|
|
let fileName = time + '_' + file.filename; |
|
|
|
let saveFile = path.join(__dirname, '../../', `/client/assets/files/${fileFolder}`, fileName); |
|
|
|
const pathUrl = `./client/assets/files/${fileFolder}`; |
|
|
|
|
|
|
|
const res1 = fs.existsSync(`./client/assets/files/${fileFolder}`); |
|
|
|
!res1 && fs.mkdirSync(`./client/assets/files/${fileFolder}`); |
|
|
|
const res = fs.existsSync(pathUrl); |
|
|
|
!res && fs.mkdirSync(pathUrl); |
|
|
|
let stream = fs.createWriteStream(saveFile); |
|
|
|
fs.createReadStream(file.path).pipe(stream); |
|
|
|
stream.on('error', function (err) { |
|
|
|
app.fs.logger.log('error', '[Upload Heatmap]', err); |
|
|
|
}); |
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = { filename: path.join(`/assets/files/${fileFolder}`, fileName), name: 'UploadSuccess', message: '上传成功' }; |
|
|
|
} catch (err) { |
|
|
|
ctx.status = 500; |
|
|
|
ctx.fs.logger.error(err); |
|
|
|
ctx.body = { err: 'upload error.' }; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
let remove = async function (ctx, next) { |
|
|
|
try { |
|
|
|
const fkeys = ctx.request.body; |
|
|
|
let removeUrl = path.join(__dirname, '../../', './client', fkeys.url); |
|
|
|
const res = fs.existsSync(removeUrl); |
|
|
|
if (!res) { |
|
|
|
ctx.status = 400; |
|
|
|
ctx.body = JSON.stringify({ name: 'DeleteFailed', message: '文件地址不存在' }); |
|
|
|
return; |
|
|
|
} |
|
|
|
fs.unlink(removeUrl, function (error) { |
|
|
|
if (error) { |
|
|
|
console.log(error); |
|
|
|
} |
|
|
|
}) |
|
|
|
ctx.status = 200; |
|
|
|
ctx.body = { name: 'DeleteSuccess.', message: '删除成功' }; |
|
|
|
} catch (err) { |
|
|
|
ctx.status = 500; |
|
|
|
ctx.fs.logger.error(err); |
|
|
|
ctx.body = { err: 'upload cleanup error.' }; |
|
|
|
} else { |
|
|
|
await next(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
router.use(download); |
|
|
|
router.get('/api/root', getApiRoot); |
|
|
|
router.post('/_upload/new', upload); |
|
|
|
router.delete('/_upload/cleanup', remove); |
|
|
|
} |
|
|
|
}; |
|
|
|