1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 13:29:22 +00:00

Can generate random new file name

This commit is contained in:
Pierre HUBERT 2019-11-30 18:34:21 +01:00
parent 6736dec599
commit 72a4ba79f2

View File

@ -1,6 +1,7 @@
import { conf } from "../helpers/ConfigHelper"; import { conf } from "../helpers/ConfigHelper";
import { join } from "path"; import { join } from "path";
import { existsSync, mkdirSync, writeFileSync } from "fs"; import { existsSync, mkdirSync, writeFileSync } from "fs";
import { randomStr } from "./CryptUtils";
/** /**
* User data utilities * User data utilities
@ -52,4 +53,26 @@ export function prepareFileCreation(userID: number, folderName: string) : string
} }
return subfolder; return subfolder;
}
/**
* Generate a new file name in order to create a new file
*
* @param dir Target directory
* @param ext File extension
* @returns Generated file name (including dir)
*/
export function generateNewUserDataFileName(dir: string, ext: string) : string {
const sys_dir = pathUserData(dir, true);
if(!existsSync(sys_dir))
throw Error("Trying to create file into " + sys_dir + " but this directory does not exists!");
// Generate random file name
let filename : string;
do {
filename = randomStr(25) + "." + ext;
} while(existsSync(join(sys_dir, filename)));
return join(dir, filename);
} }