package com.njcn.gather.system.config;

import cn.hutool.extra.spring.SpringUtil;
import com.njcn.common.bean.CustomCacheUtil;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;

/**
 * @author caozehui
 * @data 2025-03-24
 */
@Configuration
public class WebConfig {

    /**
     * 将自定缓存工具类注入到spring容器中
     *
     * @return
     */
    @Bean
    public CustomCacheUtil customCacheUtil() {
        CustomCacheUtil customCacheUtil = SpringUtil.getBean(CustomCacheUtil.CACHE_NAME);
        return customCacheUtil;
    }

    /**
     * 配置上传文件大小限制
     *
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 单个文件最大6MB
        factory.setMaxFileSize(DataSize.ofMegabytes(1024));
        // 整个请求最大12MB
        factory.setMaxRequestSize(DataSize.ofMegabytes(2048));
        return factory.createMultipartConfig();
    }

}
