Browse Source

connected queue to screenshotter; first functional version

main
Inga 🏳‍🌈 3 months ago
parent
commit
a586a0fbc7
  1. 5
      src/app.module.ts
  2. 22
      src/hello-world/controller.spec.ts
  3. 12
      src/hello-world/controller.ts
  4. 8
      src/hello-world/service.ts
  5. 0
      src/screenshots/__image_snapshots__/screenshotter-service-spec-ts-screenshotter-service-should-return-hello-world-screenshot-1-snap.png
  6. 18
      src/screenshots/screenshots.controller.ts
  7. 3
      src/screenshots/screenshots.module.ts
  8. 14
      src/screenshots/screenshots.processor.ts
  9. 0
      src/screenshots/screenshotter.service.spec.ts
  10. 0
      src/screenshots/screenshotter.service.ts
  11. 1
      tsconfig.json

5
src/app.module.ts

@ -1,8 +1,5 @@ @@ -1,8 +1,5 @@
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { HelloWorldController } from './hello-world/controller';
import { HelloWorldService } from './hello-world/service';
import { ScreenshotterService } from './screenshotter/screenshotter.service';
import { BullModule } from '@nestjs/bull';
import { ScreenshotsModule } from './screenshots/screenshots.module';
import { BullConfigService } from './bull/bullConfigService';
@ -22,7 +19,5 @@ import { join } from 'path'; @@ -22,7 +19,5 @@ import { join } from 'path';
rootPath: join(__dirname, '..', 'client'),
}),
],
controllers: [HelloWorldController],
providers: [HelloWorldService, ScreenshotterService],
})
export class AppModule {}

22
src/hello-world/controller.spec.ts

@ -1,22 +0,0 @@ @@ -1,22 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HelloWorldController } from './controller';
import { HelloWorldService } from './service';
describe('HelloWorldController', () => {
let helloWorldController: HelloWorldController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [HelloWorldController],
providers: [HelloWorldService],
}).compile();
helloWorldController = app.get<HelloWorldController>(HelloWorldController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(helloWorldController.getHello()).toBe('Hello World!');
});
});
});

12
src/hello-world/controller.ts

@ -1,12 +0,0 @@ @@ -1,12 +0,0 @@
import { Controller, Get } from '@nestjs/common';
import { HelloWorldService } from './service';
@Controller('hello-world')
export class HelloWorldController {
constructor(private readonly helloWorldService: HelloWorldService) {}
@Get()
getHello(): string {
return this.helloWorldService.getHello();
}
}

8
src/hello-world/service.ts

@ -1,8 +0,0 @@ @@ -1,8 +0,0 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class HelloWorldService {
getHello(): string {
return 'Hello World!';
}
}

0
src/screenshotter/__image_snapshots__/screenshotter-service-spec-ts-screenshotter-service-should-return-hello-world-screenshot-1-snap.png → src/screenshots/__image_snapshots__/screenshotter-service-spec-ts-screenshotter-service-should-return-hello-world-screenshot-1-snap.png

Before

Width:  |  Height:  |  Size: 6.2 KiB

After

Width:  |  Height:  |  Size: 6.2 KiB

18
src/screenshots/screenshots.controller.ts

@ -1,6 +1,8 @@ @@ -1,6 +1,8 @@
import { InjectQueue } from '@nestjs/bull';
import { Body, Controller, Get, NotFoundException, Param, Post } from '@nestjs/common';
import { Body, Controller, Get, NotFoundException, Param, Post, Res, StreamableFile } from '@nestjs/common';
import { ApiResponse } from '@nestjs/swagger';
import { Response } from 'express'
import { Readable } from 'stream';
import { CreateJobRequestDto, GetJobResponseDto, JobStatusDto } from './screenshots.dto'
import { QUEUE_NAME, ScreenshotsQueue } from './shared';
@ -40,7 +42,7 @@ export class ScreenshotsController { @@ -40,7 +42,7 @@ export class ScreenshotsController {
@Get(':id/result')
@ApiResponse({ status: 404 })
async getScreenshot(@Param('id') id: string) {
async getScreenshot(@Param('id') id: string, @Res({ passthrough: true }) res: Response): Promise<StreamableFile> {
const jobInfo = await this.screenshotsQueue.getJob(id)
if (!jobInfo) {
@ -51,6 +53,16 @@ export class ScreenshotsController { @@ -51,6 +53,16 @@ export class ScreenshotsController {
throw new NotFoundException()
}
return await jobInfo.returnvalue
switch (jobInfo.data.imageType) {
case 'jpeg':
res.setHeader('Content-Type', 'image/jpeg')
break
case 'png':
res.setHeader('Content-Type', 'image/png')
break
}
const buffer = Buffer.from(await jobInfo.returnvalue as string, 'base64')
return new StreamableFile(buffer)
}
}

3
src/screenshots/screenshots.module.ts

@ -3,6 +3,7 @@ import { Module } from '@nestjs/common'; @@ -3,6 +3,7 @@ import { Module } from '@nestjs/common';
import { QUEUE_NAME } from './shared';
import { ScreenshotsController } from './screenshots.controller';
import { ScreenshotsProcessor } from './screenshots.processor';
import { ScreenshotterService } from './screenshotter.service';
@Module({
imports: [
@ -11,6 +12,6 @@ import { ScreenshotsProcessor } from './screenshots.processor'; @@ -11,6 +12,6 @@ import { ScreenshotsProcessor } from './screenshots.processor';
}),
],
controllers: [ScreenshotsController],
providers: [ScreenshotsProcessor]
providers: [ScreenshotsProcessor, ScreenshotterService]
})
export class ScreenshotsModule {}

14
src/screenshots/screenshots.processor.ts

@ -1,12 +1,24 @@ @@ -1,12 +1,24 @@
import { Process, Processor } from '@nestjs/bull';
import { Injectable } from '@nestjs/common';
import { ScreenshotterService } from './screenshotter.service';
import { QUEUE_NAME, ScreenshotJob } from './shared';
@Injectable()
@Processor(QUEUE_NAME)
export class ScreenshotsProcessor {
constructor(private readonly screenshotterService: ScreenshotterService) {}
@Process()
async handleScreenshotJob(job: ScreenshotJob) {
job.moveToCompleted('abc')
try {
const imageData = await this.screenshotterService.takeScreenshot({
url: job.data.pageUrl,
imageType: job.data.imageType,
})
job.moveToCompleted(imageData.toString('base64'))
} catch(err) {
job.moveToFailed({ message: JSON.stringify(err) })
}
}
}

0
src/screenshotter/screenshotter.service.spec.ts → src/screenshots/screenshotter.service.spec.ts

0
src/screenshotter/screenshotter.service.ts → src/screenshots/screenshotter.service.ts

1
tsconfig.json

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["ES2019"],
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,

Loading…
Cancel
Save