-
-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* perf: optimize thumbnail generation based on image height * fix: e2e expect * fix: e2e expect * refactor: restructure image slicing process with database persistence * feat: add repair table attachment thumbnail * chore: remove useless code
- Loading branch information
Showing
27 changed files
with
325 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
apps/nestjs-backend/src/features/attachments/attachments-crop.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { EventJobModule } from '../../event-emitter/event-job/event-job.module'; | ||
import { | ||
ATTACHMENTS_CROP_QUEUE, | ||
AttachmentsCropQueueProcessor, | ||
} from './attachments-crop.processor'; | ||
import { AttachmentsStorageModule } from './attachments-storage.module'; | ||
|
||
@Module({ | ||
providers: [AttachmentsCropQueueProcessor], | ||
imports: [EventJobModule.registerQueue(ATTACHMENTS_CROP_QUEUE), AttachmentsStorageModule], | ||
exports: [AttachmentsCropQueueProcessor], | ||
}) | ||
export class AttachmentsCropModule {} |
60 changes: 60 additions & 0 deletions
60
apps/nestjs-backend/src/features/attachments/attachments-crop.processor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { InjectQueue, Processor, WorkerHost } from '@nestjs/bullmq'; | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { PrismaService } from '@teable/db-main-prisma'; | ||
import { Queue } from 'bullmq'; | ||
import type { Job } from 'bullmq'; | ||
import { AttachmentsStorageService } from '../attachments/attachments-storage.service'; | ||
|
||
interface IRecordImageJob { | ||
bucket: string; | ||
token: string; | ||
path: string; | ||
mimetype: string; | ||
height?: number | null; | ||
} | ||
|
||
export const ATTACHMENTS_CROP_QUEUE = 'attachments-crop-queue'; | ||
|
||
@Injectable() | ||
@Processor(ATTACHMENTS_CROP_QUEUE) | ||
export class AttachmentsCropQueueProcessor extends WorkerHost { | ||
private logger = new Logger(AttachmentsCropQueueProcessor.name); | ||
|
||
constructor( | ||
private readonly prismaService: PrismaService, | ||
private readonly attachmentsStorageService: AttachmentsStorageService, | ||
@InjectQueue(ATTACHMENTS_CROP_QUEUE) public readonly queue: Queue<IRecordImageJob> | ||
) { | ||
super(); | ||
} | ||
|
||
public async process(job: Job<IRecordImageJob>) { | ||
const { bucket, token, path, mimetype, height } = job.data; | ||
if (mimetype.startsWith('image/') && height) { | ||
const existingThumbnailPath = await this.prismaService.attachments.findUnique({ | ||
where: { token }, | ||
select: { thumbnailPath: true }, | ||
}); | ||
if (existingThumbnailPath?.thumbnailPath) { | ||
this.logger.log(`path(${path}) image already has thumbnail`); | ||
return; | ||
} | ||
const { lgThumbnailPath, smThumbnailPath } = | ||
await this.attachmentsStorageService.cropTableImage(bucket, path, height); | ||
await this.prismaService.attachments.update({ | ||
where: { | ||
token, | ||
}, | ||
data: { | ||
thumbnailPath: JSON.stringify({ | ||
lg: lgThumbnailPath, | ||
sm: smThumbnailPath, | ||
}), | ||
}, | ||
}); | ||
this.logger.log(`path(${path}) crop thumbnails success`); | ||
return; | ||
} | ||
this.logger.log(`path(${path}) is not a image`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 2 additions & 7 deletions
9
apps/nestjs-backend/src/features/attachments/attachments-table.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { EventJobModule } from '../../event-emitter/event-job/event-job.module'; | ||
import { AttachmentsStorageModule } from './attachments-storage.module'; | ||
import { | ||
ATTACHMENTS_TABLE_QUEUE, | ||
AttachmentsTableQueueProcessor, | ||
} from './attachments-table.processor'; | ||
import { AttachmentsTableService } from './attachments-table.service'; | ||
|
||
@Module({ | ||
providers: [AttachmentsTableService, AttachmentsTableQueueProcessor], | ||
imports: [AttachmentsStorageModule, EventJobModule.registerQueue(ATTACHMENTS_TABLE_QUEUE)], | ||
providers: [AttachmentsTableService], | ||
imports: [AttachmentsStorageModule], | ||
exports: [AttachmentsTableService], | ||
}) | ||
export class AttachmentsTableModule {} |
51 changes: 0 additions & 51 deletions
51
apps/nestjs-backend/src/features/attachments/attachments-table.processor.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.