diff --git a/server/src/main/java/org/eclipse/openvsx/storage/LocalStorageService.java b/server/src/main/java/org/eclipse/openvsx/storage/LocalStorageService.java index 62c8860a0..f9c81bc70 100644 --- a/server/src/main/java/org/eclipse/openvsx/storage/LocalStorageService.java +++ b/server/src/main/java/org/eclipse/openvsx/storage/LocalStorageService.java @@ -9,6 +9,16 @@ * ****************************************************************************** */ package org.eclipse.openvsx.storage; +import java.io.IOException; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.ArrayList; +import java.util.List; + +import jakarta.annotation.PostConstruct; + import org.apache.commons.lang3.StringUtils; import org.eclipse.openvsx.entities.FileResource; import org.eclipse.openvsx.entities.Namespace; @@ -22,14 +32,6 @@ import org.springframework.web.server.ServerErrorException; import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody; -import java.io.IOException; -import java.net.URI; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.util.ArrayList; -import java.util.List; - @Component public class LocalStorageService implements IStorageService { @@ -40,6 +42,27 @@ public class LocalStorageService implements IStorageService { public boolean isEnabled() { return !StringUtils.isEmpty(storageDirectory); } + + @PostConstruct + public void validateStorageDirectory() throws IOException { + if (!isEnabled()) { + return; + } + + var storageDirectoryPath = Path.of(storageDirectory).toAbsolutePath(); + + if (!Files.exists(storageDirectoryPath)) { + Files.createDirectories(storageDirectoryPath); + } + + if (!Files.isDirectory(storageDirectoryPath)) { + throw new IllegalStateException("Local storage directory '" + storageDirectory + "' is not a directory"); + } + + if (!Files.isWritable(storageDirectoryPath)) { + throw new IllegalStateException("Local storage directory '" + storageDirectory + "' is not writable"); + } + } @Override public void uploadFile(TempFile tempFile) {