D
D
Damir Balgabaev2020-07-11 14:53:52
JavaScript
Damir Balgabaev, 2020-07-11 14:53:52

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();

Then everything is going to the archive.

The order of work was as follows: accept the files => put in the folder => if it contains ''rar'' , unpack => if there are folders, enter each and move the files from there to the shared folder ''tmp'' => check if whether there are more folders and repeat the previous function until all the files are extracted => collect all files in an archive.

But the problem is that, the action to check if there are still folders and repeat the previous function until all the files are extracted , it starts to do before the archive is unpacked. Did everything synchronously then asynchronously, still does not work.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question