fix: Collection creator not written (bad merge from refactor while this branch has been open)

refactor: Move processing to async queue now that file can be loaded from external storage
This commit is contained in:
Tom Moor
2021-02-18 22:55:29 -08:00
parent f4c871bb62
commit c5f9412ac0
4 changed files with 59 additions and 29 deletions

View File

@ -1,9 +1,6 @@
// @flow // @flow
import fs from "fs"; import fs from "fs";
import os from "os";
import File from "formidable/lib/file";
import Router from "koa-router"; import Router from "koa-router";
import collectionImporter from "../commands/collectionImporter";
import { ValidationError } from "../errors"; import { ValidationError } from "../errors";
import { exportCollections } from "../logistics"; import { exportCollections } from "../logistics";
import auth from "../middlewares/authentication"; import auth from "../middlewares/authentication";
@ -113,34 +110,17 @@ router.post("collections.import", auth(), async (ctx) => {
const attachment = await Attachment.findByPk(attachmentId); const attachment = await Attachment.findByPk(attachmentId);
authorize(user, "read", attachment); authorize(user, "read", attachment);
const buffer = await attachment.buffer; await Event.create({
const tmpDir = os.tmpdir(); name: "collections.import",
const tmpFilePath = `${tmpDir}/upload-${attachmentId}`; modelId: attachmentId,
teamId: user.teamId,
await fs.promises.writeFile(tmpFilePath, buffer); actorId: user.id,
const file = new File({ data: { type },
name: attachment.name,
type: attachment.type,
path: tmpFilePath,
});
const { documents, attachments, collections } = await collectionImporter({
file,
user,
type,
ip: ctx.request.ip, ip: ctx.request.ip,
}); });
ctx.body = { ctx.body = {
data: { success: true,
attachmentCount: attachments.length,
documentCount: documents.length,
collectionCount: collections.length,
collections: collections.map((collection) =>
presentCollection(collection)
),
},
policies: presentPolicies(user, collections),
}; };
}); });

View File

@ -57,7 +57,7 @@ export default async function collectionImporter({
name: item.name, name: item.name,
}, },
defaults: { defaults: {
creatorId: user.id, createdById: user.id,
private: false, private: false,
}, },
}); });
@ -69,7 +69,7 @@ export default async function collectionImporter({
const name = `${item.name} (Imported)`; const name = `${item.name} (Imported)`;
collection = await Collection.create({ collection = await Collection.create({
teamId: user.teamId, teamId: user.teamId,
creatorId: user.id, createdById: user.id,
name, name,
private: false, private: false,
}); });

View File

@ -101,6 +101,15 @@ export type RevisionEvent = {
teamId: string, teamId: string,
}; };
export type CollectionImportEvent = {
name: "collections.import",
modelId: string,
teamId: string,
actorId: string,
data: { type: "outline" },
ip: string,
};
export type CollectionEvent = export type CollectionEvent =
| { | {
name: | "collections.create" // eslint-disable-line name: | "collections.create" // eslint-disable-line
@ -167,6 +176,7 @@ export type Event =
| UserEvent | UserEvent
| DocumentEvent | DocumentEvent
| CollectionEvent | CollectionEvent
| CollectionImportEvent
| IntegrationEvent | IntegrationEvent
| GroupEvent | GroupEvent
| RevisionEvent | RevisionEvent

View File

@ -0,0 +1,40 @@
// @flow
import fs from "fs";
import os from "os";
import File from "formidable/lib/file";
import collectionImporter from "../commands/collectionImporter";
import type { Event } from "../events";
import { Attachment, User } from "../models";
export default class Importer {
async on(event: Event) {
switch (event.name) {
case "collections.import": {
const { type } = event.data;
const attachment = await Attachment.findByPk(event.modelId);
const user = await User.findByPk(event.actorId);
const buffer = await attachment.buffer;
const tmpDir = os.tmpdir();
const tmpFilePath = `${tmpDir}/upload-${event.modelId}`;
await fs.promises.writeFile(tmpFilePath, buffer);
const file = new File({
name: attachment.name,
type: attachment.type,
path: tmpFilePath,
});
await collectionImporter({
file,
user,
type,
ip: event.ip,
});
return;
}
default:
}
}
}