package cn.pluss.platform.controller.common;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.util.Base64Util;
import cn.pluss.platform.util.OssOperatUtil;
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.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author DJH
 */
@RestController
@RequestMapping("/imageUpload")
public class ImageUploadController {

    @PostMapping("/base64")
    public Result<?> imageUpload(String base64) {
        final InputStream inputStream = Base64Util.base64ToInputStream(base64);
        final String url = OssOperatUtil.upload(inputStream, UUID.randomUUID().toString().replace("-", ""), "png");

        return ResultGenerator.genSuccessResult(url);
    }

    @PostMapping("/image")
    public Result<?> imageUpload(@RequestParam("file") MultipartFile file) {
        System.out.println("26sa1d521sa561s56");
        final String url;
        try {
            url = OssOperatUtil.upload(file.getInputStream(), UUID.randomUUID().toString().replace("-", ""), "png");
        } catch (IOException e) {
            throw new MsgException("文件读取异常");
        }

        return ResultGenerator.genSuccessResult(url);
    }
}
