Skip to content

Commit 07c364c

Browse files
UBERF-11423: Fix attachments in emails (#9166)
Signed-off-by: Artem Savchenko <[email protected]>
1 parent cd1a25b commit 07c364c

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

common/config/rush/pnpm-lock.yaml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

services/mail/mail-common/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"kafkajs": "^2.2.4",
6464
"sanitize-html": "^2.15.0",
6565
"turndown": "^7.2.0",
66-
"uuid": "^8.3.2"
66+
"uuid": "^8.3.2",
67+
"image-size": "^1.1.1"
6768
}
6869
}

services/mail/mail-common/src/message.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import { generateMessageId } from '@hcengineering/communication-shared'
4444

4545
import { BaseConfig, type Attachment } from './types'
4646
import { EmailMessage, MailRecipient, MessageData } from './types'
47-
import { getMdContent } from './utils'
47+
import { getBlobMetadata, getMdContent } from './utils'
4848
import { PersonCacheFactory } from './person'
4949
import { PersonSpacesCacheFactory } from './personSpaces'
5050
import { ChannelCache, ChannelCacheFactory } from './channel'
@@ -252,11 +252,11 @@ async function saveMessageToSpaces (
252252
}
253253

254254
const messageId = await createMailMessage(producer, config, messageData, threadId)
255+
await createFiles(ctx, producer, config, attachments, messageData, threadId, messageId)
255256
if (!isReply) {
256257
await addCollaborators(producer, config, messageData, threadId)
257258
await createMailThread(producer, config, messageData, messageId)
258259
}
259-
await createFiles(producer, config, attachments, messageData, threadId, messageId)
260260

261261
await threadLookup.setThreadId(mailId, space._id, threadId)
262262
})
@@ -305,6 +305,7 @@ async function createMailMessage (
305305
}
306306

307307
async function createFiles (
308+
ctx: MeasureContext,
308309
producer: Producer,
309310
config: BaseConfig,
310311
attachments: Attachment[],
@@ -313,20 +314,21 @@ async function createFiles (
313314
messageId: MessageID
314315
): Promise<void> {
315316
const fileData: Buffer[] = attachments.map((a) => {
316-
const creeateFileEvent: CreateFileEvent = {
317+
const createFileEvent: CreateFileEvent = {
317318
type: MessageRequestEventType.CreateFile,
318-
card: threadId,
319+
card: messageData.isReply ? threadId : messageData.channel,
319320
message: messageId,
320321
messageCreated: messageData.created,
321322
creator: messageData.modifiedBy,
322323
data: {
323324
blobId: a.id as Ref<Blob>,
324325
type: a.contentType,
325326
filename: a.name,
326-
size: a.data.length
327+
size: a.data.length,
328+
meta: getBlobMetadata(ctx, a)
327329
}
328330
}
329-
return Buffer.from(JSON.stringify(creeateFileEvent))
331+
return Buffer.from(JSON.stringify(createFileEvent))
330332
})
331333
const fileEvents = fileData.map((data) => ({
332334
key: Buffer.from(messageData.channel ?? messageData.spaceId),

services/mail/mail-common/src/utils.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
//
1515
import TurndownService from 'turndown'
1616
import sanitizeHtml from 'sanitize-html'
17+
import { imageSize } from 'image-size'
1718

18-
import { MeasureContext } from '@hcengineering/core'
19-
import { EmailContact, EmailMessage } from './types'
19+
import { BlobMetadata, MeasureContext } from '@hcengineering/core'
20+
import { Attachment, EmailContact, EmailMessage } from './types'
2021

2122
const NAME_EMAIL_PATTERN = /^(?:"?([^"<]+)"?\s*)?<([^>]+)>$/
2223
const NAME_SEGMENT_REGEX = /[\s,;]+/
@@ -131,3 +132,21 @@ export function parseNameFromEmailHeader (headerValue: string | undefined): Emai
131132
export function normalizeEmail (email: string): string {
132133
return email.toLowerCase().trim()
133134
}
135+
136+
export function getBlobMetadata (ctx: MeasureContext, attachment: Attachment): BlobMetadata | undefined {
137+
try {
138+
const dimensions = imageSize(attachment.data)
139+
return dimensions != null
140+
? {
141+
originalHeight: dimensions.height,
142+
originalWidth: dimensions.width
143+
}
144+
: undefined
145+
} catch (error: any) {
146+
ctx.warn('Failed to get blob metadata', {
147+
error: error.message,
148+
attachmentId: attachment.id
149+
})
150+
return undefined
151+
}
152+
}

0 commit comments

Comments
 (0)