尝试使用 node.js 循环下载文件,但似乎出现了一些同步问题
Trying to download a file in a loop with node.js, but seems to get some sync problems
我正在尝试在 node.js 中制作一个简单的程序,它将以一定的间隔下载相同的文件。如果下载的文件比以前的文件新,那么它将在计数器的帮助下以新的文件名保存。
如果是新文件,那么我想把它保存在last_unique.jpg这个名字里,下次下载文件时用它来比较。但这似乎不起作用。为了测试,我只有一个空的 last_unique.jpg,我希望它被覆盖。但它从来不是,所以每次下载jpg文件时,它都是唯一的,并保存在file3.jpg、file3.jpg等
但是,输出看起来也可能是一些异步问题?它会跳过前几次。
输出:
downloading 1
downloading 2
downloading 3
Unique file spotted!
downloading 4
Unique file spotted!
downloading 5
Unique file spotted!
downloading 6
Unique file spotted!
downloading 7
Unique file spotted!
downloading 8
Unique file spotted!
代码如下:
const http = require('http');
const fs = require('fs');
const md5File = require('md5-file');
const fileToDownload = "http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg";
var counter = 0;
function request() {
counter = counter + 1
console.log("downloading " + counter);
const save = fs.createWriteStream("last_download.jpg");
http.get(fileToDownload, function(response) {
response.pipe(save)
});
const hash1 = md5File.sync('last_download.jpg');
const hash2 = md5File.sync('last_unique.jpg');
// it is a new file
if (hash1.localeCompare(hash2) != 0) {
console.log('Unique file spotted!');
fs.copyFileSync('last_download.jpg','last_unique.jpg');
fs.copyFileSync('last_unique.jpg','file' + counter + '.jpg');
}
}
setInterval(request, 3000);
const http = require('http');
const fs = require('fs');
const md5File = require('md5-file');
const fileToDownload = "http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg";
var counter = 0;
function request() {
counter = counter + 1;
console.log("downloading " + counter);
const save = fs.createWriteStream("last_download.jpg");
http.get(fileToDownload, function(response) {
response.pipe(save);
response.on('end',function () {
save.end();
})
});
save.on('finish',function () {
const hash1 = md5File.sync('last_download.jpg');
const hash2 = md5File.sync('last_unique.jpg');
console.log(hash1,hash2);
// it is a new file
if (hash1.localeCompare(hash2) != 0) {
console.log('Unique file spotted!');
fs.copyFileSync('last_download.jpg','last_unique.jpg');
fs.copyFileSync('last_unique.jpg','file' + counter + '.jpg');
}
});
}
setInterval(request, 3000);
您需要监听流上的完成事件,否则您可能会在流完全写入之前调用复制函数。因此,部分图像从 last_download.jpg 复制到 last_unique.jpg,这意味着哈希将不同。这是由于复制和 http 请求的异步性质。
我正在尝试在 node.js 中制作一个简单的程序,它将以一定的间隔下载相同的文件。如果下载的文件比以前的文件新,那么它将在计数器的帮助下以新的文件名保存。
如果是新文件,那么我想把它保存在last_unique.jpg这个名字里,下次下载文件时用它来比较。但这似乎不起作用。为了测试,我只有一个空的 last_unique.jpg,我希望它被覆盖。但它从来不是,所以每次下载jpg文件时,它都是唯一的,并保存在file3.jpg、file3.jpg等
但是,输出看起来也可能是一些异步问题?它会跳过前几次。
输出:
downloading 1
downloading 2
downloading 3
Unique file spotted!
downloading 4
Unique file spotted!
downloading 5
Unique file spotted!
downloading 6
Unique file spotted!
downloading 7
Unique file spotted!
downloading 8
Unique file spotted!
代码如下:
const http = require('http');
const fs = require('fs');
const md5File = require('md5-file');
const fileToDownload = "http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg";
var counter = 0;
function request() {
counter = counter + 1
console.log("downloading " + counter);
const save = fs.createWriteStream("last_download.jpg");
http.get(fileToDownload, function(response) {
response.pipe(save)
});
const hash1 = md5File.sync('last_download.jpg');
const hash2 = md5File.sync('last_unique.jpg');
// it is a new file
if (hash1.localeCompare(hash2) != 0) {
console.log('Unique file spotted!');
fs.copyFileSync('last_download.jpg','last_unique.jpg');
fs.copyFileSync('last_unique.jpg','file' + counter + '.jpg');
}
}
setInterval(request, 3000);
const http = require('http');
const fs = require('fs');
const md5File = require('md5-file');
const fileToDownload = "http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg";
var counter = 0;
function request() {
counter = counter + 1;
console.log("downloading " + counter);
const save = fs.createWriteStream("last_download.jpg");
http.get(fileToDownload, function(response) {
response.pipe(save);
response.on('end',function () {
save.end();
})
});
save.on('finish',function () {
const hash1 = md5File.sync('last_download.jpg');
const hash2 = md5File.sync('last_unique.jpg');
console.log(hash1,hash2);
// it is a new file
if (hash1.localeCompare(hash2) != 0) {
console.log('Unique file spotted!');
fs.copyFileSync('last_download.jpg','last_unique.jpg');
fs.copyFileSync('last_unique.jpg','file' + counter + '.jpg');
}
});
}
setInterval(request, 3000);
您需要监听流上的完成事件,否则您可能会在流完全写入之前调用复制函数。因此,部分图像从 last_download.jpg 复制到 last_unique.jpg,这意味着哈希将不同。这是由于复制和 http 请求的异步性质。