aboutsummaryrefslogtreecommitdiffstats
path: root/src/models/Service.ts
diff options
context:
space:
mode:
authorLibravatar André Oliveira <37463445+SpecialAro@users.noreply.github.com>2023-09-02 16:28:04 +0100
committerLibravatar GitHub <noreply@github.com>2023-09-02 15:28:04 +0000
commitd1c623f4c3d72c859f9ad9cb985be127d6a3eb62 (patch)
treee102da856ae328c70e822d60ac53909acd4627b9 /src/models/Service.ts
parentDowngrade 'electron' to 25.x (diff)
downloadferdium-app-d1c623f4c3d72c859f9ad9cb985be127d6a3eb62.tar.gz
ferdium-app-d1c623f4c3d72c859f9ad9cb985be127d6a3eb62.tar.zst
ferdium-app-d1c623f4c3d72c859f9ad9cb985be127d6a3eb62.zip
feat: Add Download Manager (pause, stop, delete) (#1339)
Diffstat (limited to 'src/models/Service.ts')
-rw-r--r--src/models/Service.ts81
1 files changed, 80 insertions, 1 deletions
diff --git a/src/models/Service.ts b/src/models/Service.ts
index 265b3e13c..b1f0bc271 100644
--- a/src/models/Service.ts
+++ b/src/models/Service.ts
@@ -1,9 +1,10 @@
1import { join } from 'node:path'; 1import { join, basename } from 'node:path';
2import { autorun, action, computed, makeObservable, observable } from 'mobx'; 2import { autorun, action, computed, makeObservable, observable } from 'mobx';
3import { ipcRenderer } from 'electron'; 3import { ipcRenderer } from 'electron';
4import { webContents } from '@electron/remote'; 4import { webContents } from '@electron/remote';
5import ElectronWebView from 'react-electron-web-view'; 5import ElectronWebView from 'react-electron-web-view';
6 6
7import { v4 as uuidV4 } from 'uuid';
7import { todosStore } from '../features/todos'; 8import { todosStore } from '../features/todos';
8import { isValidExternalURL, normalizedUrl } from '../helpers/url-helpers'; 9import { isValidExternalURL, normalizedUrl } from '../helpers/url-helpers';
9import UserAgent from './UserAgent'; 10import UserAgent from './UserAgent';
@@ -523,6 +524,84 @@ export default class Service {
523 }); 524 });
524 525
525 if (webviewWebContents) { 526 if (webviewWebContents) {
527 webviewWebContents.session.on('will-download', (event, item) => {
528 event.preventDefault();
529
530 const downloadId = uuidV4();
531
532 window['ferdium'].actions.app.addDownload({
533 id: downloadId,
534 serviceId: this.id,
535 filename: item.getFilename(),
536 url: item.getURL(),
537 savePath: item.getSavePath(),
538 });
539
540 item.addListener('updated', (event, state) => {
541 if (state === 'interrupted') {
542 debug('Download is interrupted but can be resumed');
543 } else if (state === 'progressing') {
544 if (item.isPaused()) {
545 debug('Download is paused');
546 } else {
547 debug(`Received bytes: ${item.getReceivedBytes()}`);
548 }
549 }
550 window['ferdium'].actions.app.updateDownload({
551 id: downloadId,
552 serviceId: this.id,
553 filename: basename(item.getSavePath()),
554 url: item.getURL(),
555 savePath: item.getSavePath(),
556 receivedBytes: item.getReceivedBytes(),
557 totalBytes: item.getTotalBytes(),
558 state,
559 });
560 debug('download updated', event, state);
561 });
562 item.addListener('done', (event, state) => {
563 debug('download done', event, state);
564 if (state === 'completed') {
565 debug('Download successfully');
566 } else {
567 if (state === 'cancelled' && item.getSavePath() === '') {
568 window['ferdium'].actions.app.removeDownload(downloadId);
569 debug('Download is cancelled');
570 }
571 debug(`Download failed: ${state}`);
572 }
573
574 window['ferdium'].actions.app.endedDownload({
575 id: downloadId,
576 serviceId: this.id,
577 receivedBytes: item.getReceivedBytes(),
578 totalBytes: item.getTotalBytes(),
579 state,
580 });
581 });
582
583 ipcRenderer.on('toggle-pause-download', (_, data) => {
584 debug('toggle-pause-download', item.isPaused(), item.getState());
585 if (data.downloadId === downloadId || data.downloadId === undefined) {
586 if (item.isPaused()) {
587 item.resume();
588 } else {
589 item.pause();
590 }
591 }
592 debug('toggle-pause-download', item.isPaused(), item.getState());
593 window['ferdium'].actions.app.updateDownload({
594 id: downloadId,
595 paused: item.isPaused(),
596 });
597 });
598
599 ipcRenderer.on('stop-download', (_, data) => {
600 if (data === undefined || downloadId === data.downloadId) {
601 item.cancel();
602 }
603 });
604 });
526 webviewWebContents.on('login', (event, _, authInfo, callback) => { 605 webviewWebContents.on('login', (event, _, authInfo, callback) => {
527 // const authCallback = callback; 606 // const authCallback = callback;
528 debug('browser login event', authInfo); 607 debug('browser login event', authInfo);