package com.persagy.dmp.file.config;

import com.persagy.dmp.file.handler.OldFileAuthHandler;
import com.persagy.dmp.file.handler.OldFileContextHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Order(100)
@ConditionalOnProperty(value = "persagy.common.file.storage", havingValue = "2")
public class FileWebConfigurer implements WebMvcConfigurer {
    /***
     * Description: 旧文件的上下文处理器
     * @return : com.persagy.dmp.file.handler.OldFileContextHandler
     * @author : lijie
     * @date :2021/11/19 10:12
     * Update By lijie 2021/11/19 10:12
     */
    @Bean

    public OldFileContextHandler oldFileContextHandler() {
        return new OldFileContextHandler();
    }
    /***
     * Description: 旧文件的校验处理器
     * @return : com.persagy.dmp.file.handler.OldFileAuthHandler
     * @author : lijie
     * @date :2021/11/19 10:12
     * Update By lijie 2021/11/19 10:12
     */
    @Bean
    public OldFileAuthHandler oldFileAuthHandler() {
        return new OldFileAuthHandler();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] includePaths = {"/range/**","/common/**"};
        String[] excludePaths = {"/**/*.html", "/**/*.js", "/**/*.css", "/swagger-resources"};
        // 设置拦截的路径、不拦截的路径、优先级等等
        registry.addInterceptor(oldFileContextHandler())
                .order(100)
                .addPathPatterns(includePaths)
                .excludePathPatterns(excludePaths);
        String[] authIncludePaths = {"/common/file_upload",
                "/range/file_upload",
                "/range/file_delete",
                "/common/files_delete",
                "/common/images_delete",
                "/common/files_list",
                "/common/images_list",
                "/common/image_key_change",
                "/common/image_upload"};
        registry.addInterceptor(oldFileAuthHandler())
                .order(101)
                .addPathPatterns(authIncludePaths)
                .excludePathPatterns(excludePaths);
    }


}
