diff --git a/server/api/collections.js b/server/api/collections.js index 121ad859..bb3997a1 100644 --- a/server/api/collections.js +++ b/server/api/collections.js @@ -1,9 +1,6 @@ // @flow import fs from "fs"; -import os from "os"; -import File from "formidable/lib/file"; import Router from "koa-router"; -import collectionImporter from "../commands/collectionImporter"; import { ValidationError } from "../errors"; import { exportCollections } from "../logistics"; import auth from "../middlewares/authentication"; @@ -113,34 +110,17 @@ router.post("collections.import", auth(), async (ctx) => { const attachment = await Attachment.findByPk(attachmentId); authorize(user, "read", attachment); - const buffer = await attachment.buffer; - const tmpDir = os.tmpdir(); - const tmpFilePath = `${tmpDir}/upload-${attachmentId}`; - - await fs.promises.writeFile(tmpFilePath, buffer); - const file = new File({ - name: attachment.name, - type: attachment.type, - path: tmpFilePath, - }); - - const { documents, attachments, collections } = await collectionImporter({ - file, - user, - type, + await Event.create({ + name: "collections.import", + modelId: attachmentId, + teamId: user.teamId, + actorId: user.id, + data: { type }, ip: ctx.request.ip, }); ctx.body = { - data: { - attachmentCount: attachments.length, - documentCount: documents.length, - collectionCount: collections.length, - collections: collections.map((collection) => - presentCollection(collection) - ), - }, - policies: presentPolicies(user, collections), + success: true, }; }); diff --git a/server/commands/collectionImporter.js b/server/commands/collectionImporter.js index 58e6e1da..d073213b 100644 --- a/server/commands/collectionImporter.js +++ b/server/commands/collectionImporter.js @@ -57,7 +57,7 @@ export default async function collectionImporter({ name: item.name, }, defaults: { - creatorId: user.id, + createdById: user.id, private: false, }, }); @@ -69,7 +69,7 @@ export default async function collectionImporter({ const name = `${item.name} (Imported)`; collection = await Collection.create({ teamId: user.teamId, - creatorId: user.id, + createdById: user.id, name, private: false, }); diff --git a/server/events.js b/server/events.js index 0d671f5d..a3d72d9e 100644 --- a/server/events.js +++ b/server/events.js @@ -101,6 +101,15 @@ export type RevisionEvent = { teamId: string, }; +export type CollectionImportEvent = { + name: "collections.import", + modelId: string, + teamId: string, + actorId: string, + data: { type: "outline" }, + ip: string, +}; + export type CollectionEvent = | { name: | "collections.create" // eslint-disable-line @@ -167,6 +176,7 @@ export type Event = | UserEvent | DocumentEvent | CollectionEvent + | CollectionImportEvent | IntegrationEvent | GroupEvent | RevisionEvent diff --git a/server/services/importer.js b/server/services/importer.js new file mode 100644 index 00000000..3d4f9605 --- /dev/null +++ b/server/services/importer.js @@ -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: + } + } +}