Files
airjet/api/modules/fileManager.js
2023-05-10 20:43:45 +02:00

80 lines
1.1 KiB
JavaScript

const fs = require('fs');
const download = require('download');
const random = require('./random');
function fileExist(path) {
try {
fs.readFileSync(path);
return true;
}
catch (err) {
return false;
}
}
function fileDelete(path) {
try {
fs.unlinkSync(path);
return true;
}
catch (err) {
return false;
}
}
function fileDownload(url, name) {
try {
download(url, '../cdn/images/', { filename: name });
return true;
}
catch (err) {
return false;
}
}
function folderExist(path) {
try {
if (fs.existsSync(path)) {
return true;
}
else {
return false;
}
}
catch (err) {
return false;
}
}
function getFilesFromFolder(path) {
try {
return fs.readdirSync(path);
}
catch (err) {
return false;
}
}
function randomFileFromFolder(path) {
try {
if (getFilesFromFolder(path)) {
return random.random(0, getFilesFromFolder(path).length);
}
else {
return false;
}
}
catch (err) {
return false;
}
}
module.exports = {
fileExist,
fileDelete,
fileDownload,
folderExist,
getFilesFromFolder,
randomFileFromFolder,
};