UNPKG

@slickteam/nestjs-cellar

Version:
75 lines 3.72 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CellarService = void 0; const client_s3_1 = require("@aws-sdk/client-s3"); const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner"); const common_1 = require("@nestjs/common"); const config_1 = require("@nestjs/config"); let CellarService = class CellarService { configService; timeoutSignedUrl; s3EndPoint; s3Client; constructor(configService) { this.configService = configService; this.s3EndPoint = `https://${this.configService.getOrThrow('CELLAR_HOST')}`; this.timeoutSignedUrl = this.configService.get('CELLAR_TIMEOUT_SIGNED_URL') ?? 3600; this.s3Client = new client_s3_1.S3Client({ endpoint: this.s3EndPoint, region: this.configService.get('CELLAR_REGION') ?? 'fr', credentials: { accessKeyId: this.configService.getOrThrow('CELLAR_KEY_ID'), secretAccessKey: this.configService.getOrThrow('CELLAR_KEY_SECRET'), }, }); } async listObjectsByBucketName(bucketName) { const result = await this.s3Client.send(new client_s3_1.ListObjectsV2Command({ Bucket: bucketName })); return (result.Contents?.map((object) => ({ name: object.Key ?? '', lastModified: object.LastModified, eTag: object.ETag ?? '', size: object.Size ?? 0, storageClass: object.StorageClass ?? '', })) ?? []); } async uploadFile(bucketName, file, acl = 'bucket-owner-full-control') { return this.s3Client.send(new client_s3_1.PutObjectCommand({ Bucket: bucketName, Key: file.originalname, Body: file.buffer, ACL: acl, ContentType: file.mimetype, })); } async getFile(bucketName, fileName) { return this.s3Client.send(new client_s3_1.GetObjectCommand({ Bucket: bucketName, Key: fileName })); } async deleteFile(bucketName, fileName) { return this.s3Client.send(new client_s3_1.DeleteObjectCommand({ Bucket: bucketName, Key: fileName })); } async fileExists(bucketName, fileName) { const result = await this.s3Client.send(new client_s3_1.HeadObjectCommand({ Bucket: bucketName, Key: fileName })); const statusCode = result.$metadata.httpStatusCode ?? 500; return statusCode >= 200 && statusCode < 400; } async getSignedUrl(bucketName, fileName) { const command = new client_s3_1.GetObjectCommand({ Bucket: bucketName, Key: fileName }); return (0, s3_request_presigner_1.getSignedUrl)(this.s3Client, command, { expiresIn: this.timeoutSignedUrl }); } }; exports.CellarService = CellarService; exports.CellarService = CellarService = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [config_1.ConfigService]) ], CellarService); //# sourceMappingURL=cellar.service.js.map