-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfile-queue-processor.provider.ts
More file actions
185 lines (164 loc) · 7.28 KB
/
Copy pathfile-queue-processor.provider.ts
File metadata and controls
185 lines (164 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { FileEntity } from '@kleinkram/backend-common/entities/file/file.entity';
import { IngestionJobEntity } from '@kleinkram/backend-common/entities/file/ingestion-job.entity';
import { IStorageBucket } from '@kleinkram/backend-common/modules/storage/types';
import { FileLocation, FileState, QueueState } from '@kleinkram/shared';
import { InjectQueue, Process, Processor } from '@nestjs/bull';
import { Inject, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Job, Queue } from 'bull';
import { Repository } from 'typeorm';
import logger from '../logger';
import { FileIngestionService } from './file-ingestion.service';
import { GoogleDriveStrategy } from './strategies/google-drive.strategy';
import { S3Strategy } from './strategies/s3.strategy';
import { createHash } from 'node:crypto';
@Processor('file-queue')
@Injectable()
export class FileQueueProcessorProvider {
constructor(
@InjectRepository(IngestionJobEntity)
private queueRepo: Repository<IngestionJobEntity>,
@InjectRepository(FileEntity)
private fileRepo: Repository<FileEntity>,
@Inject('DataStorageBucket')
private readonly dataStorage: IStorageBucket,
private readonly fileIngestionService: FileIngestionService,
private readonly driveStrategy: GoogleDriveStrategy,
private readonly s3Strategy: S3Strategy,
@InjectQueue('file-queue') private fileQueue: Queue,
) {}
@Process({ name: 'processDriveFile', concurrency: 1 })
async processDriveFile(job: Job<{ queueUuid: string }>): Promise<void> {
logger.debug(`Processing Drive File Job: ${job.data.queueUuid}`);
const queueItem = await this.queueRepo.findOneOrFail({
where: { uuid: job.data.queueUuid },
relations: ['mission', 'creator', 'mission.project'],
});
// Check if it is a folder (recursive ingestion)
if (queueItem.location === FileLocation.DRIVE) {
const metadata = await this.driveStrategy.getMetadata(
queueItem.identifier,
);
if (metadata.mimeType === 'application/vnd.google-apps.folder') {
logger.debug(
`Found Google Drive Folder: ${queueItem.displayName}. EXPANDING...`,
);
await this.expandDriveFolder(queueItem);
return;
}
}
return this.runPipeline(queueItem);
}
@Process({ name: 'processS3File', concurrency: 1 })
async processS3File(job: Job<{ queueUuid: string }>): Promise<void> {
logger.debug(`Processing S3 File Job: ${job.data.queueUuid}`);
const queueItem = await this.queueRepo.findOneOrFail({
where: { uuid: job.data.queueUuid },
relations: ['mission', 'creator', 'mission.project'],
});
return this.runPipeline(queueItem);
}
@Process({ name: 'extractHashFromS3' })
async extractHashFromS3(job: Job<{ fileUuid: string }>): Promise<void> {
const { fileUuid } = job.data;
logger.debug(`Extracting hash for file ${fileUuid}`);
const file = await this.fileRepo.findOne({
where: { uuid: fileUuid },
});
if (!file) {
logger.error(`File ${fileUuid} not found for hash extraction`);
return;
}
try {
const stat = await this.dataStorage.getFileInfo(file.uuid);
// ETag is often surrounded by quotes in S3, e.g. "5b3...c6"
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
let hash = stat?.etag?.replaceAll('"', '');
// Valid MD5 is 32 hex chars. S3 multipart ETag has -N suffix.
const ismd5 = /^[\da-f]{32}$/i.test(hash ?? '');
if (hash && ismd5) {
logger.debug(
`Found MD5 hash in metadata for ${fileUuid}: ${hash}`,
);
// Convert Hex MD5 to Base64 to match CLI/Frontend expectation
hash = Buffer.from(hash, 'hex').toString('base64');
} else {
logger.debug(
`Calculating hash for ${fileUuid} (ETag: ${String(hash)})`,
);
const stream = await this.dataStorage.getFileStream(file.uuid);
hash = await this.calculateHash(stream);
}
file.hash = hash;
// Ensure state is OK if it was somehow different
file.state = FileState.OK;
file.stateComment = null;
await this.fileRepo.save(file);
logger.debug(`Updated hash for ${fileUuid}`);
} catch (error) {
logger.error(
`Failed to extract hash for ${fileUuid}: ${String(error)}`,
);
throw error;
}
}
private calculateHash(stream: NodeJS.ReadableStream): Promise<string> {
return new Promise((resolve, reject) => {
const hash = createHash('md5');
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
stream.on('data', (data) => hash.update(data));
stream.on('end', () => {
resolve(hash.digest('base64'));
});
stream.on('error', (error) => {
reject(
error instanceof Error ? error : new Error(String(error)),
);
});
});
}
private async expandDriveFolder(parentJob: IngestionJobEntity) {
try {
const files = await this.driveStrategy.listFiles(
parentJob.identifier,
);
logger.debug(
`Expanding folder ${parentJob.identifier}: Found ${String(files.length)} files`,
);
for (const file of files) {
// If it's a folder, we recurse (by adding it to the queue)
// If it's a file, we add it to the queue
// We basically clone the parent job but change identifier/name
const newJob = this.queueRepo.create({
identifier: file.id,
displayName: file.name,
state: QueueState.AWAITING_PROCESSING,
mission: parentJob.mission,
creator: parentJob.creator,
location: parentJob.location,
});
const savedJob = await this.queueRepo.save(newJob);
await this.fileQueue.add('processDriveFile', {
queueUuid: savedJob.uuid,
});
}
// Mark folder as completed
parentJob.state = QueueState.COMPLETED;
await this.queueRepo.save(parentJob);
} catch (error) {
logger.error(
`Failed to expand folder ${parentJob.identifier}: ${String(error)}`,
);
parentJob.state = QueueState.ERROR;
parentJob.errorMessage = String(error).slice(0, 1000);
await this.queueRepo.save(parentJob);
}
}
private async runPipeline(queueItem: IngestionJobEntity): Promise<void> {
const strategy =
queueItem.location === FileLocation.DRIVE
? this.driveStrategy
: this.s3Strategy;
await this.fileIngestionService.processJob(queueItem, strategy);
}
}