All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rexsheng.springboot.faster.system.notice.adapter.NoticeFileConfig Maven / Gradle / Ivy

The newest version!
package com.github.rexsheng.springboot.faster.system.notice.adapter;

import com.github.rexsheng.springboot.faster.system.modular.AppModuleProperties;
import com.github.rexsheng.springboot.faster.system.modular.SpringModule;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Map;

//@SpringModule
//@ConditionalOnProperty(prefix = "app.module.file",name = "enabled",havingValue = "true",matchIfMissing = false)
public class NoticeFileConfig implements WebMvcConfigurer {

    private static Logger logger= LoggerFactory.getLogger(NoticeFileConfig.class);

    @Resource
    private AppModuleProperties appModuleProperties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if(appModuleProperties.getFile()!=null && appModuleProperties.getFile().getLocal()!=null && appModuleProperties.getFile().getLocal().getPathMapping()!=null){
            for (Map.Entry entry : appModuleProperties.getFile().getLocal().getPathMapping().entrySet()){
                registry.addResourceHandler(appendSuffix(appendPrefix(entry.getKey(),"/"),"/")+"**")
                        .addResourceLocations("file:" + appendSuffix(entry.getValue(),"/"));
            }
        }
//        logger.info("虚拟路径: {}, 对应本地目录:{}", host, path);
//        File file=new File(path);
//        if(!file.exists()){
//            file.mkdirs();
//            logger.info("本地已创建目录:{}", path);
//        }
//        registry.addResourceHandler((host.endsWith("/")?host:(host+"/"))+"**").addResourceLocations("file:///" + path);
    }

    private String appendPrefix(String str,String prefix){
        if(!str.startsWith(prefix)){
            return prefix+str;
        }
        return str;
    }

    private String appendSuffix(String str,String suffix){
        if(!str.endsWith(suffix)){
            return str+suffix;
        }
        return str;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy