use of org.structr.web.entity.Folder in project structr by structr.
the class CatCommand method listFolder.
// ----- private methods -----
private void listFolder(final StructrShellCommand parent, final Iterable<AbstractFile> folder) throws FrameworkException, IOException {
boolean hasContents = false;
for (final AbstractFile child : folder) {
if (parent.isAllowed(child, Permission.read, false)) {
hasContents = true;
if (child instanceof Folder) {
term.setBold(true);
term.setTextColor(4);
term.print(child.getName() + " ");
term.setTextColor(7);
term.setBold(false);
} else {
term.print(child.getName() + " ");
}
}
}
if (hasContents) {
term.println();
}
}
use of org.structr.web.entity.Folder in project structr by structr.
the class CatCommand method displayAutocompleteSuggestions.
private void displayAutocompleteSuggestions(final StructrShellCommand parent, final Iterable<AbstractFile> files, final String line) throws IOException {
final StringBuilder buf = new StringBuilder();
for (final AbstractFile file : files) {
if (parent.isAllowed(file, Permission.read, false)) {
buf.append(file.getName());
if (file instanceof Folder) {
buf.append("/ ");
}
}
}
if (buf.length() > 0) {
term.println();
term.print(buf.toString());
term.println();
parent.displayPrompt();
term.print(line);
}
}
use of org.structr.web.entity.Folder in project structr by structr.
the class CatCommand method handleTabCompletion.
@Override
public void handleTabCompletion(final StructrShellCommand parent, final String line, final int tabCount) throws IOException {
if (line.contains(" ") && line.length() >= 3) {
String incompletePath = line.substring(line.indexOf(" ") + 1);
Folder baseFolder = null;
String lastPathPart = null;
if (incompletePath.startsWith("\"")) {
incompletePath = incompletePath.substring(1);
}
final App app = StructrApp.getInstance();
if ("..".equals(incompletePath)) {
term.handleCharacter('/');
return;
}
if (incompletePath.startsWith("/")) {
incompletePath = incompletePath.substring(1);
} else {
baseFolder = parent.getCurrentFolder();
}
// identify full path parts and find folders
final String[] parts = incompletePath.split("[/]+");
final int partCount = parts.length;
try (final Tx tx = app.tx()) {
// only a single path part
if (partCount == 1) {
lastPathPart = parts[0];
} else {
lastPathPart = parts[partCount - 1];
// more than a single path part, find preceding folders
for (int i = 0; i < partCount - 1; i++) {
// skip empty path parts
if (StringUtils.isNotBlank(parts[i])) {
baseFolder = app.nodeQuery(Folder.class).and(StructrApp.key(AbstractFile.class, "parent"), baseFolder).and(Folder.name, parts[i]).getFirst();
if (baseFolder == null) {
return;
}
}
}
}
final List<AbstractFile> allFiles = new LinkedList<>();
final List<AbstractFile> files = new LinkedList<>();
if (baseFolder != null) {
allFiles.addAll(Iterables.toList(baseFolder.getChildren()));
} else {
allFiles.addAll(app.nodeQuery(AbstractFile.class).and(StructrApp.key(File.class, "parent"), null).getAsList());
}
for (final AbstractFile file : allFiles) {
if (file.getName().startsWith(lastPathPart)) {
files.add(file);
}
}
if (files.size() > 1) {
// only display autocomplete suggestions after second tab
if (tabCount > 1) {
displayAutocompleteSuggestions(parent, files, line);
}
} else if (!files.isEmpty()) {
final AbstractFile file = files.get(0);
if (file instanceof Folder) {
final Folder folder = (Folder) file;
if (parent.isAllowed(folder, Permission.read, false)) {
if (lastPathPart.equals(folder.getName())) {
// only display autocomplete suggestions after second tab
if (tabCount > 1) {
displayAutocompleteSuggestions(parent, folder.getChildren(), line);
} else {
if (!line.endsWith("/")) {
term.handleCharacter('/');
}
}
} else {
displayAutocompleteItem(folder, lastPathPart);
}
}
} else {
final AbstractFile existingFile = files.get(0);
if (parent.isAllowed(existingFile, Permission.read, false)) {
if (!lastPathPart.equals(existingFile.getName())) {
displayAutocompleteItem(existingFile, lastPathPart);
}
}
}
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
}
}
use of org.structr.web.entity.Folder in project structr by structr.
the class CdCommand method displayAutocompleteSuggestions.
private void displayAutocompleteSuggestions(final StructrShellCommand parent, final Iterable<Folder> folders, final String line) throws IOException {
final StringBuilder buf = new StringBuilder();
for (final Folder folder : folders) {
if (parent.isAllowed(folder, Permission.read, false)) {
buf.append(folder.getName()).append("/ ");
}
}
if (buf.length() > 0) {
term.println();
term.print(buf.toString());
term.println();
parent.displayPrompt();
term.print(line);
}
}
use of org.structr.web.entity.Folder in project structr by structr.
the class CdCommand method setFolder.
// ----- private methods -----
private void setFolder(final StructrShellCommand parent, final Folder currentFolder, final String targetFolderName) throws IOException, FrameworkException {
final App app = StructrApp.getInstance();
String target = targetFolderName;
// remove trailing slash
if (target.endsWith("/")) {
target = target.substring(0, target.length() - 1);
}
if (target.startsWith("/")) {
final Folder folder = app.nodeQuery(Folder.class).and(StructrApp.key(Folder.class, "path"), target).getFirst();
if (folder != null) {
if (parent.isAllowed(folder, Permission.read, true)) {
parent.setCurrentFolder(folder);
} else {
term.println("Permission denied");
}
} else {
term.println("Folder " + target + " does not exist");
}
} else {
final Folder newFolder = parent.findRelativeFolder(currentFolder, target);
if (newFolder == null) {
term.println("Folder " + target + " does not exist");
} else {
if (parent.isAllowed(newFolder, Permission.read, true)) {
parent.setCurrentFolder(newFolder);
} else {
term.println("Permission denied");
}
}
}
}
Aggregations