leeroybrun / node-mt-files-downloader

Simple module letting you download a file from the web, compute stats, retry in case of error, etc
MIT License
84 stars 44 forks source link

After download file is complete the file is "LOCKED" #17

Open pantchox opened 7 years ago

pantchox commented 7 years ago

When the download finishes i am unable to do files action such as move/del the downloaded file(s). I assume somewhere when the download is finished the file handler is not released. only if i close the program that runs the downloader then i can do the specified actions.

please elaborate.

P.S thanks for this great lib!

pantchox commented 7 years ago

Another issue i have noticed. I am using my client to sync files from server, as i download the file which is a zip archive i run a zip test and md5 check (upon server notifying the md5) and if it does not match it is a failure and the file need to be DELETED and redownloaded. the DELETE command is failing upon reason from my first comment, there for when it finish the redownload it can not rename it to the original filename because the file that failed which has the same name could not be deleted which makes the events hang and get stack.

this is a real issue i will need to investigate this repo source and mt-downloader spec to see this repo complies with the events of releasing the file handler.

pantchox commented 7 years ago

Hi, I think the issue is happening only on windows and in a very WEIRD way.

var fs = require('fs');
var os   = require('os');
var path = require('path');
var Downloader = require('../lib/Downloader');

// Create new downloader
var downloader = new Downloader();
var fileUrl = 'http://vhost2.hansenet.de/10_mb_file.bin';
var fileSavePath = path.join(__dirname, 'mtFileDlTest1.zip');
console.log('File will be downloaded from '+ fileUrl +' to '+ fileSavePath);

// Start download
var dl = downloader.download(fileUrl, fileSavePath)
          .start();

dl.on('end', function() {
        fs.unlink(fileSavePath, function(err) {
        if ( err ) {
            console.log('ERROR: ' + err);
        } else {
            console.log('starting a new download on same name!');
            var downloader2 = new Downloader();
            var fileUrl2 = 'http://vhost2.hansenet.de/10_mb_file.bin';
            var fileSavePath2 = path.join(__dirname, 'mtFileDlTest1.zip');
            console.log('File will be downloaded from '+ fileUrl2 +' to '+ fileSavePath2);
            // Start download
            var dl2 = downloader2.download(fileUrl2, fileSavePath2)
            .start();
                require('./_handleEvents')(dl2);
                require('./_printStats')(dl2);

            }});

    });
// Import generic examples for handling events and printing stats
require('./_handleEvents')(dl);
require('./_printStats')(dl);

here i pasted a modified code from the example folder, it downloads a file then when it is finished it will delete it - if you follow my comments i was having issue to delete it after download assuming the file handler is still on and locking the file.

In the fs.unlink() command after i run it if there is an error it will show the error and exit, if not it will continue to redownload the file again. On linux everything works fine it deletes the file no error and redownload again, However in windows it also works fine, the unlink does not return any error but while it is downloading the second time i can still see the first downloaded file present, then when the 2nd file is finished downloading it can not change the MTD file name to original file name because the file STILL exists and prob' locked, after the program finishes (with loads of retrys) only then the file is deleted.

I don't know if this is an issue of mtd-files-downloader or mt-downloader or windows just sucks.

I have windows 7 64bit and node v6.60

So i assume this is a windows issue, which is the machine i do my development but thankfully it will run on linux.

hgouveia commented 6 years ago

I'm having same issue, after download or stop , the file remains locked and i can't find any way to unlock the file