package com.ozs.web.controller.upload;

import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.utils.MinioUtils;
import com.ozs.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value;
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 javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/system/upLoad")
public class UploadController {

    @Value("${minio.bucketName}")
    private String bucketName;

    @Resource
    MinioUtils minioUtils;


    @PostMapping("/upLoadImage")
    public AjaxResult upLoadImage(@RequestParam("file") MultipartFile image) throws Exception {
        if (image.isEmpty()) {
            return AjaxResult.error("不能上传空文件哦");
        }
        //图片保存路径
        //String fileUploadPath ="/"+userId+"/image";
        String uploadFile=null;
        if (image != null && !image.isEmpty()) {

            String imageName = image.getOriginalFilename();
            if (StringUtils.isNotBlank(imageName)) {
                String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
                String filename = date + image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."));
                String imgName = "img" + "/" + filename;
                minioUtils.minIoClientUpload(image.getInputStream(), imgName);
                uploadFile =  "/" + bucketName + "/" + imgName;

            }
        } else {
            return AjaxResult.error("上传功能出错");
        }
        return AjaxResult.success(uploadFile);
    }
}
