Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/s3/s3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ export class S3Service implements OnModuleInit {
});
}

async generateUploadUrl(key: string, isPublic: boolean): Promise<string> {
const s3Client = isPublic ? this.s3PublicClient : this.s3Client;
const command = new PutObjectCommand({
Bucket: this.bucket,
Key: key,
});
return await getSignedUrl(s3Client, command, { expiresIn: 900 });
}

async generateDownloadUrl(
key: string,
isPublic: boolean,
Expand Down
2 changes: 1 addition & 1 deletion src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ if (isTracingEnabled()) {
instrumentations: [
new HttpInstrumentation({
ignoreIncomingRequestHook: (req) => {
return excludedUrls.some((url) => req.url?.includes(url)) || false;
return excludedUrls.some((url) => req.url === url) || false;
},
}),
new ExpressInstrumentation({
Expand Down
4 changes: 2 additions & 2 deletions src/wizard/dto/task-callback.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export class TaskCallbackDto {
id: string;

@IsOptional()
exception: Record<string, any>;
exception?: Record<string, any>;

@IsOptional()
output: Record<string, any>;
output?: Record<string, any>;
}
15 changes: 14 additions & 1 deletion src/wizard/internal.wizard.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { transformKeysToSnakeCase } from 'omniboxd/interceptor/utils';
import { TaskCallbackDto } from 'omniboxd/wizard/dto/task-callback.dto';
import { ChunkCallbackDto } from 'omniboxd/wizard/dto/chunk-callback.dto';
import { ChunkManagerService } from 'omniboxd/wizard/chunk-manager.service';
import { Body, Controller, Get, Post, Query, Res } from '@nestjs/common';
import { Body, Controller, Get, Param, Post, Query, Res } from '@nestjs/common';
import { FetchTaskRequest } from 'omniboxd/wizard/dto/fetch-task-request.dto';

@Controller('internal/api/v1/wizard')
Expand Down Expand Up @@ -33,6 +33,19 @@ export class InternalWizardController {
return await this.wizardService.taskDoneCallback(taskCallback);
}

@Public()
@Post('tasks/:taskId/upload')
async createTaskResult(@Param('taskId') taskId: string) {
const url = await this.wizardService.createTaskUploadUrl(taskId);
return { url };
}

@Public()
@Post('tasks/:taskId/callback')
async handleUploadedTaskCallback(@Param('taskId') taskId: string) {
return await this.wizardService.uploadedTaskDoneCallback(taskId);
}

@Public()
@Post('callback/chunk')
async handleChunkCallback(
Expand Down
22 changes: 20 additions & 2 deletions src/wizard/wizard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { isEmpty } from 'omniboxd/utils/is-empty';
import { FetchTaskRequest } from 'omniboxd/wizard/dto/fetch-task-request.dto';
import { S3Service } from 'omniboxd/s3/s3.service';
import { createGunzip } from 'zlib';
import { buffer } from 'node:stream/consumers';
import { SharedResourcesService } from 'omniboxd/shared-resources/shared-resources.service';
import { ResourcesService } from 'omniboxd/resources/resources.service';

Expand Down Expand Up @@ -227,6 +228,23 @@ export class WizardService {
return { task_id: task.id, resource_id: resource.id };
}

async createTaskUploadUrl(taskId: string): Promise<string> {
return await this.s3Service.generateUploadUrl(
`wizard-tasks/${taskId}`,
true,
);
}

async uploadedTaskDoneCallback(taskId: string) {
const key = `wizard-tasks/${taskId}`;
const { stream } = await this.s3Service.getObject(key);
const payload = await buffer(stream);
const taskCallback: TaskCallbackDto = JSON.parse(payload.toString('utf-8'));
const result = await this.taskDoneCallback(taskCallback);
await this.s3Service.deleteObject(key);
return result;
}

async taskDoneCallback(data: TaskCallbackDto) {
const task = await this.wizardTaskService.taskRepository.findOneOrFail({
where: { id: data.id },
Expand All @@ -244,8 +262,8 @@ export class WizardService {
}

task.endedAt = new Date();
task.exception = data.exception;
task.output = data.output;
task.exception = data.exception || null;
task.output = data.output || null;
await this.preprocessTask(task);

await this.wizardTaskService.taskRepository.save(task);
Expand Down