package com.jiuzhekan.cbkj.controller.testBusiness;


import com.jiuzhekan.cbkj.beans.sysBeans.ResEntity;
import com.jiuzhekan.cbkj.common.utils.Constant;
import com.jiuzhekan.cbkj.common.utils.DateUtil;
import com.jiuzhekan.cbkj.common.utils.OSSUtil;
import com.jiuzhekan.cbkj.service.testBusiness.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Api(value = "上传文件接口", tags = "上传文件接口")
@Controller
public class UploadController {

    @Value("${file.address}")
    private String location;

    @Value("${root.preview}")
    private String preview;

    @Value("${root.upload.relative}")
    private String relative;

    @Autowired
    private UploadService uploadService;

    private final ResourceLoader resourceLoader;

    @Autowired
    public UploadController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    /**
     * 上传到阿里云OSS
     *
     * @param file file
     */
    @RequestMapping(value = "upload/post2", method = RequestMethod.POST)
    @ResponseBody
    public Object fileUpload(MultipartFile file) {
        Object obj = null;
        try {
            obj = OSSUtil.uploadFile(file, "cbkj");

            return new ResEntity(true, Constant.SUCCESS_DX, obj);


        } catch (Exception e) {
            e.printStackTrace();
            return new ResEntity(false, "上传服务异常", e.getMessage());
        }
    }

    /**
     * 用一个tomcat 作为图片服务器 上传
     *
     * @param request request
     * @param file    file
     */
    @ApiOperation(value = "文件上传", notes = "文件上传")
    @RequestMapping(value = "upload/post", method = RequestMethod.POST)
    @ResponseBody
    public Object handleFileUpload(HttpServletRequest request, MultipartFile file) {

        return uploadService.uploadMultipartFile(file);
    }


    @RequestMapping(value = "rpcVideo/post", method = RequestMethod.POST)
    @ResponseBody
    public Object handleIMFileUpload(String data) throws IOException {

        if (StringUtils.isBlank(data)) {
            return ResEntity.entity(false, "not data", null);
        }
        Base64 base64 = new Base64();
        byte[] bs = base64.decode(data.substring("data:image/png;base64,".length()).getBytes());
        StringBuffer sb = new StringBuffer(relative).append(DateUtil.getDateFormats(DateUtil.date5, null)).append("/");
        sb.append(String.format("%s%s", UUID.randomUUID().toString(), ".jpg"));
        File dest = new File(String.format("%s%s", location, sb.toString()));
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            FileUtils.writeByteArrayToFile(dest, bs);
            return new ResEntity(true, Constant.SUCCESS_DX, preview + sb.toString());
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return new ResEntity(false, "服务异常IM", null);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResEntity(false, "服务异常IM", null);
        }
    }


}