Skip to content

Commit

Permalink
Fix windows external resource handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wwelling committed Jul 14, 2023
1 parent 0a41e50 commit b8e9ef2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {

Expand All @@ -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;
}

});
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b8e9ef2

Please sign in to comment.