import { conf } from "../helpers/ConfigHelper"; import { join } from "path"; import { existsSync, mkdirSync, writeFileSync } from "fs"; /** * User data utilities * * @author Pierre HUBERT */ /** * Get and returns the URL path to an userdata file * * @param uri Optionnal, defines the URI pointing on the file * @param sysPath Optionnal, defines if system path is required instead of URL * @return The full URL to the userdata file */ export function pathUserData(uri: string = "", sysPath: boolean = false) : string { if(!sysPath) return join(conf().storageURL, uri).replace(":/", "://"); else return join(conf().storagePath, uri); } /** * Prepare file creation * * @param userID Target user ID * @param folderName The file directory (based on user_data folder) */ export function prepareFileCreation(userID: number, folderName: string) : string { // Determine subfolder name let subfolder : string; if(userID != 0) subfolder = join(folderName, userID.toString()); else subfolder = folderName; const subfolderPath = pathUserData(subfolder, true); // Check if the path exists if(!existsSync(subfolderPath)) { // Create the directory mkdirSync(subfolderPath, { recursive: true }); // Block directory listing writeFileSync(join(subfolderPath, "index.html"), ""); } return subfolder; }