Browse Source

fix: memory usage for thumbnail generation

nc-fix/thumbnail-worker
mertmit 3 months ago
parent
commit
c081e78e09
  1. 31
      packages/nocodb/src/modules/jobs/jobs/thumbnail-generator/thumbnail-generator.processor.ts

31
packages/nocodb/src/modules/jobs/jobs/thumbnail-generator/thumbnail-generator.processor.ts

@ -16,17 +16,24 @@ export class ThumbnailGeneratorProcessor {
async job(job: Job<ThumbnailGeneratorJobData>) {
const { attachments } = job.data;
const thumbnailPromises = attachments.map(async (attachment) => {
const results = [];
for (const attachment of attachments) {
const thumbnail = await this.generateThumbnail(attachment);
return {
if (!thumbnail) {
continue;
}
results.push({
path: attachment.path ?? attachment.url,
card_cover: thumbnail?.card_cover,
small: thumbnail?.small,
tiny: thumbnail?.tiny,
};
});
}
return await Promise.all(thumbnailPromises);
return results;
}
private async generateThumbnail(
@ -47,6 +54,8 @@ export class ThumbnailGeneratorProcessor {
return;
}
sharp.concurrency(1);
try {
const storageAdapter = await NcPluginMgrv2.storageAdapter();
@ -66,8 +75,11 @@ export class ThumbnailGeneratorProcessor {
tiny: path.join('nc', 'thumbnails', relativePath, 'tiny.jpg'),
};
await Promise.all(
Object.entries(thumbnailPaths).map(async ([size, thumbnailPath]) => {
const sharpImage = await sharp(file, {
limitInputPixels: false,
});
for (const [size, thumbnailPath] of Object.entries(thumbnailPaths)) {
let height;
switch (size) {
case 'card_cover':
@ -84,9 +96,7 @@ export class ThumbnailGeneratorProcessor {
break;
}
const resizedImage = await sharp(file, {
limitInputPixels: false,
})
const resizedImage = await sharpImage
.resize(undefined, height, {
fit: sharp.fit.cover,
kernel: 'lanczos3',
@ -100,8 +110,7 @@ export class ThumbnailGeneratorProcessor {
mimetype: 'image/jpeg',
},
);
}),
);
}
return thumbnailPaths;
} catch (error) {

Loading…
Cancel
Save