|
| 1 | +import axios from 'axios'; |
| 2 | +import { execSync } from 'child_process'; |
| 3 | + |
| 4 | +import { Auth, ConfigService, ProviderSession } from '../../config/env.config'; |
| 5 | +import { Logger } from '../../config/logger.config'; |
| 6 | + |
| 7 | +type ResponseSuccess = { status: number; data?: any }; |
| 8 | +type ResponseProvider = Promise<[ResponseSuccess?, Error?]>; |
| 9 | + |
| 10 | +export class ProviderFiles { |
| 11 | + constructor(private readonly configService: ConfigService) { |
| 12 | + this.baseUrl = `http://${this.config.HOST}:${this.config.PORT}/session/${this.config.PREFIX}`; |
| 13 | + this.globalApiToken = this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY; |
| 14 | + } |
| 15 | + |
| 16 | + private readonly logger = new Logger(ProviderFiles.name); |
| 17 | + |
| 18 | + private baseUrl: string; |
| 19 | + private globalApiToken: string; |
| 20 | + |
| 21 | + private readonly config = Object.freeze(this.configService.get<ProviderSession>('PROVIDER')); |
| 22 | + |
| 23 | + get isEnabled() { |
| 24 | + return !!this.config?.ENABLED; |
| 25 | + } |
| 26 | + |
| 27 | + public async onModuleInit() { |
| 28 | + if (this.config.ENABLED) { |
| 29 | + const url = `http://${this.config.HOST}:${this.config.PORT}`; |
| 30 | + try { |
| 31 | + const response = await axios.options(url + '/ping'); |
| 32 | + if (response?.data != 'pong') { |
| 33 | + throw new Error('Offline file provider.'); |
| 34 | + } |
| 35 | + |
| 36 | + await axios.post(`${url}/session`, { group: this.config.PREFIX }, { headers: { apikey: this.globalApiToken } }); |
| 37 | + } catch (error) { |
| 38 | + this.logger.error(['Failed to connect to the file server', error?.message, error?.stack]); |
| 39 | + const pid = process.pid; |
| 40 | + execSync(`kill -9 ${pid}`); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public async onModuleDestroy() { |
| 46 | + // |
| 47 | + } |
| 48 | + |
| 49 | + public async create(instance: string): ResponseProvider { |
| 50 | + try { |
| 51 | + const response = await axios.post( |
| 52 | + `${this.baseUrl}`, |
| 53 | + { |
| 54 | + instance, |
| 55 | + }, |
| 56 | + { headers: { apikey: this.globalApiToken } }, |
| 57 | + ); |
| 58 | + return [{ status: response.status, data: response?.data }]; |
| 59 | + } catch (error) { |
| 60 | + return [ |
| 61 | + { |
| 62 | + status: error?.response?.status, |
| 63 | + data: error?.response?.data, |
| 64 | + }, |
| 65 | + error, |
| 66 | + ]; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public async write(instance: string, key: string, data: any): ResponseProvider { |
| 71 | + try { |
| 72 | + const response = await axios.post(`${this.baseUrl}/${instance}/${key}`, data, { |
| 73 | + headers: { apikey: this.globalApiToken }, |
| 74 | + }); |
| 75 | + return [{ status: response.status, data: response?.data }]; |
| 76 | + } catch (error) { |
| 77 | + return [ |
| 78 | + { |
| 79 | + status: error?.response?.status, |
| 80 | + data: error?.response?.data, |
| 81 | + }, |
| 82 | + error, |
| 83 | + ]; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public async read(instance: string, key: string): ResponseProvider { |
| 88 | + try { |
| 89 | + const response = await axios.get(`${this.baseUrl}/${instance}/${key}`, { |
| 90 | + headers: { apikey: this.globalApiToken }, |
| 91 | + }); |
| 92 | + return [{ status: response.status, data: response?.data }]; |
| 93 | + } catch (error) { |
| 94 | + return [ |
| 95 | + { |
| 96 | + status: error?.response?.status, |
| 97 | + data: error?.response?.data, |
| 98 | + }, |
| 99 | + error, |
| 100 | + ]; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public async delete(instance: string, key: string): ResponseProvider { |
| 105 | + try { |
| 106 | + const response = await axios.delete(`${this.baseUrl}/${instance}/${key}`, { |
| 107 | + headers: { apikey: this.globalApiToken }, |
| 108 | + }); |
| 109 | + return [{ status: response.status, data: response?.data }]; |
| 110 | + } catch (error) { |
| 111 | + return [ |
| 112 | + { |
| 113 | + status: error?.response?.status, |
| 114 | + data: error?.response?.data, |
| 115 | + }, |
| 116 | + error, |
| 117 | + ]; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public async allInstances(): ResponseProvider { |
| 122 | + try { |
| 123 | + const response = await axios.get(`${this.baseUrl}/list-instances`, { headers: { apikey: this.globalApiToken } }); |
| 124 | + return [{ status: response.status, data: response?.data as string[] }]; |
| 125 | + } catch (error) { |
| 126 | + return [ |
| 127 | + { |
| 128 | + status: error?.response?.status, |
| 129 | + data: error?.response?.data, |
| 130 | + }, |
| 131 | + error, |
| 132 | + ]; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public async removeSession(instance: string): ResponseProvider { |
| 137 | + try { |
| 138 | + const response = await axios.delete(`${this.baseUrl}/${instance}`, { headers: { apikey: this.globalApiToken } }); |
| 139 | + return [{ status: response.status, data: response?.data }]; |
| 140 | + } catch (error) { |
| 141 | + return [ |
| 142 | + { |
| 143 | + status: error?.response?.status, |
| 144 | + data: error?.response?.data, |
| 145 | + }, |
| 146 | + error, |
| 147 | + ]; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments