package com.jzdsh.auto.app.controller.admin;

import com.jfinal.kit.LogKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.render.JsonRender;
import com.jfinal.upload.UploadFile;
import com.jzdsh.auto.app.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class CommonController extends BaseController {


    public void imageUpload() {
      renderText("aaaaa");
    }

    public void ueditor(){
        if ("config".equals(getPara("action"))) {
            render("/assets/ueditor/jsp/config.json");
            return;
        }
        /**
         * 对应 config.json 配置的 imageActionName: "uploadimage"
         */
        if ( ! "uploadimage".equals(getPara("action"))) {
            renderJson("state", "UploadController 只支持图片类型的文件上传");
            return ;
        }
        /**
         * uploadType 是通过如代码令 ueditor 在上传图片时通过问号挂参的方式传递过来的自定义参数
         * ue.ready(function() {
         *      ue.execCommand('serverparam', {
         *          'uploadType': 'project'
         *      });
         * });
         */
        String uploadType = getPara("uploadType");
        if (StrKit.isBlank(uploadType)) {
            renderJson("state", "上传类型参数缺失");
            return ;
        }


        UploadFile uploadFile = null;
        try {
            // "upfile" 来自 config.json 中的 imageFieldName 配置项
            uploadFile = getFile("upfile", "/img/temp", 200*1024);
            //Ret ret = srv.ueditorUpload(getLoginAccount(), uploadType, uploadFile);
            // renderJson(ret);
           // render(new JsonRender(ret).forIE());	// 防止 IE 下出现文件下载现象
        }
        catch(com.jfinal.upload.ExceededSizeException ex) {
            renderJson("state", "上传图片只允许 200K 大小");
        }
        catch(Exception e) {
            if (uploadFile != null) {
                uploadFile.getFile().delete();
            }

            renderJson("state", "上传图片出现未知异常，请告知管理员：" + e.getMessage());
            LogKit.error(e.getMessage(), e);
        }
    }
}
