make storage impl selectable

This commit is contained in:
MiniDigger | Martin 2022-08-07 00:48:44 +02:00
parent 31ca777132
commit d529b35bc7
6 changed files with 98 additions and 20 deletions

View File

@ -1,11 +1,9 @@
package io.papermc.hangar.config.hangar; package io.papermc.hangar.config.hangar;
import io.papermc.hangar.HangarApplication;
import io.papermc.hangar.model.Announcement; import io.papermc.hangar.model.Announcement;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -23,7 +21,6 @@ public class HangarConfig {
private List<Sponsor> sponsors; private List<Sponsor> sponsors;
private boolean dev = true; private boolean dev = true;
private String pluginUploadDir = new ApplicationHome(HangarApplication.class).getDir().toPath().resolve("work").toString();
private String baseUrl; private String baseUrl;
private String gaCode = ""; private String gaCode = "";
private List<Announcement> announcements = new ArrayList<>(); private List<Announcement> announcements = new ArrayList<>();
@ -56,6 +53,8 @@ public class HangarConfig {
public DiscourseConfig discourse; public DiscourseConfig discourse;
@NestedConfigurationProperty @NestedConfigurationProperty
public JobsConfig jobs; public JobsConfig jobs;
@NestedConfigurationProperty
public StorageConfig storage;
@Component @Component
public static class Sponsor { public static class Sponsor {
@ -89,7 +88,7 @@ public class HangarConfig {
} }
@Autowired @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.fakeUser = fakeUser;
this.homepage = homepage; this.homepage = homepage;
this.channels = channels; this.channels = channels;
@ -103,6 +102,7 @@ public class HangarConfig {
this.queue = queue; this.queue = queue;
this.discourse = discourse; this.discourse = discourse;
this.jobs = jobs; this.jobs = jobs;
this.storage = storage;
} }
public void checkDev() { public void checkDev() {
@ -151,14 +151,6 @@ public class HangarConfig {
this.dev = dev; this.dev = dev;
} }
public String getPluginUploadDir() {
return pluginUploadDir;
}
public void setPluginUploadDir(String pluginUploadDir) {
this.pluginUploadDir = pluginUploadDir;
}
public String getBaseUrl() { public String getBaseUrl() {
return baseUrl; return baseUrl;
} }

View File

@ -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;
}
}

View File

@ -1,15 +1,17 @@
package io.papermc.hangar.service.internal.file; package io.papermc.hangar.service.internal.file;
import org.springframework.core.io.FileSystemResource; import io.papermc.hangar.util.FileUtils;
import org.springframework.stereotype.Service;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption; 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 @Service
@ConditionalOnProperty(value = "hangar.storage.type", havingValue = "local", matchIfMissing = true)
public class LocalStorageFileService implements FileService { public class LocalStorageFileService implements FileService {
@Override @Override
public FileSystemResource getResource(String path) { public FileSystemResource getResource(String path) {

View File

@ -1,4 +1,56 @@
package io.papermc.hangar.service.internal.file; 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();
}
} }

View File

@ -1,6 +1,6 @@
package io.papermc.hangar.service.internal.uploads; 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.model.common.Platform;
import io.papermc.hangar.service.internal.file.FileService; import io.papermc.hangar.service.internal.file.FileService;
import io.papermc.hangar.util.FileUtils; import io.papermc.hangar.util.FileUtils;
@ -12,7 +12,6 @@ import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
@Component @Component
public class ProjectFiles { public class ProjectFiles {
@ -24,9 +23,9 @@ public class ProjectFiles {
private final FileService fileService; private final FileService fileService;
@Autowired @Autowired
public ProjectFiles(HangarConfig hangarConfig, FileService fileService) { public ProjectFiles(StorageConfig storageConfig, FileService fileService) {
this.fileService = fileService; this.fileService = fileService;
Path uploadsDir = Path.of(hangarConfig.getPluginUploadDir()); Path uploadsDir = Path.of(storageConfig.getPluginUploadDir());
pluginsDir = uploadsDir.resolve("plugins"); pluginsDir = uploadsDir.resolve("plugins");
tmpDir = uploadsDir.resolve("tmp"); tmpDir = uploadsDir.resolve("tmp");
if (Files.exists(tmpDir)) { if (Files.exists(tmpDir)) {

View File

@ -180,6 +180,9 @@ hangar:
not-available-timeout: 2m not-available-timeout: 2m
max-concurrent-jobs: 32 max-concurrent-jobs: 32
storage:
type: "object"
################# #################
# Debug Logging # # Debug Logging #
################# #################