|
| 1 | +/** |
| 2 | + * ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| 3 | + * β @author jrCleber β |
| 4 | + * β @filename message.model.ts β |
| 5 | + * β Developed by: Cleber Wilson β |
| 6 | + * β Creation date: Dez 05, 2023 β |
| 7 | + * β Contact: contato@codechat.dev β |
| 8 | + * ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ |
| 9 | + * β @copyright Β© Cleber Wilson 2022. All rights reserved. β |
| 10 | + * β Licensed under the Apache License, Version 2.0 β |
| 11 | + * β β |
| 12 | + * β @license "https://github.com/code-chat-br/whatsapp-api/blob/main/LICENSE" β |
| 13 | + * β β |
| 14 | + * β You may not use this file except in compliance with the License. β |
| 15 | + * β You may obtain a copy of the License at β |
| 16 | + * β β |
| 17 | + * β http://www.apache.org/licenses/LICENSE-2.0 β |
| 18 | + * β β |
| 19 | + * β Unless required by applicable law or agreed to in writing, software β |
| 20 | + * β distributed under the License is distributed on an "AS IS" BASIS, β |
| 21 | + * β WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. β |
| 22 | + * β β |
| 23 | + * β See the License for the specific language governing permissions and β |
| 24 | + * β limitations under the License. β |
| 25 | + * β β |
| 26 | + * β @function bucketExists @function createBucket β |
| 27 | + * β @function getObjectUrl @function uploadFile β |
| 28 | + * β @constant minioClient @constant BUCKET β |
| 29 | + * ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ |
| 30 | + * β @important β |
| 31 | + * β For any future changes to the code in this file, it is recommended to β |
| 32 | + * β contain, together with the modification, the information of the developer β |
| 33 | + * β who changed it and the date of modification. β |
| 34 | + * ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| 35 | + */ |
| 36 | + |
| 37 | +import * as MinIo from 'minio'; |
| 38 | +import { join } from 'path'; |
| 39 | +import { Readable, Transform } from 'stream'; |
| 40 | +import { BadRequestException } from '../../exceptions'; |
| 41 | +import { Bucket, ConfigService } from '../../config/env.config'; |
| 42 | + |
| 43 | +const BUCKET = new ConfigService().get<Bucket>('S3'); |
| 44 | + |
| 45 | +type Metadata = { |
| 46 | + 'Content-Type': string; |
| 47 | + 'custom-header-keyRemoteJid': string; |
| 48 | + 'custom-header-pushName': string; |
| 49 | + 'custom-header-messageId': string; |
| 50 | + 'custom-header-fromMe': string; |
| 51 | + 'custom-header-mediaType': string; |
| 52 | +}; |
| 53 | + |
| 54 | +const minioClient = new MinIo.Client({ |
| 55 | + endPoint: BUCKET.ENDPOINT, |
| 56 | + port: BUCKET.PORT, |
| 57 | + useSSL: BUCKET.USE_SSL, |
| 58 | + accessKey: BUCKET.ACCESS_KEY, |
| 59 | + secretKey: BUCKET.SECRET_KEY, |
| 60 | +}); |
| 61 | + |
| 62 | +const bucketName = process.env.S3_BUCKET; |
| 63 | + |
| 64 | +const bucketExists = async () => { |
| 65 | + try { |
| 66 | + const list = await minioClient.listBuckets(); |
| 67 | + return list.find((bucket) => bucket.name === bucketName); |
| 68 | + } catch (error) { |
| 69 | + return false; |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +const createBucket = async () => { |
| 74 | + try { |
| 75 | + const exists = await bucketExists(); |
| 76 | + if (!exists) { |
| 77 | + await minioClient.makeBucket(bucketName); |
| 78 | + } |
| 79 | + return true; |
| 80 | + } catch (error) { |
| 81 | + return false; |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +createBucket(); |
| 86 | + |
| 87 | +const uploadFile = async ( |
| 88 | + fileName: string, |
| 89 | + file: Buffer | Transform | Readable, |
| 90 | + metadata: Metadata, |
| 91 | +) => { |
| 92 | + const objectName = join('codechat_v1', fileName); |
| 93 | + try { |
| 94 | + metadata['custom-header-application'] = 'codechat-api-v1'; |
| 95 | + const o = await minioClient.putObject(bucketName, objectName, file, metadata); |
| 96 | + |
| 97 | + await minioClient.setObjectTagging(bucketName, objectName, { |
| 98 | + mediaType: metadata['custom-header-mediaType'], |
| 99 | + application: metadata['custom-header-application'], |
| 100 | + sender: metadata['custom-header-keyRemoteJid'], |
| 101 | + contentType: metadata['Content-Type'], |
| 102 | + fromMe: metadata['custom-header-fromMe'], |
| 103 | + }); |
| 104 | + return o; |
| 105 | + } catch (error) { |
| 106 | + console.log('ERROR: ', error); |
| 107 | + return error; |
| 108 | + } |
| 109 | +}; |
| 110 | + |
| 111 | +const getObjectUrl = async (fileName: string, expiry?: number) => { |
| 112 | + try { |
| 113 | + const objectName = join('codechat_v1', fileName); |
| 114 | + if (expiry) { |
| 115 | + return await minioClient.presignedGetObject(bucketName, objectName, expiry); |
| 116 | + } |
| 117 | + return await minioClient.presignedGetObject(bucketName, objectName); |
| 118 | + } catch (error) { |
| 119 | + throw new BadRequestException(error?.message); |
| 120 | + } |
| 121 | +}; |
| 122 | + |
| 123 | +export { uploadFile, getObjectUrl, BUCKET }; |
0 commit comments