/**
 * ueditor相关路由
 *
 * @author CaiAoLin
 * @date 2017/9/25
 * @version
 */
import Express from "express";

const router = Express.Router();
const ueditor = require("ueditor");
const path = require('path');

module.exports = function (app) {
    const uploadPath = path.join(app.locals.rootDir, 'public');

    // action定义区域
    router.get('/ue', ueditor(uploadPath, function (req, res, next) {
        res.setHeader('Content-Type', 'application/json');
        res.redirect('/lib/ueditor/nodejs/config.json')
    }));

    router.post('/ue', ueditor(uploadPath, function (req, res, next) {
        // ueditor 客户发起上传图片请求
        if (req.query.action === 'uploadimage') {
            let imgUrl = '/public/images/ueditor/';
            res.ue_up(imgUrl); //你只要输入要保存的地址 。保存操作交给ueditor来做
        }
        //  客户端发起图片列表请求
        else if (req.query.action === 'listimage') {
            let dirUrl = '/images/ueditor/';
            res.ue_list(dirUrl);  // 客户端会列出 dir_url 目录下的所有图片
        }
    }));

    app.use("/ueditor", router);
};
