Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 5 additions & 5 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ POSTGRES_DB=vrt_db_dev

# static
STATIC_SERVICE=hdd # hdd | s3 - hdd as default if not provided

# AWS_ACCESS_KEY_ID=
# AWS_SECRET_ACCESS_KEY=
# AWS_REGION=
# AWS_S3_BUCKET_NAME=
# Enter below values if STATIC_SERVICE=s3
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=
AWS_S3_BUCKET_NAME=

# optional
#HTTPS_KEY_PATH='./secrets/ssl.key'
Expand Down
3,123 changes: 2,999 additions & 124 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"node": ">=18.12.0"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.717.0",
"@aws-sdk/s3-request-presigner": "^3.717.0",
"@nestjs/cache-manager": "^2.1.0",
"@nestjs/common": "^10.2.5",
"@nestjs/config": "^3.1.1",
Expand Down
4 changes: 3 additions & 1 deletion src/compare/libs/odiff/odiff.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export class OdiffService implements ImageComparator {

constructor(private staticService: StaticService) {
if (!isHddStaticServiceConfigured()) {
throw new Error('OdiffService can only be used with HddService');
return undefined;
// If we throw an exception, the application does not start.
// throw new Error('OdiffService can only be used with HddService');
}
this.hddService = this.staticService as unknown as HddService;
}
Expand Down
18 changes: 7 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { readFileSync, existsSync } from 'fs';
import { HttpsOptions } from '@nestjs/common/interfaces/external/https-options.interface';
import { NestExpressApplication } from '@nestjs/platform-express';
import { HDD_IMAGE_PATH } from './static/hdd/constants';
import { isHddStaticServiceConfigured } from './static/utils';

function getHttpsOptions(): HttpsOptions | null {
const keyPath = './secrets/ssl.key';
Expand All @@ -35,16 +34,13 @@ async function bootstrap() {
app.use(bodyParser.json({ limit: process.env.BODY_PARSER_JSON_LIMIT }));
}

// serve images only if hdd configuration
if (isHddStaticServiceConfigured()) {
app.useStaticAssets(join(process.cwd(), HDD_IMAGE_PATH), {
maxAge: 31536000,
// allow cors
setHeaders: (res) => {
res.set('Access-Control-Allow-Origin', '*');
},
});
}
app.useStaticAssets(join(process.cwd(), HDD_IMAGE_PATH), {
maxAge: 31536000,
// allow cors
setHeaders: (res) => {
res.set('Access-Control-Allow-Origin', '*');
},
});

await app.listen(process.env.APP_PORT || 3000);
}
Expand Down
73 changes: 50 additions & 23 deletions src/static/aws/s3.service.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,66 @@
import { PNGWithMetadata } from 'pngjs';
import { PNG, PNGWithMetadata } from 'pngjs';
import { Logger } from '@nestjs/common';
import { Static } from '../static.interface';
// import { S3Client } from '@aws-sdk/client-s3';
import { DeleteObjectCommand, GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
import { Readable } from 'stream';

export class AWSS3Service implements Static {
private readonly logger: Logger = new Logger(AWSS3Service.name);
// private readonly AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID;
// private readonly AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
// private readonly AWS_REGION = process.env.AWS_REGION;
// private readonly AWS_S3_BUCKET_NAME = process.env.AWS_S3_BUCKET_NAME;
private readonly AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID;
private readonly AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
private readonly AWS_REGION = process.env.AWS_REGION;
private readonly AWS_S3_BUCKET_NAME = process.env.AWS_S3_BUCKET_NAME;

// private s3Client: S3Client;
private s3Client: S3Client;

constructor() {
// this.s3Client = new S3Client({
// credentials: {
// accessKeyId: this.AWS_ACCESS_KEY_ID,
// secretAccessKey: this.AWS_SECRET_ACCESS_KEY,
// },
// region: this.AWS_REGION,
// });
this.s3Client = new S3Client({
credentials: {
accessKeyId: this.AWS_ACCESS_KEY_ID,
secretAccessKey: this.AWS_SECRET_ACCESS_KEY,
},
region: this.AWS_REGION,
});
this.logger.log('AWS S3 service is being used for file storage.');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
saveImage(type: 'screenshot' | 'diff' | 'baseline', imageBuffer: Buffer): Promise<string> {
throw new Error('Method not implemented.');

async saveImage(type: 'screenshot' | 'diff' | 'baseline', imageBuffer: Buffer): Promise<string> {
const imageName = `${Date.now()}.${type}.png`;
try {
await this.s3Client.send(
new PutObjectCommand({
Bucket: this.AWS_S3_BUCKET_NAME,
Key: imageName,
ContentType: 'image/png',
Body: imageBuffer,
})
);
return imageName;
} catch (ex) {
throw new Error('Could not save file at AWS S3 : ' + ex);
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
getImage(fileName: string): Promise<PNGWithMetadata> {
throw new Error('Method not implemented.');
async getImage(fileName: string): Promise<PNGWithMetadata> {
if (!fileName) return null;
try {
const command = new GetObjectCommand({ Bucket: this.AWS_S3_BUCKET_NAME, Key: fileName });
const s3Response = await this.s3Client.send(command);
const stream = s3Response.Body as Readable;
return PNG.sync.read(Buffer.concat(await stream.toArray()));
} catch (ex) {
this.logger.error(`Error from read : Cannot get image: ${fileName}. ${ex}`);
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
deleteImage(imageName: string): Promise<boolean> {
throw new Error('Method not implemented.');
async deleteImage(imageName: string): Promise<boolean> {
if (!imageName) return false;
try {
await this.s3Client.send(new DeleteObjectCommand({ Bucket: this.AWS_S3_BUCKET_NAME, Key: imageName }));
return true;
} catch (error) {
this.logger.log(`Failed to delete file at AWS S3 for image ${imageName}:`, error);
return false;
}
}
}
44 changes: 44 additions & 0 deletions src/static/static.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Controller, Get, Logger, Param, Res } from '@nestjs/common';
import { Response } from 'express';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { isHddStaticServiceConfigured, isS3ServiceConfigured } from './utils';
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';

@ApiTags('images')
@Controller('images')
export class StaticController {
private readonly logger: Logger = new Logger(StaticController.name);

@Get('/:fileName')
@ApiOkResponse()
async getUrlAndRedirect(@Param('fileName') fileName: string, @Res() res: Response) {
try {
if (isHddStaticServiceConfigured()) {
res.redirect('/' + fileName);
}
if (isS3ServiceConfigured()) {
const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID;
const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
const AWS_REGION = process.env.AWS_REGION;
const AWS_S3_BUCKET_NAME = process.env.AWS_S3_BUCKET_NAME;

const s3Client = new S3Client({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this logic to service layer like getImageUrl for both hdd and s3

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Done.

credentials: {
accessKeyId: `${AWS_ACCESS_KEY_ID}`,
secretAccessKey: `${AWS_SECRET_ACCESS_KEY}`,
},
region: `${AWS_REGION}`,
});
const command = new GetObjectCommand({
Bucket: `${AWS_S3_BUCKET_NAME}`,
Key: fileName,
});
res.redirect(await getSignedUrl(s3Client, command, { expiresIn: 3600 }));
}
} catch (error) {
this.logger.error('Error fetching file from S3:' + fileName, error);
res.status(500).send('Error occurred while getting the file.');
}
}
}
2 changes: 2 additions & 0 deletions src/static/static.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { StaticService } from './static.service';
import { StaticFactoryService } from './static.factory';
import { StaticController } from './static.controller';

@Module({
imports: [ConfigModule],
providers: [StaticService, StaticFactoryService],
exports: [StaticService],
controllers: [StaticController],
})
export class StaticModule {}
4 changes: 4 additions & 0 deletions src/static/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export function isHddStaticServiceConfigured() {
return !process.env.STATIC_SERVICE || process.env.STATIC_SERVICE === 'hdd';
}

export function isS3ServiceConfigured() {
return !process.env.STATIC_SERVICE || process.env.STATIC_SERVICE === 's3';
}
Loading