* @throws IOException
* @throws RedirectException
*/
private void renderPage (Hashtable<String, String> fieldPairs, String path, final ToadletContext ctx, String filename)
throws ToadletContextClosedException, IOException, RedirectException {
HTMLNode persistenceFields = renderPersistenceFields(fieldPairs);
if (filename != null) {
File file = new File(filename);
if (file.isDirectory()) lastSuccessful = file.getAbsoluteFile();
else lastSuccessful = file.getParentFile().getAbsoluteFile();
try {
throw new RedirectException(postTo());
} catch (URISyntaxException e) {
sendErrorPage(ctx, 500, NodeL10n.getBase().getString("Toadlet.internalErrorPleaseReport"),
e.getMessage());
}
}
if (path.length() == 0) {
if (lastSuccessful != null && lastSuccessful.isDirectory() && allowedDir(lastSuccessful)) {
path = lastSuccessful.getAbsolutePath();
} else {
path = startingDir();
}
}
File currentPath = new File(path).getCanonicalFile();
//For use in error messages.
String attemptedPath = currentPath == null ? "null" : currentPath.getAbsolutePath();
PageMaker pageMaker = ctx.getPageMaker();
if (currentPath != null && !allowedDir(currentPath)) {
PageNode page = pageMaker.getPageNode(l10n("listingTitle", "path", attemptedPath), ctx);
pageMaker.getInfobox("infobox-error", "Forbidden", page.content, "access-denied", true).
addChild("#", l10n("dirAccessDenied"));
sendErrorPage(ctx, 403, "Forbidden", l10n("dirAccessDenied"));
return;
}
HTMLNode pageNode;
if (currentPath != null && currentPath.exists() && currentPath.isDirectory() && currentPath.canRead()) {
PageNode page = pageMaker.getPageNode(l10n("listingTitle", "path",
currentPath.getAbsolutePath()), ctx);
pageNode = page.outer;
HTMLNode contentNode = page.content;
if (ctx.isAllowedFullAccess()) contentNode.addChild(ctx.getAlertManager().createSummary());
HTMLNode infoboxDiv = contentNode.addChild("div", "class", "infobox");
infoboxDiv.addChild("div", "class", "infobox-header", l10n("listing", "path",
currentPath.getAbsolutePath()));
HTMLNode listingDiv = infoboxDiv.addChild("div", "class", "infobox-content");
File[] files = currentPath.listFiles();
if (files == null) {
sendErrorPage(ctx, 403, "Forbidden", l10n("dirAccessDenied"));
return;
}
Arrays.sort(files, new Comparator<File>() {
@Override
public int compare(File firstFile, File secondFile) {
/* Put directories above files, sorting each alphabetically and
* case-insensitively.
*/
if (firstFile.isDirectory() && !secondFile.isDirectory()) {
return -1;
}
if (!firstFile.isDirectory() && secondFile.isDirectory()) {
return 1;
}
return firstFile.getName().compareToIgnoreCase(secondFile.getName());
}
});
HTMLNode listingTable = listingDiv.addChild("table");
HTMLNode headerRow = listingTable.addChild("tr");
headerRow.addChild("th");
headerRow.addChild("th", l10n("fileHeader"));
headerRow.addChild("th", l10n("sizeHeader"));
/* add filesystem roots (fsck windows) */
for (File currentRoot : File.listRoots()) {
if (allowedDir(currentRoot)) {
HTMLNode rootRow = listingTable.addChild("tr");
rootRow.addChild("td");
HTMLNode rootLinkCellNode = rootRow.addChild("td");
HTMLNode rootLinkFormNode = ctx.addFormChild(rootLinkCellNode, path(),
"insertLocalFileForm");
createChangeDirButton(rootLinkFormNode, currentRoot.getCanonicalPath(),
currentRoot.getAbsolutePath(), persistenceFields);
rootRow.addChild("td");
}
}
/* add back link */
if (currentPath.getParent() != null) {
if (allowedDir(currentPath.getParentFile())) {
HTMLNode backlinkRow = listingTable.addChild("tr");
backlinkRow.addChild("td");
HTMLNode backLinkCellNode = backlinkRow.addChild("td");
HTMLNode backLinkFormNode = ctx.addFormChild(backLinkCellNode, path(),
"insertLocalFileForm");
createChangeDirButton(backLinkFormNode, "..", currentPath.getParent(), persistenceFields);
backlinkRow.addChild("td");
}
}
for (File currentFile : files) {
if(currentFile.isHidden()) {
// It won't be inserted if we insert the whole folder.
// So lets just ignore it, less confusing.
// FIXME in advanced mode, show them, and have a different style, and a toggle for whether to include them in a folder insert.
continue;
}
HTMLNode fileRow = listingTable.addChild("tr");
if (currentFile.isDirectory()) {
if (currentFile.canRead()) {
// Select directory
if (allowedDir(currentFile)) {
HTMLNode cellNode = fileRow.addChild("td");
HTMLNode formNode = ctx.addFormChild(cellNode, path(),
"insertLocalFileForm");
createSelectDirectoryButton(formNode, currentFile.getAbsolutePath(),
persistenceFields);
// Change directory
HTMLNode directoryCellNode = fileRow.addChild("td");
HTMLNode directoryFormNode = ctx.addFormChild(directoryCellNode, path(),
"insertLocalFileForm");
createChangeDirButton(directoryFormNode, currentFile.getName(),
currentFile.getAbsolutePath(), persistenceFields);
}
} else {
fileRow.addChild("td");
fileRow.addChild("td", "class", "unreadable-file",
currentFile.getName());
}
fileRow.addChild("td");
} else {
if (currentFile.canRead()) {
//Select file
HTMLNode cellNode = fileRow.addChild("td");
HTMLNode formNode = ctx.addFormChild(cellNode, path(),
"insertLocalFileForm");
createSelectFileButton(formNode, currentFile.getAbsolutePath(),
persistenceFields);
fileRow.addChild("td", currentFile.getName());
fileRow.addChild("td", "class", "right-align",
String.valueOf(currentFile.length()));
} else {
fileRow.addChild("td");
fileRow.addChild("td", "class", "unreadable-file",
currentFile.getName());
fileRow.addChild("td", "class", "right-align",
String.valueOf(currentFile.length()));
}
}
}
} else {
PageNode page = pageMaker.getPageNode(l10n("listingTitle", "path", attemptedPath), ctx);
pageNode = page.outer;
HTMLNode contentNode = page.content;
if (ctx.isAllowedFullAccess()) contentNode.addChild(ctx.getAlertManager().createSummary());
HTMLNode infoboxDiv = contentNode.addChild("div", "class", "infobox");
infoboxDiv.addChild("div", "class", "infobox-header", l10n("listing", "path", attemptedPath));
HTMLNode listingDiv = infoboxDiv.addChild("div", "class", "infobox-content");
listingDiv.addChild("#", l10n("dirCannotBeRead", "path", attemptedPath));
HTMLNode ulNode = listingDiv.addChild("ul");
ulNode.addChild("li", l10n("checkPathExist"));
ulNode.addChild("li", l10n("checkPathIsDir"));
ulNode.addChild("li", l10n("checkPathReadable"));
}
writeHTMLReply(ctx, 200, "OK", pageNode.generate());
}