This commit is contained in:
Zef Hemel
2023-05-29 09:53:49 +02:00
parent 50651dc185
commit c6a45be4bb
10 changed files with 129 additions and 47 deletions
+1 -12
View File
@@ -16,14 +16,10 @@ function normalizeForwardSlashPath(path: string) {
const excludedFiles = ["data.db", "data.db-journal", "sync.json"];
export type DiskSpaceOptions = {
maxFileSizeMB?: number;
};
export class DiskSpacePrimitives implements SpacePrimitives {
rootPath: string;
constructor(rootPath: string, private options: DiskSpaceOptions = {}) {
constructor(rootPath: string) {
this.rootPath = Deno.realPathSync(rootPath);
}
@@ -150,13 +146,6 @@ export class DiskSpacePrimitives implements SpacePrimitives {
const fullPath = file.path;
try {
const s = await Deno.stat(fullPath);
// Don't list file exceeding the maximum file size
if (
this.options.maxFileSizeMB &&
s.size / (1024 * 1024) > this.options.maxFileSizeMB
) {
continue;
}
const name = fullPath.substring(this.rootPath.length + 1);
if (excludedFiles.includes(name)) {
continue;
@@ -0,0 +1,35 @@
import { FileMeta } from "../types.ts";
import { SpacePrimitives } from "./space_primitives.ts";
export class FilteredSpacePrimitives implements SpacePrimitives {
constructor(
private wrapped: SpacePrimitives,
private filterFn: (name: FileMeta) => boolean,
private onFetchList?: () => Promise<void>,
) {
}
async fetchFileList(): Promise<FileMeta[]> {
if (this.onFetchList) {
await this.onFetchList();
}
return (await this.wrapped.fetchFileList()).filter(this.filterFn);
}
readFile(name: string): Promise<{ data: Uint8Array; meta: FileMeta }> {
return this.wrapped.readFile(name);
}
getFileMeta(name: string): Promise<FileMeta> {
return this.wrapped.getFileMeta(name);
}
writeFile(
name: string,
data: Uint8Array,
selfUpdate?: boolean | undefined,
lastModified?: number | undefined,
): Promise<FileMeta> {
return this.wrapped.writeFile(name, data, selfUpdate, lastModified);
}
deleteFile(name: string): Promise<void> {
return this.wrapped.deleteFile(name);
}
}