69 lines
2.3 KiB
Java
69 lines
2.3 KiB
Java
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);
|
|
}
|
|
}
|
|
}
|