|
@@ -8,6 +8,7 @@ import { ConnectionState, Transport, TransportEvent, TransportMessage } from '..
|
|
|
import { ConnectedSocketClient, ConnectedSocketServer } from '../transport/websocket';
|
|
|
import { EventMessage } from '../interface/transport.interface';
|
|
|
import ConsoleLogger from './log.utils';
|
|
|
+import path from 'path';
|
|
|
const console: ConsoleLogger = new ConsoleLogger(`SocketUtils`, ['transport'])
|
|
|
|
|
|
export function startSocketServer(port: number): Observable<SocketForConnectedClient> {
|
|
@@ -259,28 +260,39 @@ export function handleNewSocketClient(socket: SocketForConnectedClient, connecte
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-// Specifically to write receiver profile information
|
|
|
-export async function writeFile(data: ConnectedSocketServer, filename: string): Promise<boolean> {
|
|
|
+async function writeFile(data: ConnectedSocketServer, filename: string): Promise<boolean> {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
+ // Ensure the folder exists
|
|
|
+ const folderPath = process.env.FolderPath as string
|
|
|
+ console.log({ message: folderPath })
|
|
|
+ // const folderPath = path.join(__dirname, folder);
|
|
|
+ if (!fs.existsSync(folderPath)) {
|
|
|
+ fs.mkdirSync(folderPath, { recursive: true }); // Create folder if it doesn't exist
|
|
|
+ } else {
|
|
|
+ console.log({ message: 'Folder already exist' })
|
|
|
+ }
|
|
|
+
|
|
|
+ // Construct the full file path (include the folder)
|
|
|
+ const filePath = path.join(folderPath, `${filename}.json`);
|
|
|
+
|
|
|
// Write JSON data to a file
|
|
|
- fs.writeFile(`${filename}.json`, JSON.stringify(data, null, 2), (err) => {
|
|
|
+ fs.writeFile(filePath, JSON.stringify(data, null, 2), (err) => {
|
|
|
if (err) {
|
|
|
- console.log({ message: 'Error writing file', details: err })
|
|
|
- reject(false)
|
|
|
+ console.log({ message: 'Error writing file', details: err });
|
|
|
+ reject(false);
|
|
|
} else {
|
|
|
- console.log({ message: 'File has been written', details: err })
|
|
|
- resolve(true)
|
|
|
+ console.log({ message: 'File has been written', details: filePath });
|
|
|
+ resolve(true);
|
|
|
}
|
|
|
});
|
|
|
- })
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/* For Internal Usage only. Temporary serve as a way for server to keep track of clients. To be replaced in the future with better alternatives. */
|
|
|
-export function addClientToDB(entry: ConnectedSocketClient, filePath: string = 'clients.json'): void {
|
|
|
+export function addClientToDB(entry: ConnectedSocketClient): void {
|
|
|
try {
|
|
|
let data: ConnectedSocketClient[] = [];
|
|
|
-
|
|
|
+ let filePath = process.env.FolderPath as string + 'clients.json'
|
|
|
// Check if the file exists and load existing data
|
|
|
if (fs.existsSync(filePath)) {
|
|
|
const fileContent = fs.readFileSync(filePath, 'utf-8');
|
|
@@ -303,10 +315,11 @@ export function addClientToDB(entry: ConnectedSocketClient, filePath: string = '
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export async function checkIfClientExists(id: string, filePath: string = 'clients.json'): Promise<ConnectedSocketClient> {
|
|
|
+export async function checkIfClientExists(id: string): Promise<ConnectedSocketClient> {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
try {
|
|
|
// Check if the file exists
|
|
|
+ let filePath = process.env.FolderPath as string + 'clients.json'
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
console.log({ message: "File does not exist." })
|
|
|
reject('File does not exist');
|
|
@@ -339,7 +352,8 @@ export async function checkOwnClientInfo(filename?: string): Promise<{ id: strin
|
|
|
if (fs.existsSync(`${filename}.json`)) {
|
|
|
try {
|
|
|
// Read the file contents
|
|
|
- const fileData = fs.readFileSync(`${filename}.json`, 'utf8');
|
|
|
+ let filePath = process.env.FolderPath as string
|
|
|
+ const fileData = fs.readFileSync(filePath + `${filename}.json`, 'utf8');
|
|
|
|
|
|
// If the file is empty, return an error
|
|
|
if (fileData.trim() === "") {
|