2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
mkdir,
|
|
|
|
readdir,
|
|
|
|
readFile,
|
|
|
|
stat,
|
|
|
|
unlink,
|
|
|
|
utimes,
|
|
|
|
writeFile,
|
|
|
|
} from "fs/promises";
|
2022-03-20 08:56:28 +00:00
|
|
|
import * as path from "path";
|
2022-09-05 09:47:30 +00:00
|
|
|
import { AttachmentMeta, PageMeta } from "../types";
|
2022-09-05 14:15:01 +00:00
|
|
|
import {
|
|
|
|
AttachmentData,
|
|
|
|
AttachmentEncoding,
|
|
|
|
SpacePrimitives,
|
|
|
|
} from "./space_primitives";
|
2022-04-25 08:33:38 +00:00
|
|
|
import { Plug } from "@plugos/plugos/plug";
|
2022-04-29 16:54:27 +00:00
|
|
|
import { realpathSync } from "fs";
|
2022-09-05 09:47:30 +00:00
|
|
|
import mime from "mime-types";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-04-08 15:46:09 +00:00
|
|
|
export class DiskSpacePrimitives implements SpacePrimitives {
|
2022-03-20 08:56:28 +00:00
|
|
|
rootPath: string;
|
2022-04-06 13:39:20 +00:00
|
|
|
plugPrefix: string;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-04-06 13:39:20 +00:00
|
|
|
constructor(rootPath: string, plugPrefix: string = "_plug/") {
|
2022-04-29 16:54:27 +00:00
|
|
|
this.rootPath = realpathSync(rootPath);
|
2022-04-06 13:39:20 +00:00
|
|
|
this.plugPrefix = plugPrefix;
|
|
|
|
}
|
|
|
|
|
2022-04-29 16:54:27 +00:00
|
|
|
safePath(p: string): string {
|
2022-05-01 17:20:38 +00:00
|
|
|
let realPath = path.resolve(p);
|
2022-04-29 16:54:27 +00:00
|
|
|
if (!realPath.startsWith(this.rootPath)) {
|
|
|
|
throw Error(`Path ${p} is not in the space`);
|
|
|
|
}
|
|
|
|
return realPath;
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:39:20 +00:00
|
|
|
pageNameToPath(pageName: string) {
|
|
|
|
if (pageName.startsWith(this.plugPrefix)) {
|
2022-04-29 16:54:27 +00:00
|
|
|
return this.safePath(path.join(this.rootPath, pageName + ".plug.json"));
|
2022-04-06 13:39:20 +00:00
|
|
|
}
|
2022-04-29 16:54:27 +00:00
|
|
|
return this.safePath(path.join(this.rootPath, pageName + ".md"));
|
2022-04-06 13:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pathToPageName(fullPath: string): string {
|
|
|
|
let extLength = fullPath.endsWith(".plug.json")
|
|
|
|
? ".plug.json".length
|
|
|
|
: ".md".length;
|
|
|
|
return fullPath.substring(
|
|
|
|
this.rootPath.length + 1,
|
|
|
|
fullPath.length - extLength
|
|
|
|
);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:47:30 +00:00
|
|
|
// Pages
|
2022-03-20 08:56:28 +00:00
|
|
|
async readPage(pageName: string): Promise<{ text: string; meta: PageMeta }> {
|
2022-04-06 13:39:20 +00:00
|
|
|
const localPath = this.pageNameToPath(pageName);
|
2022-03-20 08:56:28 +00:00
|
|
|
try {
|
|
|
|
const s = await stat(localPath);
|
|
|
|
return {
|
|
|
|
text: await readFile(localPath, "utf8"),
|
|
|
|
meta: {
|
|
|
|
name: pageName,
|
|
|
|
lastModified: s.mtime.getTime(),
|
2022-05-17 09:53:17 +00:00
|
|
|
perm: "rw",
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
} catch (e) {
|
2022-03-23 14:41:12 +00:00
|
|
|
// console.error("Error while reading page", pageName, e);
|
2022-03-20 08:56:28 +00:00
|
|
|
throw Error(`Could not read page ${pageName}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:39:20 +00:00
|
|
|
async writePage(
|
|
|
|
pageName: string,
|
|
|
|
text: string,
|
2022-04-08 15:46:09 +00:00
|
|
|
selfUpdate: boolean,
|
2022-04-06 13:39:20 +00:00
|
|
|
lastModified?: number
|
|
|
|
): Promise<PageMeta> {
|
|
|
|
let localPath = this.pageNameToPath(pageName);
|
2022-03-20 08:56:28 +00:00
|
|
|
try {
|
2022-03-23 14:41:12 +00:00
|
|
|
// Ensure parent folder exists
|
|
|
|
await mkdir(path.dirname(localPath), { recursive: true });
|
|
|
|
|
|
|
|
// Actually write the file
|
2022-03-20 08:56:28 +00:00
|
|
|
await writeFile(localPath, text);
|
|
|
|
|
2022-04-06 13:39:20 +00:00
|
|
|
if (lastModified) {
|
|
|
|
let d = new Date(lastModified);
|
|
|
|
console.log("Going to set the modified time", d);
|
2022-04-07 12:04:50 +00:00
|
|
|
await utimes(localPath, d, d);
|
2022-04-06 13:39:20 +00:00
|
|
|
}
|
2022-03-23 14:41:12 +00:00
|
|
|
// Fetch new metadata
|
2022-03-20 08:56:28 +00:00
|
|
|
const s = await stat(localPath);
|
|
|
|
return {
|
|
|
|
name: pageName,
|
|
|
|
lastModified: s.mtime.getTime(),
|
2022-05-17 09:53:17 +00:00
|
|
|
perm: "rw",
|
2022-03-20 08:56:28 +00:00
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error while writing page", pageName, e);
|
|
|
|
throw Error(`Could not write ${pageName}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getPageMeta(pageName: string): Promise<PageMeta> {
|
2022-04-06 13:39:20 +00:00
|
|
|
let localPath = this.pageNameToPath(pageName);
|
2022-03-20 08:56:28 +00:00
|
|
|
try {
|
|
|
|
const s = await stat(localPath);
|
|
|
|
return {
|
|
|
|
name: pageName,
|
|
|
|
lastModified: s.mtime.getTime(),
|
2022-05-17 09:53:17 +00:00
|
|
|
perm: "rw",
|
2022-03-20 08:56:28 +00:00
|
|
|
};
|
|
|
|
} catch (e) {
|
2022-06-28 12:14:15 +00:00
|
|
|
// console.error("Error while getting page meta", pageName, e);
|
2022-03-20 08:56:28 +00:00
|
|
|
throw Error(`Could not get meta for ${pageName}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
async deletePage(pageName: string): Promise<void> {
|
2022-04-06 13:39:20 +00:00
|
|
|
let localPath = this.pageNameToPath(pageName);
|
2022-03-20 08:56:28 +00:00
|
|
|
await unlink(localPath);
|
|
|
|
}
|
2022-04-08 15:46:09 +00:00
|
|
|
|
|
|
|
async fetchPageList(): Promise<{
|
|
|
|
pages: Set<PageMeta>;
|
|
|
|
nowTimestamp: number;
|
|
|
|
}> {
|
|
|
|
let pages = new Set<PageMeta>();
|
|
|
|
|
|
|
|
const walkPath = async (dir: string) => {
|
|
|
|
let files = await readdir(dir);
|
|
|
|
for (let file of files) {
|
|
|
|
const fullPath = path.join(dir, file);
|
|
|
|
let s = await stat(fullPath);
|
|
|
|
if (s.isDirectory()) {
|
|
|
|
await walkPath(fullPath);
|
|
|
|
} else {
|
|
|
|
if (file.endsWith(".md") || file.endsWith(".json")) {
|
|
|
|
pages.add({
|
|
|
|
name: this.pathToPageName(fullPath),
|
|
|
|
lastModified: s.mtime.getTime(),
|
2022-05-17 09:53:17 +00:00
|
|
|
perm: "rw",
|
2022-04-08 15:46:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
await walkPath(this.rootPath);
|
|
|
|
return {
|
|
|
|
pages: pages,
|
|
|
|
nowTimestamp: Date.now(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-05 09:47:30 +00:00
|
|
|
// Attachments
|
|
|
|
attachmentNameToPath(name: string) {
|
|
|
|
return this.safePath(path.join(this.rootPath, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
pathToAttachmentName(fullPath: string): string {
|
|
|
|
return fullPath.substring(this.rootPath.length + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchAttachmentList(): Promise<{
|
|
|
|
attachments: Set<AttachmentMeta>;
|
|
|
|
nowTimestamp: number;
|
|
|
|
}> {
|
|
|
|
let attachments = new Set<AttachmentMeta>();
|
|
|
|
|
|
|
|
const walkPath = async (dir: string) => {
|
|
|
|
let files = await readdir(dir);
|
|
|
|
for (let file of files) {
|
|
|
|
const fullPath = path.join(dir, file);
|
|
|
|
let s = await stat(fullPath);
|
|
|
|
if (s.isDirectory()) {
|
|
|
|
if (!file.startsWith(".")) {
|
|
|
|
await walkPath(fullPath);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (
|
|
|
|
!file.startsWith(".") &&
|
|
|
|
!file.endsWith(".md") &&
|
|
|
|
!file.endsWith(".json")
|
|
|
|
) {
|
|
|
|
attachments.add({
|
|
|
|
name: this.pathToAttachmentName(fullPath),
|
|
|
|
lastModified: s.mtime.getTime(),
|
|
|
|
size: s.size,
|
|
|
|
contentType: mime.lookup(file) || "application/octet-stream",
|
|
|
|
perm: "rw",
|
|
|
|
} as AttachmentMeta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
await walkPath(this.rootPath);
|
|
|
|
return {
|
|
|
|
attachments,
|
|
|
|
nowTimestamp: Date.now(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async readAttachment(
|
2022-09-05 14:15:01 +00:00
|
|
|
name: string,
|
|
|
|
encoding: AttachmentEncoding
|
|
|
|
): Promise<{ data: AttachmentData; meta: AttachmentMeta }> {
|
2022-09-05 09:47:30 +00:00
|
|
|
const localPath = this.attachmentNameToPath(name);
|
2022-09-05 14:15:01 +00:00
|
|
|
let fileBuffer = await readFile(localPath, {
|
|
|
|
encoding: encoding === "dataurl" ? "base64" : null,
|
|
|
|
});
|
2022-09-05 09:47:30 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const s = await stat(localPath);
|
2022-09-05 14:15:01 +00:00
|
|
|
let contentType = mime.lookup(name) || "application/octet-stream";
|
2022-09-05 09:47:30 +00:00
|
|
|
return {
|
2022-09-05 14:15:01 +00:00
|
|
|
data:
|
|
|
|
encoding === "dataurl"
|
|
|
|
? `data:${contentType};base64,${fileBuffer}`
|
|
|
|
: (fileBuffer as Buffer).buffer,
|
2022-09-05 09:47:30 +00:00
|
|
|
meta: {
|
|
|
|
name: name,
|
|
|
|
lastModified: s.mtime.getTime(),
|
|
|
|
size: s.size,
|
2022-09-05 14:15:01 +00:00
|
|
|
contentType: contentType,
|
2022-09-05 09:47:30 +00:00
|
|
|
perm: "rw",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
// console.error("Error while reading attachment", name, e);
|
|
|
|
throw Error(`Could not read attachment ${name}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAttachmentMeta(name: string): Promise<AttachmentMeta> {
|
|
|
|
const localPath = this.attachmentNameToPath(name);
|
|
|
|
try {
|
|
|
|
const s = await stat(localPath);
|
|
|
|
return {
|
|
|
|
name: name,
|
|
|
|
lastModified: s.mtime.getTime(),
|
|
|
|
size: s.size,
|
|
|
|
contentType: mime.lookup(name) || "application/octet-stream",
|
|
|
|
perm: "rw",
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
// console.error("Error while getting attachment meta", name, e);
|
|
|
|
throw Error(`Could not get meta for ${name}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async writeAttachment(
|
|
|
|
name: string,
|
2022-09-05 14:15:01 +00:00
|
|
|
data: AttachmentData,
|
2022-09-05 09:47:30 +00:00
|
|
|
selfUpdate?: boolean,
|
|
|
|
lastModified?: number
|
|
|
|
): Promise<AttachmentMeta> {
|
|
|
|
let localPath = this.attachmentNameToPath(name);
|
|
|
|
try {
|
|
|
|
// Ensure parent folder exists
|
|
|
|
await mkdir(path.dirname(localPath), { recursive: true });
|
|
|
|
|
|
|
|
// Actually write the file
|
2022-09-05 14:15:01 +00:00
|
|
|
if (typeof data === "string") {
|
|
|
|
await writeFile(localPath, data.split(",")[1], { encoding: "base64" });
|
|
|
|
} else {
|
|
|
|
await writeFile(localPath, Buffer.from(data));
|
|
|
|
}
|
2022-09-05 09:47:30 +00:00
|
|
|
|
|
|
|
if (lastModified) {
|
|
|
|
let d = new Date(lastModified);
|
|
|
|
console.log("Going to set the modified time", d);
|
|
|
|
await utimes(localPath, d, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch new metadata
|
|
|
|
const s = await stat(localPath);
|
|
|
|
return {
|
|
|
|
name: name,
|
|
|
|
lastModified: s.mtime.getTime(),
|
|
|
|
size: s.size,
|
|
|
|
contentType: mime.lookup(name) || "application/octet-stream",
|
|
|
|
perm: "rw",
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error while writing attachment", name, e);
|
|
|
|
throw Error(`Could not write ${name}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteAttachment(name: string): Promise<void> {
|
|
|
|
let localPath = this.attachmentNameToPath(name);
|
|
|
|
await unlink(localPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Plugs
|
2022-04-08 15:46:09 +00:00
|
|
|
invokeFunction(
|
|
|
|
plug: Plug<any>,
|
|
|
|
env: string,
|
|
|
|
name: string,
|
|
|
|
args: any[]
|
|
|
|
): Promise<any> {
|
|
|
|
return plug.invoke(name, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
proxySyscall(plug: Plug<any>, name: string, args: any[]): Promise<any> {
|
|
|
|
return plug.syscall(name, args);
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|