refactor
This commit is contained in:
43
server/commands/attachmentCreator.js
Normal file
43
server/commands/attachmentCreator.js
Normal file
@ -0,0 +1,43 @@
|
||||
// @flow
|
||||
import uuid from "uuid";
|
||||
import { Attachment, Event, User } from "../models";
|
||||
import { uploadToS3FromBuffer } from "../utils/s3";
|
||||
|
||||
export default async function attachmentCreator({
|
||||
name,
|
||||
type,
|
||||
buffer,
|
||||
user,
|
||||
ip,
|
||||
}: {
|
||||
name: string,
|
||||
type: string,
|
||||
buffer: Buffer,
|
||||
user: User,
|
||||
ip: string,
|
||||
}) {
|
||||
const key = `uploads/${user.id}/${uuid.v4()}/${name}`;
|
||||
const acl = process.env.AWS_S3_ACL || "private";
|
||||
const url = await uploadToS3FromBuffer(buffer, type, key, acl);
|
||||
|
||||
const attachment = await Attachment.create({
|
||||
key,
|
||||
acl,
|
||||
url,
|
||||
size: buffer.length,
|
||||
contentType: type,
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
await Event.create({
|
||||
name: "attachments.create",
|
||||
data: { name },
|
||||
modelId: attachment.id,
|
||||
teamId: user.teamId,
|
||||
actorId: user.id,
|
||||
ip,
|
||||
});
|
||||
|
||||
return attachment;
|
||||
}
|
Reference in New Issue
Block a user