|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import type { IAttachmentCellValue, IOtOperation } from '@teable/core'; |
| 3 | +import { RecordOpBuilder } from '@teable/core'; |
| 4 | +import { PrismaService } from '@teable/db-main-prisma'; |
| 5 | +import { UploadType } from '@teable/openapi'; |
| 6 | +import type { EditOp, CreateOp, DeleteOp } from 'sharedb'; |
| 7 | +import { CacheService } from '../../cache/cache.service'; |
| 8 | +import { AttachmentsStorageService } from '../../features/attachments/attachments-storage.service'; |
| 9 | +import StorageAdapter from '../../features/attachments/plugins/adapter'; |
| 10 | +import { getTableThumbnailToken } from '../../utils/generate-thumbnail-path'; |
| 11 | +import { Timing } from '../../utils/timing'; |
| 12 | +import type { IRawOpMap } from '../interface'; |
| 13 | + |
| 14 | +@Injectable() |
| 15 | +export class RepairAttachmentOpService { |
| 16 | + constructor( |
| 17 | + private readonly prismaService: PrismaService, |
| 18 | + private readonly cacheService: CacheService, |
| 19 | + private readonly attachmentsStorageService: AttachmentsStorageService |
| 20 | + ) {} |
| 21 | + |
| 22 | + private isEditOp(rawOp: EditOp | CreateOp | DeleteOp): rawOp is EditOp { |
| 23 | + return Boolean(!rawOp.del && !rawOp.create && rawOp.op); |
| 24 | + } |
| 25 | + |
| 26 | + private getAttachmentCell(op: IOtOperation) { |
| 27 | + const setRecordOp = RecordOpBuilder.editor.setRecord.detect(op); |
| 28 | + if (!setRecordOp) { |
| 29 | + return; |
| 30 | + } |
| 31 | + const newCellValue = setRecordOp.newCellValue; |
| 32 | + if (newCellValue && Array.isArray(newCellValue) && newCellValue?.[0]?.mimetype) { |
| 33 | + return newCellValue as IAttachmentCellValue; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private getCollectionsAttachmentToken(rawOp: EditOp | CreateOp | DeleteOp): string[] | undefined { |
| 38 | + if (!this.isEditOp(rawOp)) { |
| 39 | + return; |
| 40 | + } |
| 41 | + return rawOp.op.reduce((acc, op) => { |
| 42 | + const attachmentCell = this.getAttachmentCell(op); |
| 43 | + if (!attachmentCell) { |
| 44 | + return acc; |
| 45 | + } |
| 46 | + attachmentCell.forEach((cell) => { |
| 47 | + if (!cell.presignedUrl) { |
| 48 | + acc.push(cell.token); |
| 49 | + } |
| 50 | + }); |
| 51 | + return acc; |
| 52 | + }, []); |
| 53 | + } |
| 54 | + |
| 55 | + private async getThumbnailPathTokenMap(tokens: string[]) { |
| 56 | + const thumbnailPathTokenMap: Record< |
| 57 | + string, |
| 58 | + { |
| 59 | + sm?: string; |
| 60 | + lg?: string; |
| 61 | + } |
| 62 | + > = {}; |
| 63 | + // once handle 1000 tokens |
| 64 | + const batchSize = 1000; |
| 65 | + for (let i = 0; i < tokens.length; i += batchSize) { |
| 66 | + const batch = tokens.slice(i, i + batchSize); |
| 67 | + const attachments = await this.prismaService.txClient().attachments.findMany({ |
| 68 | + where: { token: { in: batch } }, |
| 69 | + select: { token: true, thumbnailPath: true }, |
| 70 | + }); |
| 71 | + attachments.forEach((attachment) => { |
| 72 | + if (attachment.thumbnailPath) { |
| 73 | + thumbnailPathTokenMap[attachment.token] = JSON.parse(attachment.thumbnailPath); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + return thumbnailPathTokenMap; |
| 78 | + } |
| 79 | + |
| 80 | + private async getCachePreviewUrlTokenMap(tokens: string[]) { |
| 81 | + const previewUrlTokenMap: Record<string, string> = {}; |
| 82 | + // once handle 1000 tokens |
| 83 | + const batchSize = 1000; |
| 84 | + for (let i = 0; i < tokens.length; i += batchSize) { |
| 85 | + const batch = tokens.slice(i, i + batchSize); |
| 86 | + const previewUrls = await this.cacheService.getMany( |
| 87 | + batch.map((token) => `attachment:preview:${token}` as const) |
| 88 | + ); |
| 89 | + previewUrls.forEach((urlCache, index) => { |
| 90 | + if (urlCache) { |
| 91 | + previewUrlTokenMap[batch[i + index]] = urlCache.url; |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + return previewUrlTokenMap; |
| 96 | + } |
| 97 | + |
| 98 | + @Timing() |
| 99 | + async getCollectionsAttachmentsContext(rawOpMaps: IRawOpMap[]) { |
| 100 | + const collectionsAttachmentTokens: Record<string, string[]> = {}; |
| 101 | + for (const rawOpMap of rawOpMaps) { |
| 102 | + for (const collection in rawOpMap) { |
| 103 | + const data = rawOpMap[collection]; |
| 104 | + for (const docId in data) { |
| 105 | + const rawOp = data[docId] as EditOp | CreateOp | DeleteOp; |
| 106 | + const attachmentCells = this.getCollectionsAttachmentToken(rawOp); |
| 107 | + const tableId = collection.split('_')[1]; |
| 108 | + if (attachmentCells?.length) { |
| 109 | + collectionsAttachmentTokens[`${tableId}-${docId}`] = attachmentCells; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + const tokens = Object.values(collectionsAttachmentTokens).flat(); |
| 115 | + const uniqueTokens = [...new Set(tokens)]; |
| 116 | + const thumbnailPathTokenMap = await this.getThumbnailPathTokenMap(uniqueTokens); |
| 117 | + const cachePreviewUrlTokenMap = await this.getCachePreviewUrlTokenMap(uniqueTokens); |
| 118 | + return { |
| 119 | + thumbnailPathTokenMap, |
| 120 | + cachePreviewUrlTokenMap, |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + private async presignedAttachmentUrl( |
| 125 | + item: { name: string; path: string; token: string; mimetype: string }, |
| 126 | + context: { |
| 127 | + thumbnailPathTokenMap: Record<string, { sm?: string; lg?: string }>; |
| 128 | + cachePreviewUrlTokenMap: Record<string, string>; |
| 129 | + } |
| 130 | + ) { |
| 131 | + const { thumbnailPathTokenMap, cachePreviewUrlTokenMap } = context; |
| 132 | + const { path, token, mimetype, name } = item; |
| 133 | + |
| 134 | + const presignedUrl = |
| 135 | + cachePreviewUrlTokenMap[token] ?? |
| 136 | + (await this.attachmentsStorageService.getPreviewUrlByPath( |
| 137 | + StorageAdapter.getBucket(UploadType.Table), |
| 138 | + path, |
| 139 | + token, |
| 140 | + undefined, |
| 141 | + { |
| 142 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 143 | + 'Content-Type': mimetype, |
| 144 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 145 | + 'Content-Disposition': `attachment; filename="${name}"`, |
| 146 | + } |
| 147 | + )); |
| 148 | + let smThumbnailUrl: string | undefined; |
| 149 | + let lgThumbnailUrl: string | undefined; |
| 150 | + if (mimetype.startsWith('image/') && thumbnailPathTokenMap && thumbnailPathTokenMap[token]) { |
| 151 | + const { sm: smThumbnailPath, lg: lgThumbnailPath } = thumbnailPathTokenMap[token]!; |
| 152 | + if (smThumbnailPath) { |
| 153 | + smThumbnailUrl = |
| 154 | + cachePreviewUrlTokenMap?.[getTableThumbnailToken(smThumbnailPath)] ?? |
| 155 | + (await this.attachmentsStorageService.getTableThumbnailUrl(smThumbnailPath, mimetype)); |
| 156 | + } |
| 157 | + if (lgThumbnailPath) { |
| 158 | + lgThumbnailUrl = |
| 159 | + cachePreviewUrlTokenMap?.[getTableThumbnailToken(lgThumbnailPath)] ?? |
| 160 | + (await this.attachmentsStorageService.getTableThumbnailUrl(lgThumbnailPath, mimetype)); |
| 161 | + } |
| 162 | + smThumbnailUrl = smThumbnailUrl || presignedUrl; |
| 163 | + lgThumbnailUrl = lgThumbnailUrl || presignedUrl; |
| 164 | + } |
| 165 | + return { |
| 166 | + presignedUrl, |
| 167 | + smThumbnailUrl, |
| 168 | + lgThumbnailUrl, |
| 169 | + }; |
| 170 | + } |
| 171 | + |
| 172 | + async repairAttachmentOp( |
| 173 | + rawOp: EditOp | CreateOp | DeleteOp, |
| 174 | + context: { |
| 175 | + thumbnailPathTokenMap: Record<string, { sm?: string; lg?: string }>; |
| 176 | + cachePreviewUrlTokenMap: Record<string, string>; |
| 177 | + } |
| 178 | + ) { |
| 179 | + if (!this.isEditOp(rawOp)) { |
| 180 | + return rawOp; |
| 181 | + } |
| 182 | + for (const op of rawOp.op) { |
| 183 | + const newAttachmentCell = this.getAttachmentCell(op); |
| 184 | + if (!newAttachmentCell) { |
| 185 | + continue; |
| 186 | + } |
| 187 | + for (const item of newAttachmentCell) { |
| 188 | + if (!item.presignedUrl) { |
| 189 | + const { presignedUrl, smThumbnailUrl, lgThumbnailUrl } = |
| 190 | + await this.presignedAttachmentUrl(item, context); |
| 191 | + item.presignedUrl = presignedUrl; |
| 192 | + item.smThumbnailUrl = smThumbnailUrl; |
| 193 | + item.lgThumbnailUrl = lgThumbnailUrl; |
| 194 | + } |
| 195 | + } |
| 196 | + op.oi = newAttachmentCell; |
| 197 | + } |
| 198 | + return rawOp; |
| 199 | + } |
| 200 | +} |
0 commit comments