forked from luuxis/Selvania-Launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixed issues with file deletion - Fixed Issues with files downloading
- Loading branch information
Showing
3 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
"use strict"; | ||
/** | ||
* @author Luuxis | ||
* @license CC-BY-NC 4.0 - https://creativecommons.org/licenses/by-nc/4.0/ | ||
*/ | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs_1 = __importDefault(require("fs")); | ||
const node_fetch_1 = __importDefault(require("node-fetch")); | ||
const events_1 = require("events"); | ||
|
||
class download { | ||
constructor() { | ||
this.on = events_1.EventEmitter.prototype.on; | ||
this.emit = events_1.EventEmitter.prototype.emit; | ||
} | ||
|
||
async downloadFile(url, path, fileName, retries = 300) { | ||
const attemptDownload = async (attempt) => { | ||
if (!fs_1.default.existsSync(path)) | ||
fs_1.default.mkdirSync(path, { recursive: true }); | ||
|
||
const writer = fs_1.default.createWriteStream(path + '/' + fileName); | ||
try { | ||
const response = await (0, node_fetch_1.default)(url); | ||
let size = response.headers.get('content-length'); | ||
let downloaded = 0; | ||
|
||
return new Promise((resolve, reject) => { | ||
response.body.on('data', (chunk) => { | ||
downloaded += chunk.length; | ||
this.emit('progress', downloaded, size); | ||
writer.write(chunk); | ||
}); | ||
|
||
response.body.on('end', () => { | ||
writer.end(); | ||
resolve(); | ||
}); | ||
|
||
response.body.on('error', async (err) => { | ||
this.emit('error', err); | ||
writer.end(); | ||
if (attempt < retries) { | ||
console.log(`Retrying download (${attempt + 1}/${retries})...`); | ||
setTimeout(() => attemptDownload(attempt + 1).then(resolve).catch(reject), 2000); | ||
} else { | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
} catch (error) { | ||
this.emit('error', error); | ||
if (attempt < retries) { | ||
console.log(`Retrying download (${attempt + 1}/${retries})...`); | ||
setTimeout(() => attemptDownload(attempt + 1), 2000); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
}; | ||
|
||
return attemptDownload(0); | ||
} | ||
|
||
async downloadFileMultiple(files, size, limit = 1, timeout = 10000, retries = 300) { | ||
if (limit > files.length) | ||
limit = files.length; | ||
|
||
let completed = 0; | ||
let downloaded = 0; | ||
let queued = 0; | ||
let start = new Date().getTime(); | ||
let before = 0; | ||
let speeds = []; | ||
let estimated = setInterval(() => { | ||
let duration = (new Date().getTime() - start) / 1000; | ||
let loaded = (downloaded - before) * 8; | ||
if (speeds.length >= 5) | ||
speeds = speeds.slice(1); | ||
speeds.push((loaded / duration) / 8); | ||
let speed = 0; | ||
for (let s of speeds) | ||
speed += s; | ||
speed /= speeds.length; | ||
this.emit("speed", speed); | ||
let time = (size - downloaded) / (speed); | ||
this.emit("estimated", time); | ||
start = new Date().getTime(); | ||
before = downloaded; | ||
}, 500); | ||
|
||
const attemptDownloadFile = async (file, attempt) => { | ||
if (!fs_1.default.existsSync(file.folder)) | ||
fs_1.default.mkdirSync(file.folder, { recursive: true, mode: 0o777 }); | ||
|
||
const writer = fs_1.default.createWriteStream(file.path, { flags: 'w', mode: 0o777 }); | ||
try { | ||
const response = await (0, node_fetch_1.default)(file.url, { timeout: timeout }); | ||
response.body.on('data', (chunk) => { | ||
downloaded += chunk.length; | ||
this.emit('progress', downloaded, size, file.type); | ||
writer.write(chunk); | ||
}); | ||
response.body.on('end', () => { | ||
writer.end(); | ||
completed++; | ||
downloadNext(); | ||
}); | ||
response.body.on('error', async (err) => { | ||
this.emit('error', err); | ||
writer.end(); | ||
if (attempt < retries) { | ||
console.log(`Retrying download (${attempt + 1}/${retries}) for file: ${file.url}`); | ||
setTimeout(() => attemptDownloadFile(file, attempt + 1), 2000); | ||
} else { | ||
completed++; | ||
downloadNext(); | ||
} | ||
}); | ||
} catch (e) { | ||
writer.end(); | ||
if (attempt < retries) { | ||
console.log(`Retrying download (${attempt + 1}/${retries}) for file: ${file.url}`); | ||
setTimeout(() => attemptDownloadFile(file, attempt + 1), 2000); | ||
} else { | ||
completed++; | ||
downloadNext(); | ||
this.emit('error', e); | ||
} | ||
} | ||
}; | ||
|
||
const downloadNext = async () => { | ||
if (queued < files.length) { | ||
let file = files[queued]; | ||
queued++; | ||
await attemptDownloadFile(file, 0); | ||
} | ||
}; | ||
|
||
while (queued < limit) | ||
downloadNext(); | ||
|
||
return new Promise((resolve) => { | ||
const interval = setInterval(() => { | ||
if (completed === files.length) { | ||
clearInterval(estimated); | ||
clearInterval(interval); | ||
resolve(); | ||
} | ||
}, 100); | ||
}); | ||
} | ||
|
||
async checkURL(url, timeout = 10000) { | ||
return await new Promise(async (resolve, reject) => { | ||
await (0, node_fetch_1.default)(url, { method: 'HEAD', timeout: timeout }).then(res => { | ||
if (res.status === 200) { | ||
resolve({ | ||
size: parseInt(res.headers.get('content-length')), | ||
status: res.status | ||
}); | ||
} | ||
}); | ||
reject(false); | ||
}); | ||
} | ||
|
||
async checkMirror(baseURL, mirrors) { | ||
for (let mirror of mirrors) { | ||
let url = `${mirror}/${baseURL}`; | ||
let res = await this.checkURL(url).then(res => res).catch(err => false); | ||
if (res?.status == 200) { | ||
return { | ||
url: url, | ||
size: res.size, | ||
status: res.status | ||
}; | ||
break; | ||
} | ||
continue; | ||
} | ||
return false; | ||
} | ||
} | ||
exports.default = download; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters