외부 static resource 서비스 추가
parent
08e522e745
commit
73199eab12
@ -0,0 +1,55 @@
|
|||||||
|
package cokr.xit.foundation.boot;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import cokr.xit.foundation.Assert;
|
||||||
|
import cokr.xit.foundation.data.StringMap;
|
||||||
|
|
||||||
|
@Configuration("staticResource")
|
||||||
|
public class StaticResourceConfig {
|
||||||
|
@Value("${spring.mvc.static-path-pattern}")
|
||||||
|
private String staticURLs;
|
||||||
|
@Value("${spring.web.resources.static-locations}")
|
||||||
|
private String staticLocations;
|
||||||
|
|
||||||
|
public Map<String, String> getMappings() {
|
||||||
|
if (Assert.isEmpty(staticURLs) && Assert.isEmpty(staticLocations))
|
||||||
|
return Collections.emptyMap();
|
||||||
|
|
||||||
|
List<String>
|
||||||
|
urls = Arrays.asList(staticURLs.split(",")),
|
||||||
|
locations = Arrays.asList(staticLocations.split(","));
|
||||||
|
if (urls.size() != locations.size())
|
||||||
|
throw new RuntimeException("URLs and locations do not match in size for static resources");
|
||||||
|
|
||||||
|
StringMap<String> resMap = new StringMap<>();
|
||||||
|
for (int i = 0; i < urls.size(); ++i) {
|
||||||
|
String url = urls.get(i),
|
||||||
|
location = locations.get(i);
|
||||||
|
resMap.put(url.trim(), location.trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
return resMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getURLs(Predicate<Map.Entry<String, String>> filter) {
|
||||||
|
Predicate<Map.Entry<String, String>> test = filter != null ? filter : entry -> true;
|
||||||
|
List<String> urls = getMappings().entrySet().stream()
|
||||||
|
.filter(test)
|
||||||
|
.map(entry -> entry.getKey())
|
||||||
|
.toList();
|
||||||
|
return urls.toArray(new String[urls.size()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getLocations() {
|
||||||
|
List<String> locations = getMappings().values().stream().toList();
|
||||||
|
return locations.toArray(new String[locations.size()]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue