package cn.iocoder.yudao.module.banner.controller.admin;

import cn.iocoder.yudao.module.infra.api.file.FileApi;
import com.alibaba.fastjson.JSON;
import com.baidu.ueditor.ActionEnter;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ueditor")
public class UeditorController {

    @Resource
    private FileApi fileApi;

    //配置ueditor后端上传接口的验证
    @RequestMapping("/config")
    public String config(HttpServletRequest request, HttpServletResponse response, String action, MultipartFile[] upfile) throws IOException {
        //获取配置文件json内容返回给前端
        if (action.equals("config")) {
            request.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", "text/html");
            //注意path不能含有中文路径,确认该path能指向config.json
//            String path = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "config";
            String path = "E:/Workspace/Work/lanan/lanan-system/dl-module-base/target/classes/config/ueditor/config";
            System.out.println("path = " + path);
            PrintWriter printWriter = response.getWriter();
            printWriter.write(new ActionEnter(request, path).exec());
            printWriter.flush();
            printWriter.close();
        }
        //上传图片
        else if (action.equals("uploadimage")) {
            Map<String, Object> result = new HashMap<String, Object>();
            for (MultipartFile multipartFile : upfile) {
                //调用系统写好的文件上传方法，并返回给前端上传后的文件路径，组件需要拼接回显
                String upload = fileApi.createFile(multipartFile.getBytes());
                System.out.println("upload = " + upload);
                System.out.println("upload.sub= " + upload.substring(upload.lastIndexOf("/")));
                result.put("title", upload.substring(upload.lastIndexOf("/")+1));
                result.put("original", multipartFile.getOriginalFilename());
                result.put("state", "SUCCESS");
                result.put("url", upload);
                String jStr = JSON.toJSONString(result);
                return jStr;
            }
            return null;
        }
        //上传视频
        else if(action.equals("uploadvideo")){
            Map<String, Object> result = new HashMap<String, Object>();
            for (MultipartFile multipartFile : upfile) {
                String upload = fileApi.createFile(multipartFile.getBytes());
                System.out.println("upload = " + upload);
                System.out.println("upload.sub = " + upload.substring(upload.lastIndexOf("/")));
                result.put("title", upload.substring(upload.lastIndexOf("/")+1));
                result.put("original", multipartFile.getOriginalFilename());
                result.put("state", "SUCCESS");
                result.put("url", upload);
                String jStr = JSON.toJSONString(result);
                return jStr;
            }
        }else{
            Map<String, Object> result = new HashMap<String, Object>();
            for (MultipartFile multipartFile : upfile) {
                String upload = fileApi.createFile(multipartFile.getBytes());
                System.out.println("upload = " + upload);
                System.out.println("upload.sub= " + upload.substring(upload.lastIndexOf("/")));
                result.put("title", upload.substring(upload.lastIndexOf("/")+1));
                result.put("original", multipartFile.getOriginalFilename());
                result.put("state", "SUCCESS");
                result.put("url", upload);
                String jStr = JSON.toJSONString(result);
                return jStr;
            }
        }
        return null;
    }

}
