From b8e9ef2f91d7224d550b3e65358f1f0a8bdde7cc Mon Sep 17 00:00:00 2001 From: William Welling Date: Fri, 14 Jul 2023 14:17:04 -0500 Subject: [PATCH] Fix windows external resource handling --- .../middleware/config/WebMvcConfig.java | 24 +++++++++++++++---- src/main/resources/application.yml | 3 +-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/edu/tamu/scholars/middleware/config/WebMvcConfig.java b/src/main/java/edu/tamu/scholars/middleware/config/WebMvcConfig.java index 6243b4c6b..0bb7d8a66 100644 --- a/src/main/java/edu/tamu/scholars/middleware/config/WebMvcConfig.java +++ b/src/main/java/edu/tamu/scholars/middleware/config/WebMvcConfig.java @@ -1,10 +1,12 @@ package edu.tamu.scholars.middleware.config; +import java.io.File; import java.io.IOException; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.FileUrlResource; import org.springframework.core.io.Resource; import org.springframework.core.task.AsyncTaskExecutor; import org.springframework.lang.Nullable; @@ -34,8 +36,12 @@ public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { + String assetDir = middleware.getAssetsLocation().endsWith("/") + ? middleware.getAssetsLocation() + : String.format("%s/", middleware.getAssetsLocation()); + registry.addResourceHandler("/file/**") - .addResourceLocations(middleware.getAssetsLocation()) + .addResourceLocations(assetDir) .resourceChain(true) .addResolver(new PathResourceResolver() { @@ -44,14 +50,24 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) { protected Resource getResource(String resourcePath, Resource location) throws IOException { String[] path = resourcePath.split("/"); - resourcePath = String.format( - "a~n/%s/%s/%s", + resourcePath = String.format("a~n/%s/%s/%s", path[0].substring(1, 4), path[0].substring(4, path[0].length()), path[1] ); - return super.getResource(resourcePath, location); + location = super.getResource(resourcePath, location); + + if (location == null || !location.getFile().exists()) { + resourcePath = String.format( + "%s%s", + assetDir, + resourcePath + ); + location = new FileUrlResource(resourcePath); + } + + return location; } }); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 4ca10876f..a117d5fd1 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -2,8 +2,7 @@ ui: url: http://localhost:4200 middleware: - # must end with forward-slash - assets-location: classpath:/assets/ + assets-location: classpath:/assets load-defaults: true update-defaults: true allowed-origins: