Skip to content

Commit

Permalink
iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
tommoor committed Oct 26, 2023
1 parent 1eae330 commit 65be484
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 58 deletions.
73 changes: 22 additions & 51 deletions server/commands/attachmentCreator.ts
@@ -1,18 +1,17 @@
import { Transaction } from "sequelize";
import { v4 as uuidv4 } from "uuid";
import { AttachmentPreset } from "@shared/types";
import { Attachment, Event, User } from "@server/models";
import { Attachment, User } from "@server/models";
import AttachmentHelper from "@server/models/helpers/AttachmentHelper";
import FileStorage from "@server/storage/files";
import { APIContext } from "@server/types";

type BaseProps = {
id?: string;
name: string;
user: User;
source?: "import";
preset: AttachmentPreset;
ip?: string;
transaction?: Transaction;
ctx: APIContext;
};

type UrlProps = BaseProps & {
Expand All @@ -32,8 +31,7 @@ export default async function attachmentCreator({
user,
source,
preset,
ip,
transaction,
ctx,
...rest
}: Props): Promise<Attachment | undefined> {
const acl = AttachmentHelper.presetToAcl(preset);
Expand All @@ -53,20 +51,15 @@ export default async function attachmentCreator({
if (!res) {
return;
}
attachment = await Attachment.create(
{
id,
key,
acl,
size: res.contentLength,
contentType: res.contentType,
teamId: user.teamId,
userId: user.id,
},
{
transaction,
}
);
attachment = await Attachment.createWithCtx(ctx, {
id,
key,
acl,
size: res.contentLength,
contentType: res.contentType,
teamId: user.teamId,
userId: user.id,
});
} else {
const { buffer, type } = rest;
await FileStorage.store({
Expand All @@ -77,38 +70,16 @@ export default async function attachmentCreator({
acl,
});

attachment = await Attachment.create(
{
id,
key,
acl,
size: buffer.length,
contentType: type,
teamId: user.teamId,
userId: user.id,
},
{
transaction,
}
);
}

await Event.create(
{
name: "attachments.create",
data: {
name,
source,
},
modelId: attachment.id,
attachment = await Attachment.createWithCtx(ctx, {
id,
key,
acl,
size: buffer.length,
contentType: type,
teamId: user.teamId,
actorId: user.id,
ip,
},
{
transaction,
}
);
userId: user.id,
});
}

return attachment;
}
10 changes: 8 additions & 2 deletions server/models/helpers/DocumentHelper.tsx
Expand Up @@ -21,6 +21,7 @@ import { parser, schema } from "@server/editor";
import { trace } from "@server/logging/tracing";
import { Document, Revision, User } from "@server/models";
import FileStorage from "@server/storage/files";
import { APIContext } from "@server/types";
import diff from "@server/utils/diff";
import parseAttachmentIds from "@server/utils/parseAttachmentIds";
import parseImages from "@server/utils/parseImages";
Expand Down Expand Up @@ -418,8 +419,13 @@ export default class DocumentHelper {
url: image.src,
preset: AttachmentPreset.DocumentAttachment,
user,
ip,
transaction,
ctx: {
context: {
ip,
transaction,
auth: { user },
},
} as APIContext,
});

if (attachment) {
Expand Down
10 changes: 8 additions & 2 deletions server/queues/tasks/ImportTask.ts
Expand Up @@ -20,6 +20,7 @@ import {
Attachment,
} from "@server/models";
import { sequelize } from "@server/storage/database";
import { APIContext } from "@server/types";
import BaseTask, { TaskPriority } from "./BaseTask";

type Props = {
Expand Down Expand Up @@ -256,8 +257,13 @@ export default abstract class ImportTask extends BaseTask<Props> {
type: item.mimeType,
buffer: await item.buffer(),
user,
ip,
transaction,
ctx: {
context: {
ip,
transaction,
auth: { user },
},
} as APIContext,
});
if (attachment) {
attachments.set(item.id, attachment);
Expand Down
6 changes: 3 additions & 3 deletions server/types.ts
Expand Up @@ -23,8 +23,8 @@ export type AuthenticationResult = AccountProvisionerResult & {

export type Authentication = {
user: User;
token: string;
type: AuthenticationType;
token?: string;
type?: AuthenticationType;
};

export type Pagination = {
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface APIContext<ReqT = BaseReq, ResT = BaseRes>
context: {
transaction?: Transaction;
auth: Authentication;
ip: string;
ip?: string;
};
}

Expand Down

0 comments on commit 65be484

Please sign in to comment.