diff --git a/src/main/java/io/papermc/hangar/config/hangar/HangarConfig.java b/src/main/java/io/papermc/hangar/config/hangar/HangarConfig.java index 4a7f7f06b..c7ad8dc1c 100644 --- a/src/main/java/io/papermc/hangar/config/hangar/HangarConfig.java +++ b/src/main/java/io/papermc/hangar/config/hangar/HangarConfig.java @@ -1,11 +1,9 @@ package io.papermc.hangar.config.hangar; -import io.papermc.hangar.HangarApplication; import io.papermc.hangar.model.Announcement; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; -import org.springframework.boot.system.ApplicationHome; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; @@ -23,7 +21,6 @@ public class HangarConfig { private List sponsors; private boolean dev = true; - private String pluginUploadDir = new ApplicationHome(HangarApplication.class).getDir().toPath().resolve("work").toString(); private String baseUrl; private String gaCode = ""; private List announcements = new ArrayList<>(); @@ -56,6 +53,8 @@ public class HangarConfig { public DiscourseConfig discourse; @NestedConfigurationProperty public JobsConfig jobs; + @NestedConfigurationProperty + public StorageConfig storage; @Component public static class Sponsor { @@ -89,7 +88,7 @@ public class HangarConfig { } @Autowired - public HangarConfig(FakeUserConfig fakeUser, HomepageConfig homepage, ChannelsConfig channels, PagesConfig pages, ProjectsConfig projects, UserConfig user, OrganizationsConfig org, ApiConfig api, SSOConfig sso, HangarSecurityConfig security, QueueConfig queue, DiscourseConfig discourse, JobsConfig jobs) { + public HangarConfig(FakeUserConfig fakeUser, HomepageConfig homepage, ChannelsConfig channels, PagesConfig pages, ProjectsConfig projects, UserConfig user, OrganizationsConfig org, ApiConfig api, SSOConfig sso, HangarSecurityConfig security, QueueConfig queue, DiscourseConfig discourse, JobsConfig jobs, StorageConfig storage) { this.fakeUser = fakeUser; this.homepage = homepage; this.channels = channels; @@ -103,6 +102,7 @@ public class HangarConfig { this.queue = queue; this.discourse = discourse; this.jobs = jobs; + this.storage = storage; } public void checkDev() { @@ -151,14 +151,6 @@ public class HangarConfig { this.dev = dev; } - public String getPluginUploadDir() { - return pluginUploadDir; - } - - public void setPluginUploadDir(String pluginUploadDir) { - this.pluginUploadDir = pluginUploadDir; - } - public String getBaseUrl() { return baseUrl; } diff --git a/src/main/java/io/papermc/hangar/config/hangar/StorageConfig.java b/src/main/java/io/papermc/hangar/config/hangar/StorageConfig.java new file mode 100644 index 000000000..2d1959286 --- /dev/null +++ b/src/main/java/io/papermc/hangar/config/hangar/StorageConfig.java @@ -0,0 +1,30 @@ +package io.papermc.hangar.config.hangar; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.system.ApplicationHome; +import org.springframework.stereotype.Component; +import io.papermc.hangar.HangarApplication; + +@Component +@ConfigurationProperties(prefix = "hangar.storage") +public class StorageConfig { + + private String pluginUploadDir = new ApplicationHome(HangarApplication.class).getDir().toPath().resolve("work").toString(); + private String type = "local"; + + public String getPluginUploadDir() { + return pluginUploadDir; + } + + public void setPluginUploadDir(String pluginUploadDir) { + this.pluginUploadDir = pluginUploadDir; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } +} diff --git a/src/main/java/io/papermc/hangar/service/internal/file/LocalStorageFileService.java b/src/main/java/io/papermc/hangar/service/internal/file/LocalStorageFileService.java index c3e907b13..4b1337e82 100644 --- a/src/main/java/io/papermc/hangar/service/internal/file/LocalStorageFileService.java +++ b/src/main/java/io/papermc/hangar/service/internal/file/LocalStorageFileService.java @@ -1,15 +1,17 @@ package io.papermc.hangar.service.internal.file; -import org.springframework.core.io.FileSystemResource; -import org.springframework.stereotype.Service; +import io.papermc.hangar.util.FileUtils; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; -import io.papermc.hangar.util.FileUtils; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.core.io.FileSystemResource; +import org.springframework.stereotype.Service; @Service +@ConditionalOnProperty(value = "hangar.storage.type", havingValue = "local", matchIfMissing = true) public class LocalStorageFileService implements FileService { @Override public FileSystemResource getResource(String path) { diff --git a/src/main/java/io/papermc/hangar/service/internal/file/S3FileService.java b/src/main/java/io/papermc/hangar/service/internal/file/S3FileService.java index 411849b08..ea07e0ed1 100644 --- a/src/main/java/io/papermc/hangar/service/internal/file/S3FileService.java +++ b/src/main/java/io/papermc/hangar/service/internal/file/S3FileService.java @@ -1,4 +1,56 @@ package io.papermc.hangar.service.internal.file; -public class S3FileService { +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.core.io.FileSystemResource; +import org.springframework.stereotype.Service; +import java.io.IOException; +import java.io.InputStream; + +@Service +@ConditionalOnProperty(value = "hangar.storage.type", havingValue = "object") +public class S3FileService implements FileService { + @Override + public FileSystemResource getResource(String path) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean exists(String path) { + throw new UnsupportedOperationException(); + } + + @Override + public void deleteDirectory(String dir) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean delete(String path) { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] bytes(String path) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void write(InputStream inputStream, String path) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void move(String oldPath, String newPath) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void link(String existingPath, String newPath) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public String resolve(String path, String fileName) { + throw new UnsupportedOperationException(); + } } diff --git a/src/main/java/io/papermc/hangar/service/internal/uploads/ProjectFiles.java b/src/main/java/io/papermc/hangar/service/internal/uploads/ProjectFiles.java index 3b56f4b49..05f4abde4 100644 --- a/src/main/java/io/papermc/hangar/service/internal/uploads/ProjectFiles.java +++ b/src/main/java/io/papermc/hangar/service/internal/uploads/ProjectFiles.java @@ -1,6 +1,6 @@ package io.papermc.hangar.service.internal.uploads; -import io.papermc.hangar.config.hangar.HangarConfig; +import io.papermc.hangar.config.hangar.StorageConfig; import io.papermc.hangar.model.common.Platform; import io.papermc.hangar.service.internal.file.FileService; import io.papermc.hangar.util.FileUtils; @@ -12,7 +12,6 @@ import org.springframework.stereotype.Component; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.StandardCopyOption; @Component public class ProjectFiles { @@ -24,9 +23,9 @@ public class ProjectFiles { private final FileService fileService; @Autowired - public ProjectFiles(HangarConfig hangarConfig, FileService fileService) { + public ProjectFiles(StorageConfig storageConfig, FileService fileService) { this.fileService = fileService; - Path uploadsDir = Path.of(hangarConfig.getPluginUploadDir()); + Path uploadsDir = Path.of(storageConfig.getPluginUploadDir()); pluginsDir = uploadsDir.resolve("plugins"); tmpDir = uploadsDir.resolve("tmp"); if (Files.exists(tmpDir)) { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 37db2ea76..93ce6d381 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -180,6 +180,9 @@ hangar: not-available-timeout: 2m max-concurrent-jobs: 32 + storage: + type: "object" + ################# # Debug Logging # #################