diff options
author | André Oliveira <37463445+SpecialAro@users.noreply.github.com> | 2023-09-02 16:28:04 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-02 15:28:04 +0000 |
commit | d1c623f4c3d72c859f9ad9cb985be127d6a3eb62 (patch) | |
tree | e102da856ae328c70e822d60ac53909acd4627b9 /src/models | |
parent | Downgrade 'electron' to 25.x (diff) | |
download | ferdium-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')
-rw-r--r-- | src/models/Service.ts | 81 |
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 @@ | |||
1 | import { join } from 'node:path'; | 1 | import { join, basename } from 'node:path'; |
2 | import { autorun, action, computed, makeObservable, observable } from 'mobx'; | 2 | import { autorun, action, computed, makeObservable, observable } from 'mobx'; |
3 | import { ipcRenderer } from 'electron'; | 3 | import { ipcRenderer } from 'electron'; |
4 | import { webContents } from '@electron/remote'; | 4 | import { webContents } from '@electron/remote'; |
5 | import ElectronWebView from 'react-electron-web-view'; | 5 | import ElectronWebView from 'react-electron-web-view'; |
6 | 6 | ||
7 | import { v4 as uuidV4 } from 'uuid'; | ||
7 | import { todosStore } from '../features/todos'; | 8 | import { todosStore } from '../features/todos'; |
8 | import { isValidExternalURL, normalizedUrl } from '../helpers/url-helpers'; | 9 | import { isValidExternalURL, normalizedUrl } from '../helpers/url-helpers'; |
9 | import UserAgent from './UserAgent'; | 10 | import 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); |