2024-07-30 23:24:17 +08:00
|
|
|
import * as path from "@std/path";
|
|
|
|
import { readAll } from "@std/io/read-all";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { SpacePrimitives } from "./space_primitives.ts";
|
2024-03-16 22:29:24 +08:00
|
|
|
import { mime } from "mimetypes";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { FileMeta } from "../../plug-api/types.ts";
|
2022-03-20 16:56:28 +08:00
|
|
|
|
2022-09-12 20:50:37 +08:00
|
|
|
function lookupContentType(path: string): string {
|
2022-10-10 20:50:21 +08:00
|
|
|
return mime.getType(path) || "application/octet-stream";
|
2022-09-12 20:50:37 +08:00
|
|
|
}
|
|
|
|
|
2023-01-14 19:04:51 +08:00
|
|
|
function normalizeForwardSlashPath(path: string) {
|
|
|
|
return path.replaceAll("\\", "/");
|
|
|
|
}
|
|
|
|
|
2023-01-13 22:41:29 +08:00
|
|
|
const excludedFiles = ["data.db", "data.db-journal", "sync.json"];
|
|
|
|
|
2022-04-08 23:46:09 +08:00
|
|
|
export class DiskSpacePrimitives implements SpacePrimitives {
|
2022-03-20 16:56:28 +08:00
|
|
|
rootPath: string;
|
2024-08-04 17:28:55 +08:00
|
|
|
fileListCache: FileMeta[] = [];
|
|
|
|
fileListCacheTime = 0;
|
|
|
|
fileListCacheUpdating: AbortController | null = null;
|
2022-03-20 16:56:28 +08:00
|
|
|
|
2023-05-29 15:53:49 +08:00
|
|
|
constructor(rootPath: string) {
|
2022-10-10 20:50:21 +08:00
|
|
|
this.rootPath = Deno.realPathSync(rootPath);
|
2022-04-06 21:39:20 +08:00
|
|
|
}
|
|
|
|
|
2022-04-30 00:54:27 +08:00
|
|
|
safePath(p: string): string {
|
2022-10-10 20:50:21 +08:00
|
|
|
const realPath = path.resolve(p);
|
2022-04-30 00:54:27 +08:00
|
|
|
if (!realPath.startsWith(this.rootPath)) {
|
|
|
|
throw Error(`Path ${p} is not in the space`);
|
|
|
|
}
|
|
|
|
return realPath;
|
|
|
|
}
|
|
|
|
|
2022-09-12 20:50:37 +08:00
|
|
|
filenameToPath(pageName: string) {
|
|
|
|
return this.safePath(path.join(this.rootPath, pageName));
|
2022-04-06 21:39:20 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 20:50:37 +08:00
|
|
|
pathToFilename(fullPath: string): string {
|
|
|
|
return fullPath.substring(this.rootPath.length + 1);
|
2022-03-20 16:56:28 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 20:50:37 +08:00
|
|
|
async readFile(
|
|
|
|
name: string,
|
2023-05-24 02:53:53 +08:00
|
|
|
): Promise<{ data: Uint8Array; meta: FileMeta }> {
|
2022-09-12 20:50:37 +08:00
|
|
|
const localPath = this.filenameToPath(name);
|
2022-03-20 16:56:28 +08:00
|
|
|
try {
|
2022-10-10 20:50:21 +08:00
|
|
|
const s = await Deno.stat(localPath);
|
|
|
|
const contentType = lookupContentType(name);
|
2023-05-24 02:53:53 +08:00
|
|
|
|
|
|
|
const f = await Deno.open(localPath, { read: true });
|
|
|
|
const data = await readAll(f);
|
2024-01-28 21:13:37 +08:00
|
|
|
f.close();
|
2023-05-24 02:53:53 +08:00
|
|
|
|
2022-03-20 16:56:28 +08:00
|
|
|
return {
|
2022-09-12 20:50:37 +08:00
|
|
|
data,
|
2022-03-20 16:56:28 +08:00
|
|
|
meta: {
|
2022-09-12 20:50:37 +08:00
|
|
|
name: name,
|
2023-11-13 17:32:40 +08:00
|
|
|
created: s.birthtime?.getTime() || s.mtime?.getTime() || 0,
|
|
|
|
lastModified: s.mtime?.getTime() || 0,
|
2022-05-17 17:53:17 +08:00
|
|
|
perm: "rw",
|
2022-09-12 20:50:37 +08:00
|
|
|
size: s.size,
|
|
|
|
contentType: contentType,
|
2022-03-20 16:56:28 +08:00
|
|
|
},
|
|
|
|
};
|
2022-10-16 01:02:56 +08:00
|
|
|
} catch {
|
2022-10-10 20:50:21 +08:00
|
|
|
// console.error("Error while reading file", name, e);
|
2023-06-14 02:47:05 +08:00
|
|
|
throw Error("Not found");
|
2022-03-20 16:56:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-12 20:50:37 +08:00
|
|
|
async writeFile(
|
|
|
|
name: string,
|
2023-05-24 02:53:53 +08:00
|
|
|
data: Uint8Array,
|
|
|
|
_selfUpdate?: boolean,
|
2023-07-02 17:25:32 +08:00
|
|
|
meta?: FileMeta,
|
2022-09-12 20:50:37 +08:00
|
|
|
): Promise<FileMeta> {
|
2022-10-11 00:19:08 +08:00
|
|
|
const localPath = this.filenameToPath(name);
|
2022-03-20 16:56:28 +08:00
|
|
|
try {
|
2022-03-23 22:41:12 +08:00
|
|
|
// Ensure parent folder exists
|
2022-10-10 20:50:21 +08:00
|
|
|
await Deno.mkdir(path.dirname(localPath), { recursive: true });
|
2022-03-23 22:41:12 +08:00
|
|
|
|
2023-05-24 02:53:53 +08:00
|
|
|
const file = await Deno.open(localPath, {
|
|
|
|
write: true,
|
|
|
|
create: true,
|
|
|
|
truncate: true,
|
|
|
|
});
|
|
|
|
|
2022-03-23 22:41:12 +08:00
|
|
|
// Actually write the file
|
2024-01-28 21:37:54 +08:00
|
|
|
await file.write(data);
|
2023-05-24 02:53:53 +08:00
|
|
|
|
2023-07-02 17:25:32 +08:00
|
|
|
if (meta?.lastModified) {
|
|
|
|
// console.log("Seting mtime to", new Date(meta.lastModified));
|
2024-01-28 21:37:54 +08:00
|
|
|
await file.utime(new Date(), new Date(meta.lastModified));
|
2022-04-06 21:39:20 +08:00
|
|
|
}
|
2023-05-24 02:53:53 +08:00
|
|
|
file.close();
|
2022-09-12 20:50:37 +08:00
|
|
|
|
2024-08-04 17:28:55 +08:00
|
|
|
// Invalidate cache and trigger an update
|
|
|
|
this.fileListCache = [];
|
|
|
|
this.fileListCacheTime = 0;
|
|
|
|
this.updateCacheInBackground();
|
|
|
|
|
2022-03-23 22:41:12 +08:00
|
|
|
// Fetch new metadata
|
2023-10-03 20:16:33 +08:00
|
|
|
return this.getFileMeta(name);
|
2022-03-20 16:56:28 +08:00
|
|
|
} catch (e) {
|
2022-09-12 20:50:37 +08:00
|
|
|
console.error("Error while writing file", name, e);
|
|
|
|
throw Error(`Could not write ${name}`);
|
2022-03-20 16:56:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-12 20:50:37 +08:00
|
|
|
async getFileMeta(name: string): Promise<FileMeta> {
|
2022-10-10 20:50:21 +08:00
|
|
|
const localPath = this.filenameToPath(name);
|
2022-03-20 16:56:28 +08:00
|
|
|
try {
|
2022-10-10 20:50:21 +08:00
|
|
|
const s = await Deno.stat(localPath);
|
2022-03-20 16:56:28 +08:00
|
|
|
return {
|
2022-09-12 20:50:37 +08:00
|
|
|
name: name,
|
|
|
|
size: s.size,
|
|
|
|
contentType: lookupContentType(name),
|
2023-11-13 17:32:40 +08:00
|
|
|
created: s.birthtime?.getTime() || s.mtime?.getTime() || 0,
|
|
|
|
lastModified: s.mtime?.getTime() || 0,
|
2022-05-17 17:53:17 +08:00
|
|
|
perm: "rw",
|
2022-03-20 16:56:28 +08:00
|
|
|
};
|
2022-10-16 01:02:56 +08:00
|
|
|
} catch {
|
2022-06-28 20:14:15 +08:00
|
|
|
// console.error("Error while getting page meta", pageName, e);
|
2022-09-12 20:50:37 +08:00
|
|
|
throw Error(`Could not get meta for ${name}`);
|
2022-03-20 16:56:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-12 20:50:37 +08:00
|
|
|
async deleteFile(name: string): Promise<void> {
|
2022-10-10 20:50:21 +08:00
|
|
|
const localPath = this.filenameToPath(name);
|
|
|
|
await Deno.remove(localPath);
|
2024-08-04 17:28:55 +08:00
|
|
|
|
|
|
|
// Invalidate cache and trigger an update
|
|
|
|
this.fileListCache = [];
|
|
|
|
this.fileListCacheTime = 0;
|
|
|
|
this.updateCacheInBackground();
|
2022-03-20 16:56:28 +08:00
|
|
|
}
|
2022-04-08 23:46:09 +08:00
|
|
|
|
2022-09-12 20:50:37 +08:00
|
|
|
async fetchFileList(): Promise<FileMeta[]> {
|
2024-08-04 17:28:55 +08:00
|
|
|
// console.log("Fetching file list");
|
|
|
|
const startTime = performance.now();
|
|
|
|
|
|
|
|
// If the file list cache is less than 60 seconds old, return it
|
|
|
|
if (
|
|
|
|
this.fileListCache.length > 0 &&
|
|
|
|
startTime - this.fileListCacheTime < 60000
|
|
|
|
) {
|
|
|
|
// Trigger a background sync, but return the cached list while the cache is being updated
|
|
|
|
this.updateCacheInBackground();
|
|
|
|
return this.fileListCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise get the file list and wait for it
|
|
|
|
const allFiles: FileMeta[] = await this.getFileList();
|
|
|
|
|
|
|
|
const endTime = performance.now();
|
|
|
|
console.info("Fetched uncached file list in", endTime - startTime, "ms");
|
|
|
|
|
|
|
|
this.fileListCache = allFiles;
|
|
|
|
this.fileListCacheTime = startTime;
|
|
|
|
|
|
|
|
return allFiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getFileList(): Promise<FileMeta[]> {
|
2022-10-19 17:30:22 +08:00
|
|
|
const allFiles: FileMeta[] = [];
|
2024-02-25 21:17:23 +08:00
|
|
|
for await (const file of walkPreserveSymlinks(this.rootPath)) {
|
2024-08-04 17:28:55 +08:00
|
|
|
// Uncomment to simulate a slow-ish disk
|
|
|
|
// await new Promise((resolve) => setTimeout(resolve, 1));
|
2022-10-19 17:30:22 +08:00
|
|
|
const fullPath = file.path;
|
2022-10-28 22:17:40 +08:00
|
|
|
try {
|
|
|
|
const s = await Deno.stat(fullPath);
|
2023-01-13 22:41:29 +08:00
|
|
|
const name = fullPath.substring(this.rootPath.length + 1);
|
|
|
|
if (excludedFiles.includes(name)) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-10-28 22:17:40 +08:00
|
|
|
allFiles.push({
|
2023-01-14 19:04:51 +08:00
|
|
|
name: normalizeForwardSlashPath(name),
|
2023-11-13 17:32:40 +08:00
|
|
|
created: s.birthtime?.getTime() || s.mtime?.getTime() || 0,
|
|
|
|
lastModified: s.mtime?.getTime() || 0,
|
2022-10-28 22:17:40 +08:00
|
|
|
contentType: mime.getType(fullPath) || "application/octet-stream",
|
|
|
|
size: s.size,
|
|
|
|
perm: "rw",
|
|
|
|
});
|
|
|
|
} catch (e: any) {
|
|
|
|
if (e instanceof Deno.errors.NotFound) {
|
|
|
|
// Ignore, temporariy file already deleted by the time we got here
|
|
|
|
} else {
|
|
|
|
console.error("Failed to stat", fullPath, e);
|
|
|
|
}
|
|
|
|
}
|
2022-10-19 17:30:22 +08:00
|
|
|
}
|
|
|
|
return allFiles;
|
2022-09-05 17:47:30 +08:00
|
|
|
}
|
2024-08-04 17:28:55 +08:00
|
|
|
|
|
|
|
private updateCacheInBackground() {
|
|
|
|
if (this.fileListCacheUpdating) {
|
|
|
|
// Cancel the existing background update, so we never return stale data
|
|
|
|
this.fileListCacheUpdating.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
const abortController = new AbortController();
|
|
|
|
this.fileListCacheUpdating = abortController;
|
|
|
|
|
|
|
|
const updatePromise = this.getFileList().then((allFiles) => {
|
|
|
|
if (abortController.signal.aborted) return;
|
|
|
|
|
|
|
|
this.fileListCache = allFiles;
|
|
|
|
this.fileListCacheTime = performance.now();
|
2024-08-13 02:12:28 +08:00
|
|
|
// console.info(
|
|
|
|
// "Updated file list cache in background:",
|
|
|
|
// allFiles.length,
|
|
|
|
// "files found",
|
|
|
|
// );
|
2024-08-04 17:28:55 +08:00
|
|
|
}).catch((error) => {
|
|
|
|
if (abortController.signal.aborted) return;
|
|
|
|
|
|
|
|
if (error.name !== "AbortError") {
|
|
|
|
console.error("Error updating file list cache in background:", error);
|
|
|
|
}
|
|
|
|
}).finally(() => {
|
|
|
|
if (this.fileListCacheUpdating === abortController) {
|
|
|
|
this.fileListCacheUpdating = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return updatePromise;
|
|
|
|
}
|
2022-03-20 16:56:28 +08:00
|
|
|
}
|
2022-11-29 15:50:09 +08:00
|
|
|
|
2024-02-25 21:17:23 +08:00
|
|
|
async function* walkPreserveSymlinks(
|
|
|
|
dirPath: string,
|
|
|
|
): AsyncIterableIterator<{ path: string; entry: Deno.DirEntry }> {
|
|
|
|
for await (const dirEntry of Deno.readDir(dirPath)) {
|
|
|
|
const fullPath = `${dirPath}/${dirEntry.name}`;
|
|
|
|
if (dirEntry.name.startsWith(".")) {
|
|
|
|
// Skip hidden files and folders
|
|
|
|
continue;
|
|
|
|
}
|
2024-02-26 23:29:13 +08:00
|
|
|
|
|
|
|
let entry: Deno.DirEntry | Deno.FileInfo = dirEntry;
|
|
|
|
|
|
|
|
if (dirEntry.isSymlink) {
|
|
|
|
try {
|
|
|
|
entry = await Deno.stat(fullPath);
|
2024-10-10 18:52:28 +08:00
|
|
|
} catch (e: any) {
|
2024-02-26 23:29:13 +08:00
|
|
|
console.error("Error reading symlink", fullPath, e.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.isFile) {
|
2024-02-25 21:17:23 +08:00
|
|
|
yield { path: fullPath, entry: dirEntry };
|
|
|
|
}
|
|
|
|
|
2024-02-26 23:29:13 +08:00
|
|
|
if (entry.isDirectory) {
|
2024-02-25 21:17:23 +08:00
|
|
|
// If it's a directory or a symlink, recurse into it
|
|
|
|
yield* walkPreserveSymlinks(fullPath);
|
|
|
|
}
|
|
|
|
}
|
2022-11-29 15:50:09 +08:00
|
|
|
}
|