Skip to content

Commit 614c1e6

Browse files
committed
refactor(resources): remove composeObject
1 parent 66d5dad commit 614c1e6

File tree

3 files changed

+9
-97
lines changed

3 files changed

+9
-97
lines changed

src/minio/minio.service.ts

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import {
66
GetObjectCommand,
77
HeadObjectCommand,
88
DeleteObjectCommand,
9-
CreateMultipartUploadCommand,
10-
UploadPartCopyCommand,
11-
CompleteMultipartUploadCommand,
129
HeadBucketCommand,
1310
CreateBucketCommand,
1411
} from '@aws-sdk/client-s3';
@@ -80,7 +77,7 @@ export class MinioService implements OnModuleInit {
8077
this.bucket = s3Bucket;
8178
}
8279

83-
async putObject(objectName: string, buffer: Buffer, mimetype: string) {
80+
async putObject(objectName: string, buffer: Buffer, mimetype?: string) {
8481
const command = new PutObjectCommand({
8582
Bucket: this.bucket,
8683
Key: objectName,
@@ -90,56 +87,6 @@ export class MinioService implements OnModuleInit {
9087
return await this.s3Client.send(command);
9188
}
9289

93-
async putChunkObject(objectName: string, chunk: Buffer) {
94-
const command = new PutObjectCommand({
95-
Bucket: this.bucket,
96-
Key: objectName,
97-
Body: chunk,
98-
});
99-
return await this.s3Client.send(command);
100-
}
101-
102-
async composeObject(objectName: string, chunksName: Array<string>) {
103-
const createCommand = new CreateMultipartUploadCommand({
104-
Bucket: this.bucket,
105-
Key: objectName,
106-
ContentType: 'application/octet-stream',
107-
});
108-
const { UploadId } = await this.s3Client.send(createCommand);
109-
if (!UploadId) {
110-
throw new Error('Failed to initiate multipart upload');
111-
}
112-
113-
const uploadedParts: { ETag: string; PartNumber: number }[] = [];
114-
for (let i = 0; i < chunksName.length; i++) {
115-
const uploadPartCommand = new UploadPartCopyCommand({
116-
Bucket: this.bucket,
117-
Key: objectName,
118-
UploadId,
119-
PartNumber: i + 1,
120-
CopySource: `${this.bucket}/${chunksName[i]}`,
121-
});
122-
const response = await this.s3Client.send(uploadPartCommand);
123-
if (!response.CopyPartResult?.ETag) {
124-
throw new Error(`Failed to upload part`);
125-
}
126-
uploadedParts.push({
127-
ETag: response.CopyPartResult.ETag,
128-
PartNumber: i + 1,
129-
});
130-
}
131-
132-
const completeCommand = new CompleteMultipartUploadCommand({
133-
Bucket: this.bucket,
134-
Key: objectName,
135-
UploadId,
136-
MultipartUpload: {
137-
Parts: uploadedParts,
138-
},
139-
});
140-
return await this.s3Client.send(completeCommand);
141-
}
142-
14390
async getObject(objectName: string): Promise<Readable> {
14491
const command = new GetObjectCommand({
14592
Bucket: this.bucket,

src/namespace-resources/namespace-resources.service.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -802,14 +802,6 @@ export class NamespaceResourcesService {
802802
return `resources/${resourceId}`;
803803
}
804804

805-
chunkPath(
806-
namespaceId: string,
807-
fileHash: string,
808-
chunkNumber: string | number,
809-
) {
810-
return `chunks/${namespaceId}/${fileHash}/${chunkNumber}`;
811-
}
812-
813805
async uploadFile(
814806
userId: string,
815807
namespaceId: string,

src/wizard/chunk-manager.service.ts

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Injectable, Logger } from '@nestjs/common';
22
import { MinioService } from 'omniboxd/minio/minio.service';
33
import { ConfigService } from '@nestjs/config';
4+
import { buffer } from 'node:stream/consumers';
45

56
@Injectable()
67
export class ChunkManagerService {
@@ -26,7 +27,7 @@ export class ChunkManagerService {
2627
const buffer = Buffer.from(data, 'base64');
2728

2829
try {
29-
await this.minioService.putChunkObject(chunkPath, buffer);
30+
await this.minioService.putObject(chunkPath, buffer);
3031
this.logger.debug(
3132
`Stored chunk ${chunkIndex + 1}/${totalChunks} for task ${taskId}`,
3233
);
@@ -40,32 +41,13 @@ export class ChunkManagerService {
4041
}
4142

4243
async assembleChunks(taskId: string, totalChunks: number): Promise<string> {
43-
const assembledPath = this.getAssembledPath(taskId);
44-
const chunkPaths = Array.from({ length: totalChunks }, (_, i) =>
45-
this.getChunkPath(taskId, i),
46-
);
47-
48-
try {
49-
// Use MinIO's composeObject to merge all chunks
50-
await this.minioService.composeObject(assembledPath, chunkPaths);
51-
52-
// Retrieve the assembled data
53-
const stream = await this.minioService.getObject(assembledPath);
54-
const chunks: Buffer[] = [];
55-
56-
return new Promise((resolve, reject) => {
57-
stream.on('data', (chunk) => chunks.push(chunk));
58-
stream.on('end', () => {
59-
const assembledBuffer = Buffer.concat(chunks);
60-
const assembledData = assembledBuffer.toString('utf-8');
61-
resolve(assembledData);
62-
});
63-
stream.on('error', reject);
64-
});
65-
} catch (error) {
66-
this.logger.error(`Failed to assemble chunks for task ${taskId}:`, error);
67-
throw error;
44+
const buffers: Buffer[] = [];
45+
for (let i = 0; i < totalChunks; i++) {
46+
const chunkPath = this.getChunkPath(taskId, i);
47+
const stream = await this.minioService.getObject(chunkPath);
48+
buffers.push(await buffer(stream));
6849
}
50+
return Buffer.concat(buffers).toString('utf-8');
6951
}
7052

7153
cleanupChunks(taskId: string, totalChunks: number): void {
@@ -87,15 +69,10 @@ export class ChunkManagerService {
8769
try {
8870
const objectsToRemove: string[] = [];
8971

90-
// Add chunk paths
9172
for (let i = 0; i < totalChunks; i++) {
9273
objectsToRemove.push(this.getChunkPath(taskId, i));
9374
}
9475

95-
// Add assembled path
96-
objectsToRemove.push(this.getAssembledPath(taskId));
97-
98-
// Remove all objects
9976
await Promise.all(
10077
objectsToRemove.map((objectName) =>
10178
this.minioService.removeObject(objectName).catch((error) => {
@@ -113,8 +90,4 @@ export class ChunkManagerService {
11390
private getChunkPath(taskId: string, chunkIndex: number): string {
11491
return `wizard-chunks/${taskId}/chunk-${chunkIndex.toString().padStart(6, '0')}`;
11592
}
116-
117-
private getAssembledPath(taskId: string): string {
118-
return `wizard-chunks/${taskId}/assembled`;
119-
}
12093
}

0 commit comments

Comments
 (0)