|
1 | | -import { PNGWithMetadata } from 'pngjs'; |
| 1 | +import { PNG, PNGWithMetadata } from 'pngjs'; |
2 | 2 | import { Logger } from '@nestjs/common'; |
3 | 3 | import { Static } from '../static.interface'; |
4 | | -// import { S3Client } from '@aws-sdk/client-s3'; |
| 4 | +import { DeleteObjectCommand, GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3'; |
| 5 | +import { Readable } from 'stream'; |
| 6 | +import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; |
5 | 7 |
|
6 | 8 | export class AWSS3Service implements Static { |
7 | 9 | private readonly logger: Logger = new Logger(AWSS3Service.name); |
8 | | - // private readonly AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID; |
9 | | - // private readonly AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY; |
10 | | - // private readonly AWS_REGION = process.env.AWS_REGION; |
11 | | - // private readonly AWS_S3_BUCKET_NAME = process.env.AWS_S3_BUCKET_NAME; |
| 10 | + private readonly AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID; |
| 11 | + private readonly AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY; |
| 12 | + private readonly AWS_REGION = process.env.AWS_REGION; |
| 13 | + private readonly AWS_S3_BUCKET_NAME = process.env.AWS_S3_BUCKET_NAME; |
12 | 14 |
|
13 | | - // private s3Client: S3Client; |
| 15 | + private s3Client: S3Client; |
14 | 16 |
|
15 | 17 | constructor() { |
16 | | - // this.s3Client = new S3Client({ |
17 | | - // credentials: { |
18 | | - // accessKeyId: this.AWS_ACCESS_KEY_ID, |
19 | | - // secretAccessKey: this.AWS_SECRET_ACCESS_KEY, |
20 | | - // }, |
21 | | - // region: this.AWS_REGION, |
22 | | - // }); |
| 18 | + this.s3Client = new S3Client({ |
| 19 | + credentials: { |
| 20 | + accessKeyId: this.AWS_ACCESS_KEY_ID, |
| 21 | + secretAccessKey: this.AWS_SECRET_ACCESS_KEY, |
| 22 | + }, |
| 23 | + region: this.AWS_REGION, |
| 24 | + }); |
23 | 25 | this.logger.log('AWS S3 service is being used for file storage.'); |
24 | 26 | } |
25 | | - // eslint-disable-next-line @typescript-eslint/no-unused-vars |
26 | | - saveImage(type: 'screenshot' | 'diff' | 'baseline', imageBuffer: Buffer): Promise<string> { |
27 | | - throw new Error('Method not implemented.'); |
| 27 | + |
| 28 | + async saveImage(type: 'screenshot' | 'diff' | 'baseline', imageBuffer: Buffer): Promise<string> { |
| 29 | + const imageName = `${Date.now()}.${type}.png`; |
| 30 | + try { |
| 31 | + await this.s3Client.send( |
| 32 | + new PutObjectCommand({ |
| 33 | + Bucket: this.AWS_S3_BUCKET_NAME, |
| 34 | + Key: imageName, |
| 35 | + ContentType: 'image/png', |
| 36 | + Body: imageBuffer, |
| 37 | + }) |
| 38 | + ); |
| 39 | + return imageName; |
| 40 | + } catch (ex) { |
| 41 | + throw new Error('Could not save file at AWS S3 : ' + ex); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + async getImage(fileName: string): Promise<PNGWithMetadata> { |
| 46 | + if (!fileName) return null; |
| 47 | + try { |
| 48 | + const command = new GetObjectCommand({ Bucket: this.AWS_S3_BUCKET_NAME, Key: fileName }); |
| 49 | + const s3Response = await this.s3Client.send(command); |
| 50 | + const stream = s3Response.Body as Readable; |
| 51 | + return PNG.sync.read(Buffer.concat(await stream.toArray())); |
| 52 | + } catch (ex) { |
| 53 | + this.logger.error(`Error from read : Cannot get image: ${fileName}. ${ex}`); |
| 54 | + } |
28 | 55 | } |
29 | 56 |
|
30 | | - // eslint-disable-next-line @typescript-eslint/no-unused-vars |
31 | | - getImage(fileName: string): Promise<PNGWithMetadata> { |
32 | | - throw new Error('Method not implemented.'); |
| 57 | + async getImageUrl(imageName: string): Promise<string> { |
| 58 | + const command = new GetObjectCommand({ |
| 59 | + Bucket: `${this.AWS_S3_BUCKET_NAME}`, |
| 60 | + Key: imageName, |
| 61 | + }); |
| 62 | + return getSignedUrl(this.s3Client, command, { expiresIn: 3600 }); |
33 | 63 | } |
34 | 64 |
|
35 | | - // eslint-disable-next-line @typescript-eslint/no-unused-vars |
36 | | - deleteImage(imageName: string): Promise<boolean> { |
37 | | - throw new Error('Method not implemented.'); |
| 65 | + async deleteImage(imageName: string): Promise<boolean> { |
| 66 | + if (!imageName) return false; |
| 67 | + try { |
| 68 | + await this.s3Client.send(new DeleteObjectCommand({ Bucket: this.AWS_S3_BUCKET_NAME, Key: imageName })); |
| 69 | + return true; |
| 70 | + } catch (error) { |
| 71 | + this.logger.log(`Failed to delete file at AWS S3 for image ${imageName}:`, error); |
| 72 | + return false; |
| 73 | + } |
38 | 74 | } |
39 | 75 | } |
0 commit comments