Skip to content

Commit e1ac5c4

Browse files
committed
fix(materials): use streams for hash calculation to prevent OOM on large uploads
1 parent 247d0de commit e1ac5c4

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/materials/materials.service.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ import { Injectable, OnModuleInit } from '@nestjs/common';
1111
import { MaterialType } from '@prisma/client';
1212
import ffmpeg from 'fluent-ffmpeg';
1313
import path, { join } from 'node:path';
14-
import { readFileSync } from 'node:fs';
1514
import { promisify } from 'node:util';
15+
import { getFileHash } from '../common/helper/file.helper';
1616
import { PrismaService } from '../common/prisma/prisma.service';
17-
import { MaterialNotFoundError, MetaDataParseError } from './materials.error';
18-
import { materialDto } from './DTO/material.dto';
1917
import { UsersService } from '../users/users.service';
20-
import md5 from 'md5';
18+
import { materialDto } from './DTO/material.dto';
19+
import { MaterialNotFoundError, MetaDataParseError } from './materials.error';
2120
@Injectable()
2221
export class MaterialsService implements OnModuleInit {
2322
private ffprobeAsync: (file: string) => Promise<ffmpeg.FfprobeData>;
@@ -101,8 +100,7 @@ export class MaterialsService implements OnModuleInit {
101100
): Promise<PrismaJson.metaType> {
102101
let meta: PrismaJson.metaType;
103102

104-
const buf = readFileSync(file.path);
105-
const hash = md5(buf);
103+
const hash = await getFileHash(file.path);
106104

107105
if (type === MaterialType.image) {
108106
const metadata = await this.getImageMetadata(file.path);

0 commit comments

Comments
 (0)