use of org.structr.web.entity.Folder in project structr by structr.
the class StructrShellCommand method findRelativeFolder.
public Folder findRelativeFolder(final Folder baseFolder, final String path) throws FrameworkException {
final App app = StructrApp.getInstance();
Folder folder = baseFolder;
boolean found = false;
for (final String part : path.split("[/]+")) {
if (folder == null) {
folder = app.nodeQuery(Folder.class).and(Folder.name, part).getFirst();
} else {
for (final Folder child : folder.getFolders()) {
if (part.equals(child.getName())) {
folder = child;
found = true;
}
}
if (!found) {
return null;
}
}
}
return folder;
}
use of org.structr.web.entity.Folder in project structr by structr.
the class StructrFileSystemView method getHomeDirectory.
@Override
public FtpFile getHomeDirectory() throws FtpException {
try (Tx tx = StructrApp.getInstance(securityContext).tx()) {
org.structr.web.entity.User structrUser = (org.structr.web.entity.User) AuthHelper.getPrincipalForCredential(Principal.name, user.getName());
final Folder homeDir = structrUser.getHomeDirectory();
tx.success();
return new StructrFtpFolder(securityContext, homeDir);
} catch (FrameworkException fex) {
logger.error("Error while getting home directory", fex);
}
return null;
}
use of org.structr.web.entity.Folder in project structr by structr.
the class StructrFileSystemView method getFile.
@Override
public FtpFile getFile(final String rawRequestedPath) throws FtpException {
String requestedPath = rawRequestedPath;
// remove trailing slash
if (requestedPath.endsWith("/")) {
requestedPath = requestedPath.substring(0, requestedPath.length() - 1);
}
logger.info("Requested path: {}, cleaned to {}", rawRequestedPath, requestedPath);
try (Tx tx = StructrApp.getInstance(securityContext).tx()) {
if (StringUtils.isBlank(requestedPath) || "/".equals(requestedPath)) {
return getHomeDirectory();
}
StructrFtpFolder cur = (StructrFtpFolder) getWorkingDirectory();
if (".".equals(requestedPath) || "./".equals(requestedPath)) {
return cur;
}
if ("..".equals(requestedPath) || "../".equals(requestedPath)) {
return new StructrFtpFolder(securityContext, cur.getStructrFile().getParent());
}
// If relative path requested, prepend base path
if (!requestedPath.startsWith("/")) {
String basePath = cur.getAbsolutePath();
logger.info("Base path: {}", basePath);
while (requestedPath.startsWith("..")) {
requestedPath = StringUtils.stripStart(StringUtils.stripStart(requestedPath, ".."), "/");
basePath = StringUtils.substringBeforeLast(basePath, "/");
}
requestedPath = StringUtils.stripEnd(basePath.equals("/") ? "/".concat(requestedPath) : basePath.concat("/").concat(requestedPath), "/");
logger.info("Base path: {}, requestedPath: {}", new Object[] { basePath, requestedPath });
}
AbstractFile file = FileHelper.getFileByAbsolutePath(securityContext, requestedPath);
if (file != null) {
if (file instanceof Folder) {
tx.success();
return new StructrFtpFolder(securityContext, (Folder) file);
} else {
tx.success();
return new StructrFtpFile(securityContext, (File) file);
}
}
// Look up a page by its name
Page page = StructrApp.getInstance(securityContext).nodeQuery(Page.class).andName(PathHelper.getName(requestedPath)).getFirst();
if (page != null) {
tx.success();
return new FtpFilePageWrapper(page);
}
logger.warn("No existing file found: {}", requestedPath);
tx.success();
return new FileOrFolder(requestedPath, user);
} catch (FrameworkException fex) {
logger.error("Error in getFile()", fex);
}
return null;
}
use of org.structr.web.entity.Folder in project structr by structr.
the class StructrFtpFolder method listFiles.
@Override
public List<FtpFile> listFiles() {
final List<FtpFile> ftpFiles = new ArrayList();
final App app = StructrApp.getInstance(securityContext);
try (final Tx tx = app.tx()) {
String requestedPath = getAbsolutePath();
logger.debug("Children of {} requested", requestedPath);
if ("/".equals(requestedPath)) {
try {
Result<Folder> folders = app.nodeQuery(Folder.class).getResult();
logger.debug("{} folders found", folders.size());
for (Folder f : folders.getResults()) {
if (f.getHasParent()) {
continue;
}
FtpFile ftpFile = new StructrFtpFolder(securityContext, f);
logger.debug("Folder found: {}", ftpFile.getAbsolutePath());
ftpFiles.add(ftpFile);
}
Result<File> files = app.nodeQuery(File.class).getResult();
logger.debug("{} files found", files.size());
for (File f : files.getResults()) {
if (f.getHasParent()) {
continue;
}
logger.debug("Structr file found: {}", f);
FtpFile ftpFile = new StructrFtpFile(securityContext, f);
logger.debug("File found: {}", ftpFile.getAbsolutePath());
ftpFiles.add(ftpFile);
}
Result<Page> pages = app.nodeQuery(Page.class).getResult();
logger.debug("{} pages found", pages.size());
for (Page p : pages.getResults()) {
logger.debug("Structr page found: {}", p);
ftpFiles.add(new FtpFilePageWrapper(p));
}
return ftpFiles;
} catch (FrameworkException ex) {
logger.error("", ex);
}
}
Iterable<Folder> folders = ((Folder) structrFile).getFolders();
for (Folder f : folders) {
FtpFile ftpFile = new StructrFtpFolder(securityContext, f);
logger.debug("Subfolder found: {}", ftpFile.getAbsolutePath());
ftpFiles.add(ftpFile);
}
Iterable<File> files = ((Folder) structrFile).getFiles();
for (File f : files) {
FtpFile ftpFile = new StructrFtpFile(securityContext, f);
logger.debug("File found: {}", ftpFile.getAbsolutePath());
ftpFiles.add(ftpFile);
}
tx.success();
return ftpFiles;
} catch (FrameworkException fex) {
logger.error("Error in listFiles()", fex);
}
return null;
}
use of org.structr.web.entity.Folder in project structr by structr.
the class StructrFilesPath method getDirectoryStream.
@Override
public DirectoryStream<Path> getDirectoryStream(final DirectoryStream.Filter<? super Path> filter) {
return new DirectoryStream() {
boolean closed = false;
@Override
public Iterator iterator() {
if (!closed) {
final App app = StructrApp.getInstance(fs.getSecurityContext());
final PropertyKey<Boolean> hasParentKey = StructrApp.key(AbstractFile.class, "hasParent");
final List<StructrPath> files = new LinkedList<>();
try (final Tx tx = app.tx()) {
for (final Folder folder : app.nodeQuery(Folder.class).and(hasParentKey, false).getAsList()) {
files.add(new StructrFilePath(fs, StructrFilesPath.this, folder.getName()));
}
for (final File file : app.nodeQuery(File.class).and(hasParentKey, false).getAsList()) {
files.add(new StructrFilePath(fs, StructrFilesPath.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;
}
};
}
Aggregations