Commit 6a33d0bc authored by wudizhanche1000's avatar wudizhanche1000

下载速度显示。

parent ab762033
......@@ -30,7 +30,19 @@ export interface Action {
export class AppStatus {
progress: number;
total: number;
status: string;
private _status: string;
get status(): string {
return this._status
}
set status(status: string) {
this.progress = 0;
this.total = 0;
this.progressMessage = '';
this._status = status;
}
progressMessage: string;
}
export class App {
id: string;
......@@ -94,13 +106,7 @@ export class App {
}
progressMessage(): string | undefined {
if (this.isDownloading()) {
return '1M/s'
} else if (this.isInstalling()) {
return 'マニュアル/index.html'
} else if (this.isWaiting()) {
return 'wine, Neko Project II'
}
return this.status.progressMessage;
}
constructor(app: any) {
......
......@@ -120,7 +120,7 @@ export class AppsService {
const addDownloadTask = async(app: App, dir: string) => {
let metalinkUrl = app.download;
if (app.id === "ygopro") {
metalinkUrl="https://thief.mycard.moe/metalinks/ygopro-"+process.platform+".meta4";
metalinkUrl = "https://thief.mycard.moe/metalinks/ygopro-" + process.platform + ".meta4";
}
let metalink = await this.http.get(metalinkUrl).map((response) => {
return response.text()
......@@ -130,8 +130,24 @@ export class AppsService {
let observable = this.downloadService.downloadProgress(downloadId);
return new Promise((resolve, reject) => {
observable.subscribe((task) => {
if (task.totalLength) {
app.status.total = task.totalLength;
} else {
app.status.total = 0;
}
app.status.progress = task.completedLength;
app.status.total = task.totalLength;
console.log(task);
if (task.downloadSpeed) {
let currentSpeed = parseInt(task.downloadSpeed);
const speedUnit = ["Byte/s", "KB/s", "MB/s", "GB/s", "TB/s"];
let currentUnit = Math.floor(Math.log(currentSpeed) / Math.log(1024));
console.log(currentSpeed, currentUnit);
app.status.progressMessage = (currentSpeed / 1024 ** currentUnit).toFixed(1) + " " + speedUnit[currentUnit];
}else{
app.status.progressMessage='';
}
this.ref.tick();
}, (error) => {
reject(error);
......@@ -146,7 +162,6 @@ export class AppsService {
let apps: App[] = [];
let dependencies = app.findDependencies();
apps.push(...dependencies, app);
console.log(apps);
let downloadPath = path.join(option.installLibrary, 'downloading');
let tasks: Promise<any>[] = [];
for (let a of apps) {
......
......@@ -73,21 +73,31 @@ export class DownloadService {
let status = '';
let completedLength = 0;
let totalLength = 0;
let downloadSpeed = 0;
let gidList = this.map.get(id) !;
this.updateEmitter.subscribe((value: string) => {
let statusList = new Array(gidList.length);
let newCompletedLength = 0;
let newTotalLength = 0;
let newDownloadSpeed = 0;
for (let [index,gid] of gidList.entries()) {
let task = this.taskList.get(gid)!;
statusList[index] = task.status;
newCompletedLength += parseInt(task.completedLength);
newTotalLength += parseInt(task.totalLength);
newDownloadSpeed += parseInt(task.downloadSpeed);
}
if (newCompletedLength !== completedLength || newTotalLength !== totalLength) {
completedLength = newCompletedLength;
totalLength = newTotalLength;
observer.next({status: status, completedLength: completedLength, totalLength: totalLength});
downloadSpeed = newDownloadSpeed;
observer.next({
status: status,
completedLength: completedLength,
totalLength: totalLength,
downloadSpeed: downloadSpeed
});
}
status = statusList.reduce((value, current) => {
if (value === "complete" && current === "complete") {
......
......@@ -84,55 +84,63 @@ export class InstallService {
this.installingId = id;
try {
let app = task.app;
let option = task.option;
// if (!app.isInstalled()) {
let checksumFile = await this.getChecksumFile(app);
console.log(checksumFile);
if (app.parent) {
let conflictFiles = new Set<string>();
let parentFilesMap = app.parent.local!.files;
for (let key of checksumFile.keys()) {
if (parentFilesMap.has(key)) {
conflictFiles.add(key);
let dependencies = app.findDependencies();
let readyForInstall = dependencies.every((dependency) => {
return dependency.isReady();
});
if (readyForInstall) {
let option = task.option;
// if (!app.isInstalled()) {
let checksumFile = await this.getChecksumFile(app);
if (app.parent) {
let conflictFiles = new Set<string>();
let parentFilesMap = app.parent.local!.files;
for (let key of checksumFile.keys()) {
if (parentFilesMap.has(key)) {
conflictFiles.add(key);
}
}
if (conflictFiles.size > 0) {
let backupPath = path.join(option.installLibrary, "backup", app.parent.id);
this.backupFiles(option.installDir, backupPath, conflictFiles);
}
}
if (conflictFiles.size > 0) {
let backupPath = path.join(option.installLibrary, "backup", app.parent.id);
this.backupFiles(option.installDir, backupPath, conflictFiles);
let allFiles = new Set(checksumFile.keys());
app.status.status = "installing";
app.status.total = allFiles.size;
app.status.progress = 0;
// let timeNow = new Date().getTime();
for (let file of option.downloadFiles) {
await this.createDirectory(option.installDir);
let interval = setInterval(() => {
}, 500);
await new Promise((resolve, reject) => {
this.extract(file, option.installDir).subscribe(
(lastItem: string) => {
app.status.progress += 1;
app.status.progressMessage = lastItem;
// if (new Date().getTime() - timeNow > 500) {
// timeNow = new Date().getTime();
// }
},
(error) => {
reject(error);
},
() => {
resolve();
});
});
clearInterval(interval);
}
await this.postInstall(app, option.installDir);
let local = new AppLocal();
local.path = option.installDir;
local.files = checksumFile;
local.version = app.version;
app.local = local;
this.saveAppLocal(app);
app.status.status = "ready";
}
let allFiles = new Set(checksumFile.keys());
app.status.status = "installing";
app.status.total = allFiles.size;
app.status.progress = 0;
let timeNow = new Date().getTime();
for (let file of option.downloadFiles) {
await this.createDirectory(option.installDir);
await new Promise((resolve, reject) => {
this.extract(file, option.installDir).subscribe(
(lastItem: string) => {
console.log(app.status.progress, app.status.total, lastItem);
app.status.progress += 1;
if (new Date().getTime() - timeNow > 500) {
timeNow = new Date().getTime();
this.ref.tick();
}
},
(error) => {
reject(error);
},
() => {
resolve();
});
});
}
let local = new AppLocal();
local.path = option.installDir;
local.files = checksumFile;
local.version = app.version;
app.local = local;
this.saveAppLocal(app);
app.status.status = "ready";
// }
} catch (e) {
throw e;
......@@ -168,7 +176,6 @@ export class InstallService {
input: <ReadableStream>tarProcess.stderr,
});
rl.on('line', (input: string) => {
console.log(input.split(" ", 2));
observer.next(input.split(" ", 2)[1]);
});
tarProcess.on('exit', (code) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment