Skip to content

Commit dff986e

Browse files
committed
Improve saving dup images when fetch overruns.
1 parent 49187c5 commit dff986e

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/index.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ function writeImage(filename, data) {
4242
});
4343
}
4444

45+
function writeDup(filename, data) {
46+
let dupstr = ("0000" + dup).slice(-5);
47+
console.log(`Write ${filename}.${dupstr}`);
48+
writeImage(`${filename}.${dupstr}`, data);
49+
dup++;
50+
}
51+
4552
async function fetcher() {
4653
let gotOptions = {
4754
retries: 0,
@@ -59,17 +66,22 @@ async function fetcher() {
5966
}
6067

6168
try {
62-
start = Date.now();
63-
const response = await got(url, gotOptions);
64-
// FIXME should probably ping pong rather than copy
65-
lastImage = response.body.slice(0);
66-
filename = start;
67-
dup = 0;
68-
writeImage(filename, response.body);
69+
if (start) {
70+
writeDup(start, lastImage);
71+
} else {
72+
start = Date.now();
73+
const response = await got(url, gotOptions);
74+
// FIXME should probably ping pong rather than copy
75+
lastImage = response.body.slice(0);
76+
filename = start;
77+
start = 0;
78+
dup = 0;
79+
writeImage(filename, response.body);
80+
}
6981
} catch {
82+
let dupname = start;
83+
writeDup(start, lastImage);
7084
start = 0;
71-
writeImage(filename + "." + dup, lastImage);
72-
dup++;
7385
}
7486

7587
if (count++ > 1000000) {

0 commit comments

Comments
 (0)