/**
 * Web MVC 配置。
 *
 * <p>用于配置静态资源映射，例如上传文件的访问路径。</p>
 */
package com.hertz.config;

import java.io.File;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

  @Value("${app.upload.root-path}")
  private String uploadRootPath;

  private final AppPathResolver pathResolver;

  public WebMvcConfig(AppPathResolver pathResolver) {
    this.pathResolver = pathResolver;
  }

  @Override
  public void addResourceHandlers(@NonNull ResourceHandlerRegistry registry) {
    String absoluteUploadRootPath = new File(pathResolver.resolve(uploadRootPath)).getAbsolutePath().replace("\\", "/");
    registry.addResourceHandler("/uploads/**")
        .addResourceLocations("file:" + absoluteUploadRootPath + "/");
  }
}
