-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
135 lines (99 loc) · 3.54 KB
/
Copy pathindex.js
File metadata and controls
135 lines (99 loc) · 3.54 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const config = require('./config.json');
const path = require('path');
const chalk = require('chalk');
const icons = require('log-symbols');
const axios = require('axios');
const createTemp = require('create-temp-directory');
const fs = require('fs');
const ftp = require('basic-ftp');
const cacheFileLocation = path.join(__dirname, './cache.txt');
const start = async () => {
const tmpPath = await createTemp.createTempDirectory();
const iplParts = path.parse(config.iplLocation);
const iplLocationLocal = path.join(tmpPath.path, iplParts.base);
try {
const cache = loadCache();
// Download ipl file
const client = new ftp.Client();
client.ftp.verbose = config.verbose;
await client.access(config.ftp);
await client.cd(iplParts.dir);
await client.downloadTo(iplLocationLocal, iplParts.base);
// Get array of roms
const roms = JSON.parse(fs.readFileSync(iplLocationLocal, { encoding:'utf8' })).items.map(item => { return { path: item.path, label: item.label }; });//.slice(0, 15);
// Remote download roms
for await (const rom of roms) {
console.log('\n' + chalk.cyanBright(rom.label + ` (${path.basename(rom.path)})`));
if (!cache.includes(rom.path)){
const romParts = path.parse(rom.path);
const remoteUrl = config.newRomsRootUrl + romParts.base;
const localFile = path.join(tmpPath.path, romParts.base);
process.stdout.write(chalk.grey('Downloading... '));
await download(remoteUrl, localFile);
process.stdout.write(icons.success + '\n');
await client.cd(romParts.dir);
process.stdout.write(chalk.grey('Copying to ftp server... '));
await client.uploadFrom(localFile, romParts.base);
process.stdout.write(icons.success + '\n');
saveCache(rom.path);
} else {
console.log(chalk.grey('...is in cache'));
}
};
client.close();
console.log('\n');
}
catch(err) {
console.error(err);
}
finally {
fs.rmSync(iplLocationLocal);
tmpPath.remove();
}
}
const loadCache = () => {
if (fs.existsSync(cacheFileLocation)) {
const nowDate = new Date().getTime();
const stats = fs.statSync(cacheFileLocation);
const dif = (nowDate - stats.mtimeMs) / 1000;
if (dif > 3600){ // 1 hour
fs.rmSync(cacheFileLocation);
return [];
}
return fs.readFileSync(cacheFileLocation, { encoding:'utf8' }).split('\n');
} else {
return [];
}
}
const saveCache = (romPath) => {
fs.appendFileSync(cacheFileLocation, romPath + '\n');
}
const download = async (url, path) => {
if (fs.existsSync(path)) {
fs.unlinkSync(path);
}
await axios({
method: 'get',
url: url,
responseType: 'stream',
headers: {},
}).then(function (response) {
const writer = fs.createWriteStream(path);
return new Promise((resolve, reject) => {
response.data.pipe(writer);
let error;
writer.on('error', (err) => {
error = err;
writer.close();
reject(err);
});
writer.on('close', () => {
if (!error) {
resolve(true);
}
});
});
});
return true;
};
start();