chore: Refactoring event processors and service architecture (#2495)
This commit is contained in:
42
server/queues/processors/imports.js
Normal file
42
server/queues/processors/imports.js
Normal file
@ -0,0 +1,42 @@
|
||||
// @flow
|
||||
import fs from "fs";
|
||||
import os from "os";
|
||||
import File from "formidable/lib/file";
|
||||
import collectionImporter from "../../commands/collectionImporter";
|
||||
import { Attachment, User } from "../../models";
|
||||
import type { Event } from "../../types";
|
||||
|
||||
export default class ImportsProcessor {
|
||||
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,
|
||||
});
|
||||
|
||||
await attachment.destroy();
|
||||
|
||||
return;
|
||||
}
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user