1414import java .util .stream .Stream ;
1515
1616public class WebAppImpl implements WebApp {
17- private static final String IMAGE_ROOT_PATH = " images" ;
17+ private static final Path IMAGE_ROOT_PATH = Path . of ( "data" , " images") ;
1818
1919 private final Plugin plugin ;
2020
@@ -43,9 +43,10 @@ public String createImage(BufferedImage image, String path) throws IOException {
4343 Path webRoot = getWebRoot ().toAbsolutePath ();
4444 String separator = webRoot .getFileSystem ().getSeparator ();
4545
46- Path webDataRoot = webRoot .resolve ("data" );
47- Path imagePath = webDataRoot .resolve (Path .of (IMAGE_ROOT_PATH , path .replace ("/" , separator ) + ".png" )).toAbsolutePath ();
46+ Path imageRootFolder = webRoot .resolve (IMAGE_ROOT_PATH );
47+ Path imagePath = imageRootFolder .resolve (Path .of (path .replace ("/" , separator ) + ".png" )).toAbsolutePath ();
4848
49+ Files .createDirectories (imagePath .getParent ());
4950 Files .deleteIfExists (imagePath );
5051 Files .createFile (imagePath );
5152
@@ -64,24 +65,27 @@ public Map<String, String> availableImages() throws IOException {
6465
6566 Map <String , String > availableImagesMap = new HashMap <>();
6667
67- try (Stream <Path > fileStream = Files .walk (imageRootPath )){
68- fileStream
69- .filter (p -> !Files .isDirectory (p ))
70- .filter (p -> p .getFileName ().toString ().endsWith (".png" ))
71- .map (Path ::toAbsolutePath )
72- .forEach (p -> {
73- try {
74- String key = imageRootPath .relativize (p ).toString ();
75- key = key
76- .substring (0 , key .length () - 4 ) //remove .png
77- .replace (separator , "/" );
78-
79- String value = webRoot .relativize (p ).toString ()
80- .replace (separator , "/" );
81-
82- availableImagesMap .put (key , value );
83- } catch (IllegalArgumentException ignore ) {}
84- });
68+ if (Files .exists (imageRootPath )) {
69+ try (Stream <Path > fileStream = Files .walk (imageRootPath )) {
70+ fileStream
71+ .filter (p -> !Files .isDirectory (p ))
72+ .filter (p -> p .getFileName ().toString ().endsWith (".png" ))
73+ .map (Path ::toAbsolutePath )
74+ .forEach (p -> {
75+ try {
76+ String key = imageRootPath .relativize (p ).toString ();
77+ key = key
78+ .substring (0 , key .length () - 4 ) //remove .png
79+ .replace (separator , "/" );
80+
81+ String value = webRoot .relativize (p ).toString ()
82+ .replace (separator , "/" );
83+
84+ availableImagesMap .put (key , value );
85+ } catch (IllegalArgumentException ignore ) {
86+ }
87+ });
88+ }
8589 }
8690
8791 return availableImagesMap ;
0 commit comments