package com.jeeplus.common.bos;

import com.jeeplus.modules.iim.entity.MailPage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/bos/")
public class BosController {
    @RequestMapping("upload")
    @ResponseBody
    public Map upload(HttpServletRequest request, MultipartFile file){
        Map map = new HashMap();
        String storeAs = request.getParameter("storeAs");
        try {
            String path = "/"+storeAs;
            BOSClientUtil bosClientUtil = new BOSClientUtil();
            InputStream inputStream = file.getInputStream();
            String upload = bosClientUtil.upload(path, inputStream);
            map.put("msg","上传成功");
            map.put("code","1");
            map.put("url",upload);
        }catch (Exception e){
            map.put("msg","上传失败");
            map.put("code","0");
        }
        return map;
    }

    @RequestMapping("uploadPic")
    @ResponseBody
    public Map uploadPic(MultipartFile uploadFile){
        Map map = new HashMap();
        try {
            String name = uploadFile.getOriginalFilename();
            String path = "/picture/"+System.currentTimeMillis()+"/"+name;
            BOSClientUtil bosClientUtil = new BOSClientUtil();
            InputStream inputStream = uploadFile.getInputStream();
            String upload = bosClientUtil.upload(path, inputStream);
            map.put("msg","上传成功");
            map.put("code","1");
            map.put("url",upload);
        }catch (Exception e){
            map.put("msg","上传失败");
            map.put("code","0");
        }
        return map;
    }

}
