This commit is contained in:
Tom Moor
2020-12-19 23:23:37 -08:00
parent 7f5a7d7df7
commit 938f6ba8c5
3 changed files with 41 additions and 16 deletions

View File

@ -1,5 +1,6 @@
// @flow // @flow
import fs from "fs"; import fs from "fs";
import path from "path";
import File from "formidable/lib/file"; import File from "formidable/lib/file";
import JSZip from "jszip"; import JSZip from "jszip";
import { Collection, User } from "../models"; import { Collection, User } from "../models";
@ -17,7 +18,7 @@ export default async function documentBatchImporter({
type: "outline", type: "outline",
ip: string, ip: string,
}) { }) {
const zipData = await fs.promises.readFile(file.path, "utf8"); const zipData = await fs.promises.readFile(file.path);
const zip = await JSZip.loadAsync(zipData); const zip = await JSZip.loadAsync(zipData);
async function ingestDocuments( async function ingestDocuments(
@ -27,14 +28,25 @@ export default async function documentBatchImporter({
) { ) {
const documents = []; const documents = [];
let items = [];
zip.forEach(async function (path, item) {
items.push([path, item]);
});
// TODO: attachments // TODO: attachments
// 2 passes, one for documents and then second for their nested documents // 2 passes, one for documents and then second for their nested documents
zip.forEach(async function (filePath, item) { for (const [_, item] of items) {
if (item.dir) return; if (item.dir) return;
const fileData = await item.async("blob"); const content = await item.async("string");
const file = new File([fileData], item.name); const name = path.basename(item.name);
await fs.promises.writeFile(`/tmp/${name}`, content);
const file = new File({
name,
type: "text/markdown",
path: `/tmp/${name}`,
});
const { text, title } = await documentImporter({ const { text, title } = await documentImporter({
file, file,
@ -54,28 +66,41 @@ export default async function documentBatchImporter({
// Keep track of which documents have been created // Keep track of which documents have been created
documents.push(document); documents.push(document);
}); }
for (const [filePath, item] of folders) {
const name = path.basename(item.name);
zip.forEach(async function (filePath, item) {
// treat items in here as nested documents // treat items in here as nested documents
if (!item.dir) return; if (!item.dir) return;
if (item.name === "uploads") return; if (name === "uploads") return;
const document = documents.find((doc) => doc.title === item.name); const document = documents.find((doc) => doc.title === name);
if (!document) { if (!document) {
console.log( console.log(
`Couldn't find a matching parent document for folder ${item.name}` `Couldn't find a matching parent document for folder ${name}`
); );
return; return;
} }
// ensure document is created first, get parentDocumentId // ensure document is created first, get parentDocumentId
await ingestDocuments(zip.folder(filePath), collectionId, document.id); await ingestDocuments(zip.folder(filePath), collectionId, document.id);
}); }
} }
zip.forEach(async function (folderPath, item) { let folders = [];
// all top level items must be directories representing collections zip.forEach(async function (path, item) {
folders.push([path, item]);
});
for (const [folderPath, item] of folders) {
const name = path.basename(item.name);
if (folderPath.startsWith("__MACOSX") || folderPath.endsWith(".DS_Store")) {
continue;
}
// all top level items must be directories representing collections
console.log("iterating over", folderPath); console.log("iterating over", folderPath);
// treat this as a collection // treat this as a collection
@ -84,16 +109,15 @@ export default async function documentBatchImporter({
const [collection, isCreated] = await Collection.findOrCreate({ const [collection, isCreated] = await Collection.findOrCreate({
where: { where: {
teamId: user.teamId, teamId: user.teamId,
name: item.name, name,
}, },
defaults: { defaults: {
private: false, private: false,
}, },
}); });
console.log(`Collection ${item.name} ${isCreated ? "created" : "found"}`); console.log(`Collection ${name} ${isCreated ? "created" : "found"}`);
await ingestDocuments(zip.folder(folderPath), collection.id); await ingestDocuments(zip.folder(folderPath), collection.id);
} }
}); }
} }

View File

@ -20,6 +20,7 @@ describe("documentBatchImporter", () => {
type: "application/zip", type: "application/zip",
path: path.resolve(__dirname, "..", "test", "fixtures", name), path: path.resolve(__dirname, "..", "test", "fixtures", name),
}); });
console.log(file);
await documentBatchImporter({ await documentBatchImporter({
type: "outline", type: "outline",

BIN
server/test/fixtures/outline.zip vendored Normal file

Binary file not shown.