-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupload-images.js
54 lines (43 loc) · 1.92 KB
/
upload-images.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const fs = require('fs');
const path = require('path');
const got = require('got');
const FormData = require('form-data');
const ENDPOINT = 'https://tarkov-data-manager.herokuapp.com/suggest-image';
const maxSimultaneousUploads = 1;
module.exports = async (options) => {
const uploadFiles = fs.readdirSync(path.join('./', 'generated-images-missing'));
let currentUploads = [];
if (uploadFiles.length == 0) return 0;
for(const filename of uploadFiles){
const form = new FormData();
const matches = filename.match(/(?<id>.{24})-(?<type>.+?)\.(?:jpg|png)/);
if(!matches){
console.log(`Found junkfile ${filename}, skipping`);
continue;
}
if (!options.response.uploaded[matches.groups.id]) options.response.uploaded[matches.groups.id] = [];
if (!options.response.uploadErrors[matches.groups.id]) options.response.uploadErrors[matches.groups.id] = [];
form.append('id', matches.groups.id);
form.append('type', matches.groups.type);
form.append(matches.groups.type, fs.createReadStream(path.join('./', 'generated-images-missing', filename)));
console.log(`Uploading new ${matches.groups.type} for ${matches.groups.id}`);
const upload = got.post(ENDPOINT, {
body: form,
}).then(() => {
options.response.uploaded[matches.groups.id].push(matches.groups.type.replace('-', ' '));
}).catch(error => {
options.response.uploadErrors[matches.groups.id].push(error);
if(!error.response){
console.log(error);
} else {
console.log(error.response.statusCode);
console.log(error.response.body);
}
});
currentUploads.push(upload);
if (currentUploads.length >= maxSimultaneousUploads) {
await Promise.allSettled(currentUploads);
currentUploads = [];
}
}
};