Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dogodo-cc committed Oct 22, 2024
1 parent 8ba171a commit 2f6fa24
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
16 changes: 10 additions & 6 deletions app-views/src/pages/demo/download.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ function bytesToSize(bytes: number): string {
let i = Math.floor(Math.log(bytes) / Math.log(1024));
return parseFloat((bytes / Math.pow(1024, i)).toFixed(2)) + ' ' + units[i];
}
// const name = '380_'
const name = 'hello'
const links = [
`http://ftp.cocos.org/TestBuilds/Dashboard/local-test/1/${name}1.zip`,
`http://ftp.cocos.org/TestBuilds/Dashboard/local-test/1/${name}2.zip`,
`http://ftp.cocos.org/TestBuilds/Dashboard/local-test/1/${name}3.zip`,
`http://ftp.cocos.org/TestBuilds/Dashboard/local-test/1/${name}4.zip`,
`http://ftp.cocos.org/TestBuilds/Dashboard/local-test/1/${name}5.zip`,
`http://ftp.cocos.org/TestBuilds/Dashboard/local-test/1/${name}6.zip`
]


const url = 'https://www.90s.co/videos/hello.zip';
const url2 = 'http://ftp.cocos.org/TestBuilds/Editor-3d/v3.8.4/CocosCreator-v3.8.4-win-082913.zip'
const url3 = 'http://ftp.cocos.org/TestBuilds/Editor-3d/v3.8.4/CocosCreator-v3.8.4-mac-082613.zip';

const links = [url, url2, url3];

const taskList = ref<IDownloadItem[]>([]);


Expand Down
18 changes: 11 additions & 7 deletions app/src/download/download-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class DownloadItem extends DownloadItemEmitter implements IDownloadItem {
file: string;
contentLength: number;
percent: number;
receivedBytes: number;
isPause?: boolean;
isWait?: boolean;
isLoading?: boolean;
Expand All @@ -66,16 +67,20 @@ export class DownloadItem extends DownloadItemEmitter implements IDownloadItem {
this.url = url;
this.contentLength = 0;
this.percent = 0;
this.receivedBytes = 0;
this.file = file;
this.chunkSize = chunkSize;
}

// 开始下载
async downloadStart() {
this.isPause = false;
this.isWait = false;
this.isLoading = true;
// 每次下载都请求一次 contentLength 方便和本地数据做对比
const contentLength = await this.getContentLength(this.url);
if (!contentLength) {
console.log(`${this.url} content-length is invilod: ${contentLength}`);
console.log(`${this.url} content-length is invalid: ${contentLength}`);
this.downloadEnd(false);
return;
}
Expand All @@ -99,9 +104,7 @@ export class DownloadItem extends DownloadItemEmitter implements IDownloadItem {

this.contentLength = contentLength;
this.percent = size / contentLength;
this.isPause = false;
this.isWait = false;
this.isLoading = true;
this.receivedBytes = size;
this.bytesPerSecond = 0;

ensureDir(dirname(this.file)); // 每次下载都确保一下路径是存在的,避免创建文件的错误
Expand Down Expand Up @@ -156,6 +159,7 @@ export class DownloadItem extends DownloadItemEmitter implements IDownloadItem {
})
.then(() => {
this.percent = currentChunkEnd / (this.contentLength - 1);
this.receivedBytes = currentChunkEnd;
const endTime = Date.now();
this.bytesPerSecond = ((currentChunkEnd - downloadedSize) / (endTime - startTime)) * 1000;
this.emit('download:progress', this.pickItem());
Expand Down Expand Up @@ -193,7 +197,7 @@ export class DownloadItem extends DownloadItemEmitter implements IDownloadItem {

// 用于传递数据的 item,需要剥离一些不可序列化的数据
pickItem(): IDownloadItem {
const { url, file, contentLength, percent, isPause, isWait, isLoading, bytesPerSecond } = this;
const { url, file, contentLength, percent, isPause, isWait, isLoading, bytesPerSecond, receivedBytes } = this;
return {
url,
file,
Expand All @@ -203,6 +207,7 @@ export class DownloadItem extends DownloadItemEmitter implements IDownloadItem {
isWait,
isLoading,
bytesPerSecond,
receivedBytes,
};
}
private getContentLength(url: string): Promise<number> {
Expand All @@ -211,8 +216,7 @@ export class DownloadItem extends DownloadItemEmitter implements IDownloadItem {
.head(url)
.then((res) => {
const contentLength = res.headers['content-length'];

if (contentLength && res.headers['accept-ranges'] === 'bytes') {
if (contentLength) {
resolve(Number(contentLength));
}
resolve(0);
Expand Down
2 changes: 1 addition & 1 deletion app/src/download/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class DownloadManger {
this.checktNextTask();
}

private checktNextTask() {
checktNextTask() {
if (this.downloadWaitTasks.length > 0) {
const nextItem = this.downloadWaitTasks[0];
this.createTask(nextItem.url);
Expand Down
2 changes: 1 addition & 1 deletion app/src/download/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { broadcast } from '../ipc.js';
import { join } from 'node:path';
import type { IDownloadItem } from './type.js';

const downloadManger = new DownloadManger(join(app.getPath('downloads'), 'CocosDashboard'));
const downloadManger = new DownloadManger(join(app.getPath('downloads'), 'CocosDashboard_123'));

const list: IDownloadItem[] = [];

Expand Down
1 change: 1 addition & 0 deletions app/src/download/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface IDownloadItem {
file: string; // 文件保存地址
contentLength: number; // 文件大小
percent: number; // 下载的进度
receivedBytes: number; // 下载到的大小
bytesPerSecond?: number; // 下载速度
isPause?: boolean; // 暂停下载是用户交互控制的 比如手动暂停 或者 退出程序
isWait?: boolean; // 等待是内部机制,不能同时下载超过一定数量
Expand Down

0 comments on commit 2f6fa24

Please sign in to comment.