khamba
Version:
A cli tool for sharing files through local network.
128 lines (127 loc) • 4.21 kB
JavaScript
import { fileTypeMapping, peoplesNames } from '../functions/data.js';
import { log } from '../functions/log.js';
import { SEND_PATH } from '../functions/variables.js';
import fs, { statfs } from 'fs';
import path from 'path';
export function getRandomBanglaName() {
const randomIndex = Math.floor(Math.random() * peoplesNames.length);
return peoplesNames[randomIndex];
}
export const hasNullValue = (obj) => {
return Object.values(obj).some(value => value === null);
};
export const cleanFileName = (name) => {
return name.replace(/\/$/, ''); // ! Removes "/" at the end of the name
// return name;
// return name.replace(/[\\/]/g, '').replace(/^\./, '');
};
export const isDirectory = (_path) => {
const fullPath = `${SEND_PATH}/${_path}`;
if (!fs.existsSync(fullPath))
return false;
return fs.statSync(fullPath)?.isDirectory();
};
export const getAllFiles = (dirPath, arrayOfFiles = []) => {
const files = fs.readdirSync(dirPath);
files.forEach(file => {
const fullPath = path.join(dirPath, file);
if (fs.statSync(fullPath).isDirectory()) {
arrayOfFiles = getAllFiles(fullPath, arrayOfFiles);
}
else {
const cwd = path.resolve(SEND_PATH);
const relativePath = path.relative(cwd, fullPath);
arrayOfFiles.push(relativePath);
}
});
return arrayOfFiles;
};
export const getFileType = (_path) => {
const stats = fs.statSync(_path);
if (stats.isDirectory())
return 'folder';
const extension = path.extname(_path);
if (!extension)
return 'folder';
return fileTypeMapping[extension] || 'others';
};
export const getFileSize = (_path) => {
if (!fs.existsSync(_path))
return 0;
const stats = fs.statSync(_path);
return stats.size;
};
export const getFolderSize = (_path) => {
let totalSize = 0;
const calculateSize = (filePath) => {
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
const files = fs.readdirSync(filePath);
files.forEach(file => calculateSize(path.join(filePath, file)));
}
else {
totalSize += stats.size;
}
};
calculateSize(_path);
return totalSize;
};
export async function checkFileExists(filePath) {
try {
await fs.promises.access(filePath);
return true;
}
catch {
return false;
}
}
export async function getMissingFiles(files) {
const results = await Promise.all(files.map(async (file) => {
const filePath = path.join(SEND_PATH, file);
const exists = await checkFileExists(filePath);
return exists ? null : file;
}));
return results.filter(file => file !== null);
}
export const getDiskSpace = async () => {
const drive = path.parse(process.cwd()).root;
return new Promise((resolve, reject) => {
statfs(drive, (err, stats) => {
log('Total free space', stats.bsize * stats.bfree);
log('Available for user', stats.bsize * stats.bavail);
if (err) {
reject(err);
}
else {
resolve(stats.bsize * stats.bavail);
}
});
});
};
export const formatBytes = (bytes) => {
if (bytes === 0)
return '0 Byte';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
};
export const findLongestString = (strings) => {
return strings.reduce((longest, current) => (current.length > longest.length ? current : longest), '');
};
export const adjustStringLength = (str, length) => {
if (str.length < length) {
return str.padEnd(length, ' ');
}
else if (str.length > length) {
const charBeforeDots = Math.floor(length / 2) - 1;
const charAfterDots = length - (charBeforeDots + 3);
return str.slice(0, charBeforeDots) + '...' + str.slice(-charAfterDots);
}
else {
return str;
}
};
export const isObjectEmpty = (obj) => {
return Object.keys(obj).length === 0;
};