com.sippnex.fileblade.service.DirectoryService Maven / Gradle / Ivy
package com.sippnex.fileblade.service;
import com.sippnex.fileblade.domain.FbDirectory;
import com.sippnex.fileblade.domain.FbElement;
import com.sippnex.fileblade.domain.FbFile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;
@Service
public class DirectoryService {
@Value("${fileblade.root:fileblade}")
private String rootPath;
public Set readDirectory(FbDirectory directory) throws IOException {
Set elements = new HashSet<>();
try (DirectoryStream stream = Files.newDirectoryStream(Paths.get(rootPath + directory.getPath()))) {
for (Path path : stream) {
FbElement element;
String pathString = path.toString().substring(rootPath.length());
if(Files.isDirectory(path)) {
element = new FbDirectory(pathString);
} else {
element = new FbFile(pathString);
}
elements.add(element);
}
}
return elements;
}
public void createDirectory(FbDirectory directory) throws IOException {
Path path = Paths.get(rootPath + directory.getPath());
Files.createDirectory(path);
}
}