Skip to content

Commit 2f013cf

Browse files
committed
assets(String, String) check first on disk or fallback to classpath
1 parent 92190a9 commit 2f013cf

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

jooby/src/main/java/io/jooby/Router.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99

1010
import javax.annotation.Nonnull;
1111
import javax.inject.Provider;
12+
import java.nio.file.Files;
1213
import java.nio.file.Path;
14+
import java.nio.file.Paths;
1315
import java.util.ArrayList;
1416
import java.util.Collections;
1517
import java.util.List;
1618
import java.util.Map;
1719
import java.util.concurrent.Executor;
1820
import java.util.function.Predicate;
21+
import java.util.stream.Stream;
1922

2023
import static java.util.Arrays.asList;
2124
import static java.util.Collections.unmodifiableList;
@@ -454,13 +457,21 @@ interface Match {
454457
}
455458

456459
/**
457-
* Add a static resource handler. Static resources are resolved from classpath.
460+
* Add a static resource handler. Static resources are resolved from:
461+
*
462+
* - file-system if the source folder exists in the current user directory
463+
* - or fallback to classpath when file-system folder doesn't exist.
458464
*
459465
* @param pattern Path pattern.
460-
* @param source Classpath folder.
466+
* @param source File-System folder when exists, or fallback to a classpath folder.
461467
* @return A route.
462468
*/
463469
default @Nonnull Route assets(@Nonnull String pattern, @Nonnull String source) {
470+
Path path = Stream.of(source.split("/"))
471+
.reduce(Paths.get(System.getProperty("user.dir")), Path::resolve, Path::resolve);
472+
if (Files.exists(path)) {
473+
return assets(pattern, path);
474+
}
464475
return assets(pattern, AssetSource.create(getClass().getClassLoader(), source));
465476
}
466477

@@ -471,7 +482,7 @@ interface Match {
471482
* @return A route.
472483
*/
473484
default @Nonnull Route assets(@Nonnull String pattern) {
474-
return assets(pattern, "/");
485+
return assets(pattern, AssetSource.create(getClass().getClassLoader(), "/"));
475486
}
476487

477488
/**

0 commit comments

Comments
 (0)