use of com.eden.orchid.api.theme.pages.OrchidPage in project Orchid by JavaEden.
the class IndexFileResponse method getResponse.
// TODO: convert this to a component
static OrchidResponse getResponse(OrchidContext context, File targetFile, String targetPath) {
if (targetFile.isDirectory()) {
Clog.i("Rendering directory index: {}", targetPath);
File[] files = targetFile.listFiles();
if (files != null) {
List<FileRow> jsonDirs = new ArrayList<>();
List<FileRow> jsonFiles = new ArrayList<>();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd-hh:mm:ss z", Locale.getDefault());
jsonDirs.add(new FileRow(OrchidUtils.applyBaseUrl(context, StringUtils.strip(targetPath, "/")) + "/..", StringUtils.strip(targetPath, "/") + "/..", ".. (Go up)", "", "", "assets/svg/folder.svg"));
for (File file : files) {
FileRow newFile = new FileRow();
newFile.url = OrchidUtils.applyBaseUrl(context, StringUtils.strip(targetPath, "/") + "/" + file.getName());
newFile.path = StringUtils.strip(targetPath, "/") + "/" + file.getName();
newFile.name = file.getName();
newFile.date = formatter.format(new Date(file.lastModified()));
String icon;
if (file.isDirectory()) {
icon = "folder";
newFile.size = file.listFiles().length + " entries";
jsonDirs.add(newFile);
} else {
icon = FilenameUtils.getExtension(file.getName());
newFile.size = humanReadableByteCount(file.length(), true);
jsonFiles.add(newFile);
}
newFile.icon = "assets/svg/" + icon + ".svg";
}
jsonDirs.sort(Comparator.comparing(fileRow -> fileRow.name));
jsonFiles.sort(Comparator.comparing(fileRow -> fileRow.name));
OrchidResource resource = context.getDefaultResourceSource(null, context.getTheme()).getResourceEntry(context, "templates/server/directoryListing.peb");
Map<String, Object> indexPageVars = new HashMap<>();
indexPageVars.put("title", "List of files/dirs under " + targetPath);
indexPageVars.put("path", targetPath);
indexPageVars.put("dirs", jsonDirs);
indexPageVars.put("files", jsonFiles);
Map<String, Object> object = new HashMap<>(context.getConfig());
object.put("page", indexPageVars);
object.put("theme", context.getTheme());
String directoryListingContent;
if (resource != null) {
directoryListingContent = context.compile(resource, resource.getReference().getExtension(), resource.getContent(), object);
} else {
directoryListingContent = context.serialize("json", object);
}
OrchidPage page = new OrchidPage(new StringResource(new OrchidReference(context, "directoryListing.txt"), directoryListingContent), RenderService.RenderMode.TEMPLATE, "directoryListing", "Index");
return new OrchidResponse(context).page(page).status(404);
}
}
return new OrchidResponse(context).content("");
}
use of com.eden.orchid.api.theme.pages.OrchidPage in project Orchid by JavaEden.
the class FileController method findPage.
@Override
public OrchidResponse findPage(OrchidContext context, String targetPath) {
List<String> possibleMatches = CollectionsKt.listOf(targetPath, targetPath + "/" + indexFiles[0], targetPath + "/" + indexFiles[1]);
OrchidPage matchedPage = null;
for (String possibleMatch : possibleMatches) {
Stream<OrchidPage> indexedPagesStream = context.getIndex().getAllIndexedPages().values().stream().flatMap(it -> it.getFirst().getAllPages().stream());
Stream<AssetPage> assetPagesStream = context.getAssetManager().getAllAssetPages();
Stream<OrchidPage> allOrchidPagesStream = Stream.concat(indexedPagesStream, assetPagesStream);
OrchidPage page = OrchidExtensionsKt.findPageByServerPath(allOrchidPagesStream, possibleMatch);
if (page != null) {
matchedPage = page;
break;
}
}
if (matchedPage != null) {
return new OrchidResponse(context).page(matchedPage).status(200);
} else {
return findFile(context, targetPath);
}
}
Aggregations