Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add download progress to sidebar #214

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ We use Todesktop to build and codesign our releases. To make a new release:
3. Merge the PR
4. A build will automatically start and you can view it at https://app.todesktop.com


### Publish Locally

```bash
Expand Down
78 changes: 69 additions & 9 deletions src/models/DownloadManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ export class DownloadManager {
const download = this.downloads.get(url);

if (download) {
this.reportProgress(url, 0, DownloadStatus.PENDING);
this.reportProgress({
url,
filename: download.filename,
savePath: download.savePath,
progress: 0,
status: DownloadStatus.PENDING,
});
item.setSavePath(download.tempPath);
download.item = item;
log.info(`Setting save path to ${item.getSavePath()}`);
Expand All @@ -61,9 +67,21 @@ export class DownloadManager {
const progress = item.getReceivedBytes() / item.getTotalBytes();
if (item.isPaused()) {
log.info('Download is paused');
this.reportProgress(url, progress, DownloadStatus.PAUSED);
this.reportProgress({
url,
progress,
filename: download.filename,
savePath: download.savePath,
status: DownloadStatus.PAUSED,
});
} else {
this.reportProgress(url, progress, DownloadStatus.IN_PROGRESS);
this.reportProgress({
url,
progress,
filename: download.filename,
savePath: download.savePath,
status: DownloadStatus.IN_PROGRESS,
});
}
}
});
Expand All @@ -77,12 +95,24 @@ export class DownloadManager {
log.error(`Failed to rename downloaded file: ${error}. Deleting temp file.`);
fs.unlinkSync(download.tempPath);
}
this.reportProgress(url, 1, DownloadStatus.COMPLETED);
this.reportProgress({
url,
filename: download.filename,
savePath: download.savePath,
progress: 1,
status: DownloadStatus.COMPLETED,
});
this.downloads.delete(url);
} else {
log.info(`Download failed: ${state}`);
const progress = item.getReceivedBytes() / item.getTotalBytes();
this.reportProgress(url, progress, DownloadStatus.ERROR);
this.reportProgress({
url,
filename: download.filename,
progress,
status: DownloadStatus.ERROR,
savePath: download.savePath,
});
}
});
}
Expand All @@ -93,14 +123,28 @@ export class DownloadManager {
const localSavePath = this.getLocalSavePath(filename, savePath);
if (!this.isPathInModelsDirectory(localSavePath)) {
log.error(`Save path ${localSavePath} is not in models directory ${this.modelsDirectory}`);
this.reportProgress(url, 0, DownloadStatus.ERROR, 'Save path is not in models directory');
this.reportProgress({
url,
savePath,
filename,
progress: 0,
status: DownloadStatus.ERROR,
message: 'Save path is not in models directory',
});
return false;
}

const validationResult = this.validateSafetensorsFile(url, filename);
if (!validationResult.isValid) {
log.error(validationResult.error);
this.reportProgress(url, 0, DownloadStatus.ERROR, validationResult.error);
this.reportProgress({
url,
savePath,
filename,
progress: 0,
status: DownloadStatus.ERROR,
message: validationResult.error,
});
return false;
}

Expand Down Expand Up @@ -248,13 +292,29 @@ export class DownloadManager {
return absoluteFilePath.startsWith(absoluteModelsDir);
}

private reportProgress(url: string, progress: number, status: DownloadStatus, message: string = ''): void {
log.info(`Download progress: ${progress}, status: ${status}, message: ${message}`);
private reportProgress({
url,
progress,
status,
savePath,
filename,
message = '',
}: {
url: string;
progress: number;
status: DownloadStatus;
filename: string;
savePath: string;
message?: string;
}): void {
log.info(`Download progress [${filename}]: ${progress}, status: ${status}, message: ${message}`);
this.mainWindow.send(IPC_CHANNELS.DOWNLOAD_PROGRESS, {
url,
progress,
status,
message,
savePath,
filename,
});
}

Expand Down
Loading