package com.chushang.common.core.config;

import cn.hutool.core.io.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;

@Configuration
public class MultipartConfig {

    @Value("${spring.servlet.multipart.location:/tmp}")
    private String tmpLocation;

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 若没有该目录，则创建，此处使用了第三方插件的FileUtil，各位可以使用原生File实现该逻辑
        if (!FileUtil.exist(tmpLocation)) {
            FileUtil.mkdir(tmpLocation);
        }
        factory.setLocation(tmpLocation);
        return factory.createMultipartConfig();
    }
}