2022-11-20 09:24:42 +00:00
|
|
|
import { FileMeta } from "../types.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
import { SpacePrimitives } from "./space_primitives.ts";
|
2022-11-20 09:24:42 +00:00
|
|
|
import type { SysCallMapping } from "../../plugos/system.ts";
|
|
|
|
|
|
|
|
// Enriches the file list listing with custom metadata from the page index
|
|
|
|
export class FileMetaSpacePrimitives implements SpacePrimitives {
|
|
|
|
constructor(
|
|
|
|
private wrapped: SpacePrimitives,
|
|
|
|
private indexSyscalls: SysCallMapping,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchFileList(): Promise<FileMeta[]> {
|
2023-01-13 14:41:29 +00:00
|
|
|
const files = await this.wrapped.fetchFileList();
|
2022-11-20 09:24:42 +00:00
|
|
|
// Enrich the file list with custom meta data (for pages)
|
|
|
|
const allFilesMap: Map<string, any> = new Map(
|
2023-01-13 14:41:29 +00:00
|
|
|
files.map((fm) => [fm.name, fm]),
|
2022-11-20 09:24:42 +00:00
|
|
|
);
|
|
|
|
for (
|
|
|
|
const { page, value } of await this.indexSyscalls["index.queryPrefix"](
|
|
|
|
{} as any,
|
|
|
|
"meta:",
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
const p = allFilesMap.get(`${page}.md`);
|
|
|
|
if (p) {
|
|
|
|
for (const [k, v] of Object.entries(value)) {
|
|
|
|
if (
|
|
|
|
["name", "lastModified", "size", "perm", "contentType"].includes(k)
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
p[k] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [...allFilesMap.values()];
|
|
|
|
}
|
|
|
|
|
|
|
|
readFile(
|
|
|
|
name: string,
|
2023-05-23 18:53:53 +00:00
|
|
|
): Promise<{ data: Uint8Array; meta: FileMeta }> {
|
|
|
|
return this.wrapped.readFile(name);
|
2022-11-20 09:24:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-02 09:25:32 +00:00
|
|
|
async getFileMeta(name: string): Promise<FileMeta> {
|
|
|
|
const meta = await this.wrapped.getFileMeta(name);
|
|
|
|
if (name.endsWith(".md")) {
|
|
|
|
const pageName = name.slice(0, -3);
|
|
|
|
const additionalMeta = await this.indexSyscalls["index.get"](
|
|
|
|
{} as any,
|
|
|
|
pageName,
|
|
|
|
"meta:",
|
|
|
|
);
|
|
|
|
if (additionalMeta) {
|
|
|
|
for (const [k, v] of Object.entries(additionalMeta)) {
|
|
|
|
if (
|
|
|
|
["name", "lastModified", "size", "perm", "contentType"].includes(k)
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
meta[k] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return meta;
|
2022-11-20 09:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeFile(
|
|
|
|
name: string,
|
2023-05-23 18:53:53 +00:00
|
|
|
data: Uint8Array,
|
2023-01-13 14:41:29 +00:00
|
|
|
selfUpdate?: boolean,
|
2023-07-02 09:25:32 +00:00
|
|
|
meta?: FileMeta,
|
2022-11-20 09:24:42 +00:00
|
|
|
): Promise<FileMeta> {
|
2023-05-23 18:53:53 +00:00
|
|
|
return this.wrapped.writeFile(
|
|
|
|
name,
|
|
|
|
data,
|
|
|
|
selfUpdate,
|
2023-07-02 09:25:32 +00:00
|
|
|
meta,
|
2023-05-23 18:53:53 +00:00
|
|
|
);
|
2022-11-20 09:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteFile(name: string): Promise<void> {
|
|
|
|
return this.wrapped.deleteFile(name);
|
|
|
|
}
|
|
|
|
}
|