package cn.palmte.work.controller.backend;

import cn.palmte.work.bean.ResponseMsg;
import cn.palmte.work.config.Constant;
import cn.palmte.work.config.UploadProperties;
import cn.palmte.work.pojo.UploadResult;
import cn.palmte.work.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author pengqiang
 * @date 2018/4/21
 */
@Controller
@RequestMapping("/file")
public class FileController extends BaseController {

    @Autowired
    private UploadProperties uploadProperties;

    /**
     * 上传文件
     * @return 返回状态和下载地址
     */
    @PostMapping("/upload")
    @ResponseBody
    public ResponseMsg upload(@RequestParam(value = "files[]") MultipartFile[] multipartfiles) {
        ResponseMsg responseMsg = ResponseMsg.buildFailedMsg("failed");
        if (null != multipartfiles && multipartfiles.length > 0) {
            MultipartFile file = multipartfiles[0];
            UploadResult result = UploadUtil.upload(file,
                    uploadProperties.getPath() , uploadProperties.getPrefix());
            if (Constant.STATUS_SUCCESS == result.getStatus()) {
                responseMsg = ResponseMsg.buildSuccessMsg("成功", result);
            } else {
                responseMsg.setMsg(result.getMsg());
            }
        }
        return responseMsg;
    }

}
