@maioradv/nestjs-core 1.1.2 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ export type AWSConfig = {
2
+ s3sdk?: {
3
+ region: string;
4
+ bucketName: string;
5
+ accessKeyId: string;
6
+ secretAccessKey: string;
7
+ folder: string;
8
+ baseUrl: string;
9
+ };
10
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export * from './aws.config';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./aws.config"), exports);
package/dist/index.d.ts CHANGED
@@ -7,3 +7,5 @@ export * from './logger';
7
7
  export * from './guards';
8
8
  export * from './model';
9
9
  export * from './providers';
10
+ export * from './config';
11
+ export * from './validators';
package/dist/index.js CHANGED
@@ -23,3 +23,5 @@ __exportStar(require("./logger"), exports);
23
23
  __exportStar(require("./guards"), exports);
24
24
  __exportStar(require("./model"), exports);
25
25
  __exportStar(require("./providers"), exports);
26
+ __exportStar(require("./config"), exports);
27
+ __exportStar(require("./validators"), exports);
@@ -1 +1,2 @@
1
1
  export * from './crypt.service';
2
+ export * from './s3.service';
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./crypt.service"), exports);
18
+ __exportStar(require("./s3.service"), exports);
@@ -0,0 +1,21 @@
1
+ /// <reference types="multer" />
2
+ import { ConfigService } from "@nestjs/config";
3
+ import { S3Client } from "@aws-sdk/client-s3";
4
+ export type UploadImageResponse = {
5
+ src: string;
6
+ height: number;
7
+ width: number;
8
+ checksum: string;
9
+ fileName: string;
10
+ mimeType: string;
11
+ size: number;
12
+ };
13
+ export declare class S3Service {
14
+ private readonly config;
15
+ readonly client: S3Client;
16
+ private sdkConfigs;
17
+ constructor(config: ConfigService);
18
+ uploadImage(file: Express.Multer.File): Promise<UploadImageResponse>;
19
+ removeFile(fileName: string): Promise<import("@aws-sdk/client-s3").DeleteObjectCommandOutput>;
20
+ private randomString;
21
+ }
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.S3Service = void 0;
16
+ const common_1 = require("@nestjs/common");
17
+ const config_1 = require("@nestjs/config");
18
+ const client_s3_1 = require("@aws-sdk/client-s3");
19
+ const crypto_1 = require("crypto");
20
+ const image_size_1 = __importDefault(require("image-size"));
21
+ let S3Service = class S3Service {
22
+ constructor(config) {
23
+ this.config = config;
24
+ this.sdkConfigs = this.config.get('aws.s3sdk');
25
+ this.client = new client_s3_1.S3Client({
26
+ region: this.sdkConfigs.region,
27
+ credentials: {
28
+ accessKeyId: this.sdkConfigs.accessKeyId,
29
+ secretAccessKey: this.sdkConfigs.secretAccessKey
30
+ }
31
+ });
32
+ }
33
+ async uploadImage(file) {
34
+ let fileName = `${this.randomString(6)}-${file.originalname}`;
35
+ const response = await this.client.send(new client_s3_1.PutObjectCommand({
36
+ Bucket: this.sdkConfigs.bucketName,
37
+ Key: `${this.sdkConfigs.folder}/${fileName}`,
38
+ Body: file.buffer,
39
+ }));
40
+ const size = (0, image_size_1.default)(file.buffer);
41
+ const checksum = (0, crypto_1.createHash)('md5').update(file.buffer).digest("base64");
42
+ return {
43
+ src: `${this.sdkConfigs.baseUrl}/${this.sdkConfigs.folder}/${fileName}`,
44
+ width: size.width,
45
+ height: size.height,
46
+ checksum: checksum,
47
+ fileName: fileName,
48
+ mimeType: file.mimetype,
49
+ size: file.size
50
+ };
51
+ }
52
+ async removeFile(fileName) {
53
+ return this.client.send(new client_s3_1.DeleteObjectCommand({
54
+ Bucket: this.sdkConfigs.bucketName,
55
+ Key: `${this.sdkConfigs.folder}/${fileName}`,
56
+ }));
57
+ }
58
+ randomString(length) {
59
+ let result = '';
60
+ const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
61
+ const charactersLength = characters.length;
62
+ for (let i = 0; i < length; i++) {
63
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
64
+ }
65
+ return result;
66
+ }
67
+ };
68
+ exports.S3Service = S3Service;
69
+ exports.S3Service = S3Service = __decorate([
70
+ (0, common_1.Injectable)(),
71
+ __metadata("design:paramtypes", [config_1.ConfigService])
72
+ ], S3Service);
@@ -0,0 +1,7 @@
1
+ export declare const DEFAULT_MAX_IMAGE_SIZE = 2;
2
+ export declare const DEFAULT_MIMETYPES_SUPPORT = ".(webp|avif|png|gif|jpeg|jpg)";
3
+ export type ImagePipeValidatorOptions = {
4
+ maxSize?: number;
5
+ mimeTypes?: ('webp' | 'avif' | 'png' | 'gif' | 'jpeg' | 'jpg')[];
6
+ };
7
+ export declare const ImagePipeValidator: (options?: ImagePipeValidatorOptions) => import("@nestjs/common").ParseFilePipe;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ImagePipeValidator = exports.DEFAULT_MIMETYPES_SUPPORT = exports.DEFAULT_MAX_IMAGE_SIZE = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ exports.DEFAULT_MAX_IMAGE_SIZE = 2;
6
+ exports.DEFAULT_MIMETYPES_SUPPORT = '.(webp|avif|png|gif|jpeg|jpg)';
7
+ const ImagePipeValidator = (options) => new common_1.ParseFilePipeBuilder()
8
+ .addFileTypeValidator({ fileType: options?.mimeTypes ? `.(${options.mimeTypes.join('|')})` : exports.DEFAULT_MIMETYPES_SUPPORT })
9
+ .addMaxSizeValidator({ maxSize: (options?.maxSize ?? exports.DEFAULT_MAX_IMAGE_SIZE) * 8e6 })
10
+ .build({ errorHttpStatusCode: common_1.HttpStatus.UNPROCESSABLE_ENTITY });
11
+ exports.ImagePipeValidator = ImagePipeValidator;
@@ -0,0 +1 @@
1
+ export * from './image-pipe.validator';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./image-pipe.validator"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maioradv/nestjs-core",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "NestJS helpers by MaiorADV",
5
5
  "repository": "https://github.com/maioradv/nestjs-core.git",
6
6
  "author": "Maior ADV Srl",
@@ -16,6 +16,7 @@
16
16
  "publish:npm": "npm publish --access public"
17
17
  },
