use of org.structr.web.entity.AbstractFile in project structr by structr.
the class StructrFilePath method delete.
@Override
public void delete() throws IOException {
final App app = StructrApp.getInstance(fs.getSecurityContext());
final AbstractFile actualFile = getActualFile();
try (final Tx tx = app.tx()) {
// if a folder is to be deleted, check contents
if (actualFile instanceof Folder && ((Folder) actualFile).getChildren().iterator().hasNext()) {
throw new DirectoryNotEmptyException(getActualFile().getPath());
} else {
app.delete(actualFile);
// remove cached version
this.cachedActualFile = null;
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("Unable to delete file {}: {}", new Object[] { getActualFile().getPath(), fex.getMessage() });
}
}
use of org.structr.web.entity.AbstractFile in project structr by structr.
the class AbstractStructrFtpFile method move.
@Override
public boolean move(final FtpFile target) {
try (Tx tx = StructrApp.getInstance(securityContext).tx()) {
logger.info("move()");
final AbstractStructrFtpFile targetFile = (AbstractStructrFtpFile) target;
final String path = targetFile instanceof StructrFtpFile ? "/" : targetFile.getAbsolutePath();
try {
if (path.contains("/")) {
String newParentPath = StringUtils.substringBeforeLast(path, "/");
AbstractFile newParent = FileHelper.getFileByAbsolutePath(securityContext, newParentPath);
if (newParent != null && newParent instanceof Folder) {
Folder newParentFolder = (Folder) newParent;
structrFile.setParent(newParentFolder);
} else {
// Move to /
structrFile.setParent(null);
}
}
if (!("/".equals(path))) {
final String newName = path.contains("/") ? StringUtils.substringAfterLast(path, "/") : path;
structrFile.setProperty(AbstractNode.name, newName);
}
} catch (FrameworkException ex) {
logger.error("Could not move ftp file", ex);
return false;
}
tx.success();
return true;
} catch (FrameworkException ex) {
logger.error("", ex);
}
return false;
}
use of org.structr.web.entity.AbstractFile 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.AbstractFile 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.AbstractFile 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);
}
}
}
Aggregations