添加filesystem
This commit is contained in:
32
packages/filesystem/zip/rw.ts
Normal file
32
packages/filesystem/zip/rw.ts
Normal file
@ -0,0 +1,32 @@
|
||||
/* eslint-disable max-classes-per-file */
|
||||
/* eslint-disable import/prefer-default-export */
|
||||
import JSZip, { JSZipObject } from "jszip";
|
||||
import { FileReader, FileWriter } from "../filesystem";
|
||||
|
||||
export class ZipFileReader implements FileReader {
|
||||
zipObject: JSZipObject;
|
||||
|
||||
constructor(zipObject: JSZipObject) {
|
||||
this.zipObject = zipObject;
|
||||
}
|
||||
|
||||
read(type?: "string" | "blob"): Promise<string | Blob> {
|
||||
return this.zipObject.async(type || "string");
|
||||
}
|
||||
}
|
||||
|
||||
export class ZipFileWriter implements FileWriter {
|
||||
zip: JSZip;
|
||||
|
||||
path: string;
|
||||
|
||||
constructor(zip: JSZip, path: string) {
|
||||
this.zip = zip;
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
write(content: string): Promise<void> {
|
||||
this.zip.file(this.path, content);
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
68
packages/filesystem/zip/zip.ts
Normal file
68
packages/filesystem/zip/zip.ts
Normal file
@ -0,0 +1,68 @@
|
||||
import JSZip from "jszip";
|
||||
import FileSystem, {
|
||||
File,
|
||||
FileReader,
|
||||
FileWriter,
|
||||
} from "@Pkg/filesystem/filesystem";
|
||||
import { ZipFileReader, ZipFileWriter } from "./rw";
|
||||
|
||||
export default class ZipFileSystem implements FileSystem {
|
||||
zip: JSZip;
|
||||
|
||||
basePath: string;
|
||||
|
||||
// zip为空时,创建一个空的zip
|
||||
constructor(zip?: JSZip, basePath?: string) {
|
||||
this.zip = zip || new JSZip();
|
||||
this.basePath = basePath || "";
|
||||
}
|
||||
|
||||
verify(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
open(info: File): Promise<FileReader> {
|
||||
const path = info.name;
|
||||
const file = this.zip.file(path);
|
||||
if (file) {
|
||||
return Promise.resolve(new ZipFileReader(file));
|
||||
}
|
||||
return Promise.reject(new Error("File not found"));
|
||||
}
|
||||
|
||||
openDir(path: string): Promise<FileSystem> {
|
||||
return Promise.resolve(new ZipFileSystem(this.zip, path));
|
||||
}
|
||||
|
||||
create(path: string): Promise<FileWriter> {
|
||||
return Promise.resolve(new ZipFileWriter(this.zip, path));
|
||||
}
|
||||
|
||||
createDir(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
delete(path: string): Promise<void> {
|
||||
this.zip.remove(path);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
list(): Promise<File[]> {
|
||||
const files: File[] = [];
|
||||
Object.keys(this.zip.files).forEach((key) => {
|
||||
files.push({
|
||||
name: key,
|
||||
path: key,
|
||||
size: 0,
|
||||
digest: "",
|
||||
createtime: this.zip.files[key].date.getTime(),
|
||||
updatetime: this.zip.files[key].date.getTime(),
|
||||
});
|
||||
});
|
||||
return Promise.resolve(files);
|
||||
}
|
||||
|
||||
getDirUrl(): Promise<string> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user