18
18
  "dependencies": {
19
+ "@aws-sdk/client-s3": "^3.608.0",
19
20
  "@nestjs/common": "^10.3.8",
20
21
  "@nestjs/config": "^3.2.2",
21
22
  "@nestjs/graphql": "^12.1.1",
@@ -25,24 +26,29 @@
25
26
  "bcrypt": "^5.1.1",
26
27
  "class-transformer": "^0.5.1",
27
28
  "class-validator": "^0.14.1",
29
+ "image-size": "^1.1.1",
28
30
  "nest-winston": "^1.10.0",
29
31
  "winston": "^3.13.0",
30
32
  "winston-daily-rotate-file": "^5.0.0"
31
33
  },
32
34
  "devDependencies": {
35
+ "@types/multer": "^1.4.11",
33
36
  "@types/node": "^20.12.13",
34
37
  "ts-node": "^10.9.2",
35
38
  "typescript": "^5.4.5"
36
39
  },
37
40
  "peerDependencies": {
41
+ "@aws-sdk/client-s3": "^3.608.0",
38
42
  "@nestjs/common": "^10.3.8",
39
43
  "@nestjs/config": "^3.2.2",
40
44
  "@nestjs/graphql": "^12.1.1",
41
45
  "@nestjs/platform-express": "^10.0.0",
42
46
  "@nestjs/swagger": "^7.3.1",
47
+ "@types/multer": "^1.4.11",
43
48
  "bcrypt": "^5.1.1",
44
49
  "class-transformer": "^0.5.1",
45
50
  "class-validator": "^0.14.1",
51
+ "image-size": "^1.1.1",
46
52
  "nest-winston": "^1.10.0",
47
53
  "winston": "^3.13.0",
48
54
  "winston-daily-rotate-file": "^5.0.0"