The problem of continuous transmission at breakpoint

my requirements are simply described as:

 chunkSizemongodb
 node.jsn(n)
 writeStream

what to do now is

 
 

problems encountered:


<0><chunkSize-1>
Node.jsfdC
seek

(downloadpromisePromise):
 function download(filedata,username,chunkSize) {
 var chunks_n = Math.ceil(filedata.length/filedata.chunkSize);
 var file_id = filedata._id;
 var DLpromiseall = [];

 //createWrite
 fs.open(filedata.filename, "a", (err, fs) => {
     if (err) {
         console.log(err);
     }
 })
 
 for(let curindex = 0;curindex < chunks_n;curindexPP) {
     DLpromiseall.push(downloadpromise(username,file_id,curindex,filedata.filename,chunkSize));
 }
 var mytimer = setInterval(() => {
     if(DLpromiseall.length == chunks_n) {
         clearInterval(mytimer);
         Promise.all(DLpromiseall).then(vales=>{
             console.log(vales);
             console.log("all download")
         }).catch(err =>{
             console.log(err);
             console.log("");
         })
     }
 },500)
}
The

downloadPromise function is as follows:

 * 
 **/
function  downloadpromise(username,file_id,n,filename,chunkSize) {
    return new Promise(function (resolve,reject) {
        let mydata = {
            username: username,
            file_id: file_id,
            n: n
        };
        let contents = queryString.stringify(mydata);
        let options = {
            host: "localhost",
            path: "/nodedownload/",
            port: 8000,
            method: "POST",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Content-Length": contents.length
            }
        };
        let req = http.request(options, function (res) {
            let bufs = [];
            res.on("data", function (chunk) {
                bufs.push(chunk)
            });
            res.on("end", function (d) {
                console.log("end")
                var resubuf = Buffer.concat(bufs)
                var resu = new Buffer(JSON.parse(resubuf));
                try {
                    let WSoptions = {
                        start: n*chunkSize,
                        flags: "r+"
                    }
                    let WStream = fs.createWriteStream(filename,WSoptions)
                    WStream.write(resu,function () {
                        console.log("This is : "+n);
                    });
                    WStream.end();
                    resolve(filename);
                }catch (err) {
                    console.log(err);
                    reject(err);
                }
            });
            res.on("error", function (e) {
                throw e;
            })
        });
        req.write(contents);
        req.end();
    })
}
Mar.07,2021

temporarily came up with an immature idea, by judging whether there is something written in each block, and whether the written content meets the required writing standard. The code is as follows

function readPromise(filepath,i,options) {
    return new Promise(function (resolve,reject) {
        let flag = false;
        let readStream = fs.createReadStream(filepath,options);
        readStream.on("data",(chunk)=>{
            let chunString = chunk.toString().trim();
            let nsize = options.end - options.start + 1;
            if(!chunString||chunString.length < nsize) {
                flag = true;
            }
        })
        readStream.on("close",(ele) => {
            resolve(flag);
        })
        readStream.on("error",(e) => {
            reject(e);
        })
    })
}

the above functions have been completed, and what we need to do now is the problem of resuming the breakpoint in the download process, that is, how to download
if this part of the data is already in the local file, then skip this part of the download.

in the original http protocol, the client requests the part of content to be returned by the server through Range (it is determined that several bytes have been downloaded locally), and the server informs the client of the part content currently returned through Content-Range . And the extra one you have here is just divided into pieces, and it's all the same.

specify the location where the file stream starts through option.start .

fs.createReadStream(filePath, {
     start: startRange,
     end : endRange //
});
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e464d3-44b91.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e464d3-44b91.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?