@wrcb/cb-common 1.0.471 → 1.0.472
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.
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/middlewares/uploadMedia.js +0 -85
- package/build/types/country.d.ts +2 -2
- package/build/types/country.js +4 -4
- package/build/types/subscriptionStatus.d.ts +7 -0
- package/build/types/subscriptionStatus.js +11 -0
- package/package.json +1 -1
- package/build/storage/services/storageService.d.ts +0 -11
- package/build/storage/services/storageService.js +0 -156
package/build/index.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export * from './types/feedPostType';
|
|
|
22
22
|
export * from './types/liveType';
|
|
23
23
|
export * from './types/liveStatus';
|
|
24
24
|
export * from './types/sessionStatus';
|
|
25
|
+
export * from './types/subscriptionStatus';
|
|
25
26
|
export * from './types/giftType';
|
|
26
27
|
export * from './types/paymentProviderType';
|
|
27
28
|
export * from './events/subjects';
|
package/build/index.js
CHANGED
|
@@ -38,6 +38,7 @@ __exportStar(require("./types/feedPostType"), exports);
|
|
|
38
38
|
__exportStar(require("./types/liveType"), exports);
|
|
39
39
|
__exportStar(require("./types/liveStatus"), exports);
|
|
40
40
|
__exportStar(require("./types/sessionStatus"), exports);
|
|
41
|
+
__exportStar(require("./types/subscriptionStatus"), exports);
|
|
41
42
|
__exportStar(require("./types/giftType"), exports);
|
|
42
43
|
__exportStar(require("./types/paymentProviderType"), exports);
|
|
43
44
|
__exportStar(require("./events/subjects"), exports);
|
|
@@ -9,9 +9,6 @@ const multer_1 = __importDefault(require("multer"));
|
|
|
9
9
|
const storage = multer_1.default.memoryStorage();
|
|
10
10
|
const upload = (0, multer_1.default)({ storage });
|
|
11
11
|
exports.uploadMedia = {
|
|
12
|
-
// single: (fieldName: string) => upload.single(fieldName),
|
|
13
|
-
// multiple: (fieldName: string, maxCount?: number) =>
|
|
14
|
-
// upload.array(fieldName, maxCount),
|
|
15
12
|
fields: (fields) => {
|
|
16
13
|
const multerFields = fields.map(({ name, maxCount }) => ({
|
|
17
14
|
name,
|
|
@@ -23,86 +20,4 @@ exports.uploadMedia = {
|
|
|
23
20
|
middleware(req, res, next);
|
|
24
21
|
};
|
|
25
22
|
}
|
|
26
|
-
// processUpload: async (req: Request, res: Response, next: NextFunction) => {
|
|
27
|
-
// try {
|
|
28
|
-
// const storageService = new StorageService(StorageFactory.create())
|
|
29
|
-
// const tenant = (req.headers['x-tenant'] as Tenant) || Tenant.PoliticaBet
|
|
30
|
-
// let allFiles: Express.Multer.File[] = []
|
|
31
|
-
// if (req.file) {
|
|
32
|
-
// allFiles = [req.file]
|
|
33
|
-
// } else if (req.files) {
|
|
34
|
-
// if (Array.isArray(req.files)) {
|
|
35
|
-
// allFiles = req.files
|
|
36
|
-
// } else {
|
|
37
|
-
// allFiles = Object.values(req.files).flat()
|
|
38
|
-
// }
|
|
39
|
-
// }
|
|
40
|
-
// if (!allFiles.length) return next()
|
|
41
|
-
// // ✅ Verificar se há mais de 5 imagens ou mais de 1 vídeo
|
|
42
|
-
// if (req.files && !Array.isArray(req.files)) {
|
|
43
|
-
// const imageFiles = req.files['images'] || []
|
|
44
|
-
// const videoFiles = req.files['videos'] || []
|
|
45
|
-
// if (imageFiles.length > 5) {
|
|
46
|
-
// throw new BadRequestError('TooManyImages')
|
|
47
|
-
// }
|
|
48
|
-
// if (videoFiles.length > 1) {
|
|
49
|
-
// throw new BadRequestError('TooManyVideos')
|
|
50
|
-
// }
|
|
51
|
-
// }
|
|
52
|
-
// // ✅ Verificar novamente via allFiles se alguém tentar burlar o `fieldname`
|
|
53
|
-
// const videoCount = allFiles.filter(f =>
|
|
54
|
-
// f.mimetype.startsWith('video/')
|
|
55
|
-
// ).length
|
|
56
|
-
// if (videoCount > 1) {
|
|
57
|
-
// throw new BadRequestError('TooManyVideos')
|
|
58
|
-
// }
|
|
59
|
-
// // Validação de tamanho por campo
|
|
60
|
-
// const fieldConfig = req._uploadFieldConfig || []
|
|
61
|
-
// const getMaxSizeForField = (fieldName: string): number | undefined => {
|
|
62
|
-
// const field = fieldConfig.find(f => f.name === fieldName)
|
|
63
|
-
// return field?.maxSizeMB ? field.maxSizeMB * 1024 * 1024 : undefined
|
|
64
|
-
// }
|
|
65
|
-
// for (const file of allFiles) {
|
|
66
|
-
// const isVideo = file.mimetype.startsWith('video/')
|
|
67
|
-
// const isImage = file.mimetype.startsWith('image/')
|
|
68
|
-
// if (!isImage && !isVideo) {
|
|
69
|
-
// throw new BadRequestError('InvalidFileType')
|
|
70
|
-
// }
|
|
71
|
-
// const maxSize = getMaxSizeForField(file.fieldname)
|
|
72
|
-
// if (maxSize && file.size > maxSize) {
|
|
73
|
-
// throw new BadRequestError(isImage ? 'ImageTooLarge' : 'VideoTooLarge')
|
|
74
|
-
// }
|
|
75
|
-
// }
|
|
76
|
-
// const uploadPromises = allFiles.map(async file => {
|
|
77
|
-
// const isVideo = file.mimetype.startsWith('video/')
|
|
78
|
-
// const type: MediaType = isVideo ? 'video' : 'photo'
|
|
79
|
-
// const options: UploadOptions = {
|
|
80
|
-
// tenant,
|
|
81
|
-
// userId: req.currentUser?.id,
|
|
82
|
-
// targetId: req.body.targetId || undefined,
|
|
83
|
-
// type,
|
|
84
|
-
// generateThumbnail: true
|
|
85
|
-
// }
|
|
86
|
-
// return isVideo
|
|
87
|
-
// ? storageService.uploadVideo(file.buffer, file.originalname, options)
|
|
88
|
-
// : storageService.uploadImage(file.buffer, file.originalname, options)
|
|
89
|
-
// })
|
|
90
|
-
// const results = await Promise.all(uploadPromises)
|
|
91
|
-
// req.uploadResults = results
|
|
92
|
-
// if (req.files && !Array.isArray(req.files)) {
|
|
93
|
-
// req.uploadResultsByField = {}
|
|
94
|
-
// let index = 0
|
|
95
|
-
// for (const [fieldName, files] of Object.entries(req.files)) {
|
|
96
|
-
// req.uploadResultsByField[fieldName] = results.slice(
|
|
97
|
-
// index,
|
|
98
|
-
// index + files.length
|
|
99
|
-
// )
|
|
100
|
-
// index += files.length
|
|
101
|
-
// }
|
|
102
|
-
// }
|
|
103
|
-
// next()
|
|
104
|
-
// } catch (error) {
|
|
105
|
-
// next(error)
|
|
106
|
-
// }
|
|
107
|
-
// }
|
|
108
23
|
};
|
package/build/types/country.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export declare enum TimeZone {
|
|
|
18
18
|
Argentina = "GMT-3"
|
|
19
19
|
}
|
|
20
20
|
export declare enum Currency {
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
BRL = "BRL",
|
|
22
|
+
ARS = "ARS"
|
|
23
23
|
}
|
|
24
24
|
export declare const countryInfoMap: Record<string, CountryInfo>;
|
package/build/types/country.js
CHANGED
|
@@ -19,19 +19,19 @@ var TimeZone;
|
|
|
19
19
|
})(TimeZone || (exports.TimeZone = TimeZone = {}));
|
|
20
20
|
var Currency;
|
|
21
21
|
(function (Currency) {
|
|
22
|
-
Currency["
|
|
23
|
-
Currency["
|
|
22
|
+
Currency["BRL"] = "BRL";
|
|
23
|
+
Currency["ARS"] = "ARS";
|
|
24
24
|
})(Currency || (exports.Currency = Currency = {}));
|
|
25
25
|
exports.countryInfoMap = {
|
|
26
26
|
[Country.Brasil]: {
|
|
27
27
|
country: Country.Brasil,
|
|
28
|
-
currency: Currency.
|
|
28
|
+
currency: Currency.BRL,
|
|
29
29
|
language: Language.Português,
|
|
30
30
|
timeZone: TimeZone.Brasil
|
|
31
31
|
},
|
|
32
32
|
[Country.Argentina]: {
|
|
33
33
|
country: Country.Argentina,
|
|
34
|
-
currency: Currency.
|
|
34
|
+
currency: Currency.ARS,
|
|
35
35
|
language: Language.Español,
|
|
36
36
|
timeZone: TimeZone.Argentina
|
|
37
37
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SubscriptionStatus = void 0;
|
|
4
|
+
var SubscriptionStatus;
|
|
5
|
+
(function (SubscriptionStatus) {
|
|
6
|
+
SubscriptionStatus["Active"] = "Active";
|
|
7
|
+
SubscriptionStatus["Cancelled"] = "Cancelled";
|
|
8
|
+
SubscriptionStatus["Expired"] = "Expired";
|
|
9
|
+
SubscriptionStatus["Pending"] = "Pending";
|
|
10
|
+
SubscriptionStatus["Past_due"] = "Past_due"; // pagamento falhou mas ainda tentando
|
|
11
|
+
})(SubscriptionStatus || (exports.SubscriptionStatus = SubscriptionStatus = {}));
|
package/package.json
CHANGED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { StorageProvider, UploadOptions, UploadResult } from '../types';
|
|
2
|
-
export declare class StorageService {
|
|
3
|
-
private provider;
|
|
4
|
-
constructor(provider: StorageProvider);
|
|
5
|
-
uploadImage(file: Buffer, filename: string, options: UploadOptions): Promise<UploadResult>;
|
|
6
|
-
uploadVideo(file: Buffer, filename: string, options: UploadOptions): Promise<UploadResult>;
|
|
7
|
-
private processImage;
|
|
8
|
-
private generatePath;
|
|
9
|
-
private getVideoMetadata;
|
|
10
|
-
private generateVideoThumbnail;
|
|
11
|
-
}
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
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.StorageService = void 0;
|
|
16
|
-
// src/storage/services/storageService.ts
|
|
17
|
-
const sharp_1 = __importDefault(require("sharp"));
|
|
18
|
-
const fluent_ffmpeg_1 = __importDefault(require("fluent-ffmpeg"));
|
|
19
|
-
const fs_1 = require("fs"); // Importar promises do fs
|
|
20
|
-
const uuid_1 = require("uuid");
|
|
21
|
-
const TenantDataService_1 = require("../../services/TenantDataService");
|
|
22
|
-
const badRequestError_1 = require("../../errors/badRequestError");
|
|
23
|
-
class StorageService {
|
|
24
|
-
constructor(provider) {
|
|
25
|
-
this.provider = provider;
|
|
26
|
-
}
|
|
27
|
-
uploadImage(file, filename, options) {
|
|
28
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
-
const tenantData = TenantDataService_1.TenantDataService.getTenantData(options.tenant);
|
|
30
|
-
const config = tenantData.MEDIA_CONFIG;
|
|
31
|
-
// Validar tamanho
|
|
32
|
-
const sizeMB = file.length / (1024 * 1024);
|
|
33
|
-
if (sizeMB > config.maxPhotoSizeMB) {
|
|
34
|
-
throw new badRequestError_1.BadRequestError('ImageSizeExceeds');
|
|
35
|
-
}
|
|
36
|
-
// Validar formato
|
|
37
|
-
const metadata = yield (0, sharp_1.default)(file).metadata();
|
|
38
|
-
const format = metadata.format;
|
|
39
|
-
if (!format || !config.allowedImageFormats.includes(format)) {
|
|
40
|
-
throw new badRequestError_1.BadRequestError('InvalidImageFormat');
|
|
41
|
-
}
|
|
42
|
-
// Processar imagem - criar versões otimizadas
|
|
43
|
-
const versions = yield this.processImage(file, config);
|
|
44
|
-
// Upload de cada versão
|
|
45
|
-
const uploadPromises = Object.entries(versions).map(([size, buffer]) => {
|
|
46
|
-
const path = this.generatePath(options, filename, size);
|
|
47
|
-
return this.provider.upload(buffer, path, options);
|
|
48
|
-
});
|
|
49
|
-
const results = yield Promise.all(uploadPromises);
|
|
50
|
-
// Retornar a versão large como principal, com thumbnail
|
|
51
|
-
const mainResult = results.find(r => r.id.includes('large'));
|
|
52
|
-
const thumbResult = results.find(r => r.id.includes('thumbnail'));
|
|
53
|
-
return Object.assign(Object.assign({}, mainResult), { metadata: Object.assign(Object.assign({}, mainResult.metadata), { thumbnailUrl: thumbResult === null || thumbResult === void 0 ? void 0 : thumbResult.metadata.url }) });
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
uploadVideo(file, filename, options) {
|
|
57
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
-
const tenantData = TenantDataService_1.TenantDataService.getTenantData(options.tenant);
|
|
59
|
-
const config = tenantData.MEDIA_CONFIG;
|
|
60
|
-
// Validar tamanho
|
|
61
|
-
const sizeMB = file.length / (1024 * 1024);
|
|
62
|
-
if (sizeMB > config.maxVideoSizeMB) {
|
|
63
|
-
throw new badRequestError_1.BadRequestError('VideoSizeExceeds');
|
|
64
|
-
}
|
|
65
|
-
// Salvar temporariamente para processar com ffmpeg
|
|
66
|
-
const tempPath = `/tmp/${(0, uuid_1.v4)()}-${filename}`;
|
|
67
|
-
yield fs_1.promises.writeFile(tempPath, file);
|
|
68
|
-
try {
|
|
69
|
-
// Obter metadados do vídeo
|
|
70
|
-
const metadata = yield this.getVideoMetadata(tempPath);
|
|
71
|
-
// Gerar thumbnail se solicitado
|
|
72
|
-
let thumbnailUrl;
|
|
73
|
-
if (options.generateThumbnail) {
|
|
74
|
-
const thumbnail = yield this.generateVideoThumbnail(tempPath);
|
|
75
|
-
const thumbPath = this.generatePath(options, filename, 'thumbnail');
|
|
76
|
-
const thumbResult = yield this.provider.upload(thumbnail, thumbPath, options);
|
|
77
|
-
thumbnailUrl = thumbResult.metadata.url;
|
|
78
|
-
}
|
|
79
|
-
// Upload do vídeo
|
|
80
|
-
const videoPath = this.generatePath(options, filename, 'original');
|
|
81
|
-
const result = yield this.provider.upload(file, videoPath, options);
|
|
82
|
-
return Object.assign(Object.assign({}, result), { metadata: Object.assign(Object.assign(Object.assign({}, result.metadata), metadata), { thumbnailUrl }) });
|
|
83
|
-
}
|
|
84
|
-
finally {
|
|
85
|
-
// Limpar arquivo temporário
|
|
86
|
-
yield fs_1.promises.unlink(tempPath).catch(() => { });
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
processImage(file, config) {
|
|
91
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
-
const versions = {};
|
|
93
|
-
for (const [size, settings] of Object.entries(config.imageQualities)) {
|
|
94
|
-
versions[size] = yield (0, sharp_1.default)(file)
|
|
95
|
-
.resize(settings.width, settings.height, {
|
|
96
|
-
fit: 'inside',
|
|
97
|
-
withoutEnlargement: true
|
|
98
|
-
})
|
|
99
|
-
.jpeg({ quality: settings.quality })
|
|
100
|
-
.toBuffer();
|
|
101
|
-
}
|
|
102
|
-
return versions;
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
generatePath(options, filename, variant) {
|
|
106
|
-
const parts = [options.tenant]; // Declarar como array de strings
|
|
107
|
-
if (options.userId) {
|
|
108
|
-
parts.push('users', options.userId);
|
|
109
|
-
}
|
|
110
|
-
else if (options.targetId) {
|
|
111
|
-
parts.push('targets', options.targetId);
|
|
112
|
-
}
|
|
113
|
-
parts.push(options.type);
|
|
114
|
-
parts.push(variant);
|
|
115
|
-
parts.push(`${(0, uuid_1.v4)()}-${filename}`);
|
|
116
|
-
return parts.join('/');
|
|
117
|
-
}
|
|
118
|
-
getVideoMetadata(path) {
|
|
119
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
-
return new Promise((resolve, reject) => {
|
|
121
|
-
fluent_ffmpeg_1.default.ffprobe(path, (err, metadata) => {
|
|
122
|
-
if (err)
|
|
123
|
-
reject(err);
|
|
124
|
-
else {
|
|
125
|
-
const video = metadata.streams.find(s => s.codec_type === 'video');
|
|
126
|
-
resolve({
|
|
127
|
-
duration: metadata.format.duration,
|
|
128
|
-
width: video === null || video === void 0 ? void 0 : video.width,
|
|
129
|
-
height: video === null || video === void 0 ? void 0 : video.height
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
generateVideoThumbnail(videoPath) {
|
|
137
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
138
|
-
const outputPath = `/tmp/thumb-${(0, uuid_1.v4)()}.jpg`;
|
|
139
|
-
return new Promise((resolve, reject) => {
|
|
140
|
-
(0, fluent_ffmpeg_1.default)(videoPath)
|
|
141
|
-
.screenshots({
|
|
142
|
-
timestamps: ['50%'],
|
|
143
|
-
filename: outputPath,
|
|
144
|
-
size: '320x240'
|
|
145
|
-
})
|
|
146
|
-
.on('end', () => __awaiter(this, void 0, void 0, function* () {
|
|
147
|
-
const buffer = yield fs_1.promises.readFile(outputPath);
|
|
148
|
-
yield fs_1.promises.unlink(outputPath).catch(() => { });
|
|
149
|
-
resolve(buffer);
|
|
150
|
-
}))
|
|
151
|
-
.on('error', reject);
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
exports.StorageService = StorageService;
|