mirror of
https://github.com/an-anime-team/an-anime-game-launcher.git
synced 2024-11-29 18:38:48 +03:00
Added downloader class
This commit is contained in:
parent
ed2a96840c
commit
2d1eaa6eb4
3 changed files with 108 additions and 4 deletions
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "neutralino-template",
|
||||
"version": "0.0.0",
|
||||
"name": "an-anime-game-launcher",
|
||||
"version": "3.0.0",
|
||||
"scripts": {
|
||||
"dev": "vite build && neu run",
|
||||
"build": "vite build && neu build --release"
|
||||
|
|
|
@ -1,9 +1,18 @@
|
|||
import * as Vue from 'vue/dist/vue.esm-bundler';
|
||||
|
||||
import Window from '../ts/neutralino/Window';
|
||||
import Runners from '../ts/Runners';
|
||||
|
||||
Runners.get().then(console.log);
|
||||
import Downloader from '../ts/Downloader';
|
||||
|
||||
Downloader.download('https://vitejs.dev/', 'test.html').then((stream) => {
|
||||
stream.progress((current: number, total: number) => {
|
||||
console.log(`${Math.round(current / total * 100)}%`);
|
||||
});
|
||||
|
||||
stream.finish(() => {
|
||||
console.log('finished');
|
||||
});
|
||||
});
|
||||
|
||||
Vue.createApp({
|
||||
data: () => {
|
||||
|
|
95
src/ts/Downloader.ts
Normal file
95
src/ts/Downloader.ts
Normal file
|
@ -0,0 +1,95 @@
|
|||
class Stream
|
||||
{
|
||||
protected uri: string;
|
||||
protected total: number|null;
|
||||
|
||||
protected onProgress?: (current: number, total: number) => void;
|
||||
protected onFinish?: () => void;
|
||||
|
||||
protected finished: boolean = false;
|
||||
|
||||
public constructor(uri: string, output: string, total: number|null = null)
|
||||
{
|
||||
this.uri = uri;
|
||||
this.total = total;
|
||||
|
||||
// @ts-expect-error
|
||||
Neutralino.os.execCommand(`wget -O "${output}" -nv "${uri}"`).then(() => {
|
||||
this.finished = true;
|
||||
|
||||
this.onFinish();
|
||||
});
|
||||
|
||||
if (total !== null)
|
||||
{
|
||||
const updateProgress = () => {
|
||||
// @ts-expect-error
|
||||
Neutralino.filesystem.getStats(output).then((stats) => {
|
||||
if (this.onProgress)
|
||||
this.onProgress(stats.size, this.total);
|
||||
|
||||
if (!this.finished)
|
||||
setTimeout(updateProgress, 100);
|
||||
});
|
||||
};
|
||||
|
||||
updateProgress();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify event that will be called every 100 ms during the file downloading
|
||||
*
|
||||
* @param callable
|
||||
*/
|
||||
public progress(callable: (current: number, total: number) => void)
|
||||
{
|
||||
this.onProgress = callable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify event that will be called after the file will be downloaded
|
||||
*
|
||||
* @param callable
|
||||
*/
|
||||
public finish(callable: () => void)
|
||||
{
|
||||
this.onFinish = callable;
|
||||
|
||||
if (this.finished)
|
||||
callable();
|
||||
}
|
||||
}
|
||||
|
||||
export default class Downloader
|
||||
{
|
||||
/**
|
||||
* Download file
|
||||
*
|
||||
* @param uri
|
||||
* @param output
|
||||
*
|
||||
* @returns Promise<Stream>
|
||||
*/
|
||||
public static async download(uri: string, output: string): Promise<Stream>
|
||||
{
|
||||
return new Promise(async (resolve) => {
|
||||
// @ts-expect-error
|
||||
let statsRaw = await Neutralino.os.execCommand(`wget --spider "${uri}"`);
|
||||
|
||||
if (statsRaw.stdOut == '')
|
||||
statsRaw = statsRaw.stdErr;
|
||||
|
||||
else statsRaw = statsRaw.stdOut;
|
||||
|
||||
let length: any = /Length: ([\d]+)/.exec(statsRaw)[1] ?? null;
|
||||
|
||||
if (length !== null)
|
||||
length = parseInt(length);
|
||||
|
||||
resolve(new Stream(uri, output, length));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export { Stream };
|
Loading…
Reference in a new issue