Answer the question
In order to leave comments, you need to log in
How to handle request in Rest API?
The task of the application is that archives with files come from the client. The structure of the archives is different. And the output should be one archive with all the files.
Here is an example code:
const fileList = req.files.file;
function moveToTemp() {
if (fileList.length > 1) {
fileList.forEach(file => {
file.mv(path.join(__dirname, '..', 'dist', 'tmp', file.name), err => {
if (err) {
console.log(err);
return res.status(500).send(err);
}
});
});
}
if(fileList.name){
console.log('single file')
}
}
await moveToTemp();
function unPack (){
fs.readdir(path.join(__dirname, '..', 'dist', 'tmp'), (err, files) => {
if(err){
console.log(err);
}
files.forEach((value => {
if(value.slice(-3) === 'rar'){
unrar(path.join(__dirname, '..', 'dist', 'tmp', value), path.join(__dirname, '..', 'dist', 'tmp'), {override: true});
}
}));
})
};
await unPack();
function unlink() {
fs.readdir(path.join(__dirname, '..', 'dist', 'tmp'), (err, files) => {
if(err){
console.log(err);
}
files.forEach((value => {
if(value.slice(-3) === 'rar'){
fs.unlink(path.join(__dirname, '..', 'dist', 'tmp', value), (err) => {
if (err) console.log(err);
});
}
}));
})
}
await unlink();
function contain(){
fs.readdir(path.join(__dirname, '..', 'dist', 'tmp'), (err, files) => {
if(err){
console.log(err);
}
files.forEach((value => {
if(fs.lstatSync(path.join(__dirname, '..', 'dist', 'tmp', value)).isDirectory()){
fs.readdir(path.join(__dirname, '..', 'dist', 'tmp', value), (err, files) => {
if(err){
console.log(err);
}
files.forEach((value => {
value.mv(path.join(__dirname, '..', 'dist', 'tmp', value), err => {
if (err) {
console.log(err);
}
});
}));
});
fs.unlink(path.join(__dirname, '..', 'dist', 'tmp', value), (err) => {
if (err) console.log(err);
});
}
}));
})
}
function lookForFolders () {
const list = fs.readdirSync(path.join(__dirname, '..', 'dist', 'temp'));
console.log(list);
fs.readdirSync(path.join(__dirname, '..', 'dist', 'temp'), (err, files) => {
if(err){
console.log(err);
}
if(files){
files.forEach(file => {
do {
contain();
} while (
fs.lstatSync(path.join(__dirname, '..', 'dist', 'tmp', file)).isDirectory()
)
});
}
})
};
await lookForFolders();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question