Skip to content

Add support for ZIP file attachments. #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 53 additions & 9 deletions src/fileAttachment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {require as requireDefault} from "d3-require";
import sqlite, {SQLiteDatabaseClient} from "./sqlite.js";
import jszip from "./zip.js";

async function remote_fetch(file) {
const response = await fetch(await file.url());
Expand All @@ -14,15 +15,9 @@ async function dsv(file, delimiter, {array = false, typed = false} = {}) {
: (array ? d3.csvParseRows : d3.csvParse))(text, typed && d3.autoType);
}

class FileAttachment {
constructor(url, name) {
Object.defineProperties(this, {
_url: {value: url},
name: {value: name, enumerable: true}
});
}
async url() {
return (await this._url) + "";
class AbstractFile {
constructor(name) {
Object.defineProperty(this, "name", {value: name, enumerable: true});
}
async blob() {
return (await remote_fetch(this)).blob();
Expand Down Expand Up @@ -62,6 +57,20 @@ class FileAttachment {
const db = new SQL.Database(new Uint8Array(buffer));
return new SQLiteDatabaseClient(db);
}
async zip() {
const [JSZip, buffer] = await Promise.all([jszip(requireDefault), this.arrayBuffer()]);
return new ZipArchive(await JSZip.loadAsync(buffer));
}
}

class FileAttachment extends AbstractFile {
constructor(url, name) {
super(name);
Object.defineProperty(this, "_url", {value: url});
}
async url() {
return (await this._url) + "";
}
}

export function NoFileAttachments(name) {
Expand All @@ -78,3 +87,38 @@ export default function FileAttachments(resolve) {
{prototype: FileAttachment.prototype} // instanceof
);
}

export class ZipArchive {
constructor(archive) {
Object.defineProperty(this, "_", {value: archive});
this.filenames = Object.keys(archive.files).filter(name => !archive.files[name].dir);
}
file(path) {
const object = this._.file(path += "");
if (!object || object.dir) throw new Error(`file not found: ${path}`);
return new ZipArchiveEntry(object);
}
}

class ZipArchiveEntry extends AbstractFile {
constructor(object) {
super(object.name);
Object.defineProperty(this, "_", {value: object});
Object.defineProperty(this, "_url", {writable: true});
}
async url() {
return this._url || (this._url = this.blob().then(URL.createObjectURL));
}
async blob() {
return this._.async("blob");
}
async arrayBuffer() {
return this._.async("arraybuffer");
}
async text() {
return this._.async("text");
}
async json() {
return JSON.parse(await this.text());
}
}
3 changes: 3 additions & 0 deletions src/zip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default async function jszip(require) {
return await require("[email protected]/dist/jszip.min.js");
}