use of org.structr.web.entity.Folder in project structr by structr.
the class FileOrFolder method createOutputStream.
@Override
public OutputStream createOutputStream(final long l) throws IOException {
try (Tx tx = StructrApp.getInstance().tx()) {
if (structrFile == null) {
final Folder parentFolder = (Folder) FileHelper.getFileByAbsolutePath(securityContext, StringUtils.substringBeforeLast(newPath, "/"));
try {
structrFile = FileHelper.createFile(securityContext, new byte[0], null, File.class, getName());
structrFile.setProperty(AbstractNode.type, File.class.getSimpleName());
structrFile.setProperty(AbstractNode.owner, owner.getStructrUser());
if (parentFolder != null) {
structrFile.setParent(parentFolder);
}
} catch (FrameworkException ex) {
logger.error("", ex);
return null;
}
}
tx.success();
return ((File) structrFile).getOutputStream();
} catch (FrameworkException fex) {
logger.error(null, fex);
}
return null;
}
use of org.structr.web.entity.Folder in project structr by structr.
the class FileOrFolder method mkdir.
@Override
public boolean mkdir() {
final App app = StructrApp.getInstance(securityContext);
try (final Tx tx = app.tx()) {
logger.info("mkdir() Folder");
AbstractFile existing = FileHelper.getFileByAbsolutePath(SecurityContext.getSuperUserInstance(), newPath);
if (existing != null) {
logger.warn("File {} already exists.", newPath);
return false;
}
final Folder parentFolder = (Folder) FileHelper.getFileByAbsolutePath(securityContext, StringUtils.substringBeforeLast(newPath, "/"));
try {
Folder newFolder = (Folder) app.command(CreateNodeCommand.class).execute(new NodeAttribute(AbstractNode.type, Folder.class.getSimpleName()), new NodeAttribute(AbstractNode.owner, owner.getStructrUser()), new NodeAttribute(AbstractNode.name, getName()));
if (parentFolder != null) {
newFolder.setParent(parentFolder);
}
} catch (FrameworkException ex) {
logger.error("", ex);
return false;
}
tx.success();
return true;
} catch (FrameworkException ex) {
logger.error("", ex);
return false;
}
}
use of org.structr.web.entity.Folder in project structr by structr.
the class StructrSSHFile method findFile.
public StructrSSHFile findFile(final String path) {
if ("/".equals(path)) {
return getRootFolder();
}
final App app = StructrApp.getInstance(getRootFolder().getSecurityContext());
final boolean isAbsolute = path.startsWith("/");
final String localPath = isAbsolute ? path.substring(1) : path;
final String[] parts = localPath.split("[/]+");
final String localPart = parts[0];
if (".".equals(path)) {
return this;
}
if ("..".equals(path)) {
return parent;
}
if (isAbsolute && parent != null) {
return getRootFolder().findFile(path);
} else {
// look for files/folders without parent
try (final Tx tx = app.tx()) {
for (final Folder folder : getFolders()) {
final String folderName = folder.getName();
if (localPart.equals(folderName)) {
final StructrSSHFile matchingFolder = new StructrSSHFile(this, folderName, folder);
if (parts.length > 1) {
return matchingFolder.findFile(localPath.substring(folderName.length() + 1));
} else {
// match found
return matchingFolder;
}
}
}
for (final File file : getFiles()) {
final String fileName = file.getName();
if (localPart.equals(fileName)) {
if (parts.length > 1) {
throw new IllegalStateException("Found file where folder was expected, aborting.");
}
return new StructrSSHFile(this, fileName, file);
}
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
}
return new StructrSSHFile(this, path, null);
}
use of org.structr.web.entity.Folder in project structr by structr.
the class StructrShellCommand method getPrompt.
public String getPrompt() {
final App app = StructrApp.getInstance();
final StringBuilder buf = new StringBuilder();
try (final Tx tx = app.tx()) {
buf.append(user.getName());
buf.append("@structr:");
if (currentFolder != null) {
String folderPart = currentFolder.getPath();
final Folder homeFolder = user.getHomeDirectory();
if (homeFolder != null) {
// replace home directory with ~ if at the beginning of the full path
final String homeFolderPath = homeFolder.getPath();
if (folderPart.startsWith(homeFolderPath)) {
folderPart = "~" + folderPart.substring(homeFolderPath.length());
}
}
buf.append(folderPart);
} else {
buf.append("/");
}
buf.append(user.isAdmin() ? "#" : "$");
buf.append(" ");
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
return buf.toString();
}
use of org.structr.web.entity.Folder in project structr by structr.
the class StructrFilePath method getDirectoryStream.
@Override
public DirectoryStream<Path> getDirectoryStream(DirectoryStream.Filter<? super Path> filter) {
final Folder folder = (Folder) getActualFile();
if (folder != null) {
return new DirectoryStream() {
boolean closed = false;
@Override
public Iterator iterator() {
if (!closed) {
final App app = StructrApp.getInstance(fs.getSecurityContext());
final List<StructrPath> files = new LinkedList<>();
try (final Tx tx = app.tx()) {
for (final Folder folder : folder.getFolders()) {
files.add(new StructrFilePath(fs, StructrFilePath.this, folder.getName()));
}
for (final File file : folder.getFiles()) {
files.add(new StructrFilePath(fs, StructrFilePath.this, file.getName()));
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
return files.iterator();
}
return Collections.emptyIterator();
}
@Override
public void close() throws IOException {
closed = true;
}
};
}
return null;
}
Aggregations