1
0
This commit is contained in:
2022-04-20 21:47:36 +02:00
commit 8abb6d0372
31 changed files with 395 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
package de.nbscloud.files;
import de.nbscloud.files.config.FilesConfig;
import de.nbscloud.files.exception.FileSystemServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class FileSystemService {
public record ContentContainer(boolean directory, String name, long size) {
}
@Autowired
private FilesConfig filesConfig;
@Autowired
private LocationTracker locationTracker;
public Path createDirectory(String name) {
try {
return Files.createDirectory(this.locationTracker.getCurrentLocation().resolve(name));
} catch (IOException e) {
throw new FileSystemServiceException("Could not create directory", e);
}
}
public boolean delete(String name) {
try {
return Files.deleteIfExists(this.locationTracker.getCurrentLocation().resolve(name));
} catch (IOException e) {
throw new FileSystemServiceException("Could not delete file", e);
}
}
// TODO soft vs hard delete - first move into /trash then delete from there
public Path move(Path originalPath, Path newPath) {
try {
return Files.move(originalPath, newPath);
} catch (IOException e) {
throw new FileSystemServiceException("Could not move file", e);
}
}
public List<ContentContainer> list() {
try {
return Files.list(this.locationTracker.getCurrentLocation())
.map(path -> {
try {
return new ContentContainer(Files.isDirectory(path),
path.getFileName().toString(),
Files.size(path));
} catch (IOException e) {
throw new FileSystemServiceException("Could not list files", e);
}
})
.collect(Collectors.toList());
} catch (IOException e) {
throw new FileSystemServiceException("Could not list files", e);
}
}
}

View File

@@ -0,0 +1,54 @@
package de.nbscloud.files;
import de.nbscloud.files.config.FilesConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.nio.file.Path;
import java.nio.file.Paths;
@Component
public class LocationTracker {
@Autowired
private FilesConfig filesConfig;
private Path baseDirPath;
private Path currentLocation;
public void init() {
this.baseDirPath = Paths.get(this.filesConfig.getBaseDir());
this.currentLocation = baseDirPath.resolve("");
}
public void reset() {
init();
}
public Path getCurrentLocation() {
return this.currentLocation;
}
public void setCurrentLocation(String navigateTo) {
validate(navigateTo);
this.currentLocation = this.currentLocation.resolve(navigateTo);
}
private void validate(String navigateTo) {
if(navigateTo == null) {
throw new IllegalStateException("Null");
}
if(navigateTo.contains("..")) {
throw new IllegalStateException("Relative path");
}
if (Paths.get(navigateTo).isAbsolute()) {
throw new IllegalStateException("Absolute path: " + navigateTo);
}
if(!this.currentLocation.resolve(navigateTo).startsWith(this.baseDirPath)) {
throw new IllegalStateException("Illegal path: " + navigateTo);
}
}
}

View File

@@ -0,0 +1,18 @@
package de.nbscloud.files.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "nbs-cloud.files")
public class FilesConfig {
private String baseDir;
public String getBaseDir() {
return baseDir;
}
public void setBaseDir(String baseDir) {
this.baseDir = baseDir;
}
}

View File

@@ -0,0 +1,7 @@
package de.nbscloud.files.exception;
public class FileSystemServiceException extends RuntimeException {
public FileSystemServiceException(String message, Exception cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,14 @@
### This is the main configuration file of the application.
### Filtering of the @...@ values happens via the maven-resource-plugin. The execution of the plugin is configured in
### the Spring Boot parent POM.// The same property exists on the server, look there for documentation
spring.profiles.active=@activeProfiles@
info.app.name=NoBullShit Cloud - Files app
info.app.description=A simple web file admin app
info.build.group=@project.groupId@
info.build.artifact=@project.artifactId@
info.build.version=@project.version@
nbs-cloud.files.baseDir=/home/marius
spring.flyway.enabled=false