package com.itheima.file.service.impl;

import com.heima.file.service.FileStorageService;
import com.itheima.file.service.Demo1Service;
import com.itheima.file.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

@Slf4j
@Service
public class WmMaterialServiceImpl implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;
    @Override
    public String uploadPicture(MultipartFile multipartFile) {
        // 1 检查参数
        if (multipartFile.isEmpty() || multipartFile.getSize() == 0) {
            return "上传失败";
        }
        // 2 上传图片到minio中
        String fileName = UUID.randomUUID().toString().replace("-", "");
        // 文件名
        String originalFilename = multipartFile.getOriginalFilename();
        // 后缀名
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = fileName + postfix;
        String fileId = "";
        try {
            fileId = fileStorageService.uploadImgFile("", newFileName, multipartFile.getInputStream());
            log.debug("上传图片到minio, fileId:{}", fileId);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl---上传图片到minio失败");
        }
        return "上传成功:" + fileId;
    }

    @Override
    public String getList() {
        return null;
    }
}
