From 2d1eaa6eb40c906e503d84e597e68fcf570d9748 Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Tue, 21 Dec 2021 01:39:48 +0200 Subject: [PATCH] Added downloader class --- package.json | 4 +- src/pages/index.ts | 13 +++++- src/ts/Downloader.ts | 95 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 src/ts/Downloader.ts diff --git a/package.json b/package.json index 7315bb8..911501b 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/pages/index.ts b/src/pages/index.ts index 1472104..99e89e8 100644 --- a/src/pages/index.ts +++ b/src/pages/index.ts @@ -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: () => { diff --git a/src/ts/Downloader.ts b/src/ts/Downloader.ts new file mode 100644 index 0000000..1482813 --- /dev/null +++ b/src/ts/Downloader.ts @@ -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 + */ + public static async download(uri: string, output: string): Promise + { + 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 };