feat: SDA-2025: download manager single item api (#987)

* SDA-2025: update download manager api

- Send a single item in place of an entire array when an item is downloaded

Signed-off-by: Vishwas Shashidhar <vishwas.shashidhar@symphony.com>

* SDA-2025: update download manager api

- fix origin in app bridge

Signed-off-by: Vishwas Shashidhar <vishwas.shashidhar@symphony.com>
This commit is contained in:
Vishwas Shashidhar 2020-05-06 17:02:14 +05:30 committed by GitHub
parent f09b5a8996
commit f95649b6b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 34 deletions

View File

@ -6,7 +6,7 @@ import { windowExists } from './window-utils';
const DOWNLOAD_MANAGER_NAMESPACE = 'DownloadManager';
export interface IDownloadManager {
export interface IDownloadItem {
_id: string;
fileName: string;
fileDisplayName?: string;
@ -22,9 +22,9 @@ class DownloadHandler {
* Checks and constructs file name
*
* @param fileName {String} Filename
* @param item {IDownloadManager} Download Item
* @param item {IDownloadItem} Download Item
*/
private static getFileDisplayName(fileName: string, item: IDownloadManager): string {
private static getFileDisplayName(fileName: string, item: IDownloadItem): string {
/* If it exists, add a count to the name like how Chrome does */
if (item.count && item.count > 0) {
const extLastIndex = fileName.lastIndexOf('.');
@ -54,7 +54,7 @@ class DownloadHandler {
}
private window!: Electron.WebContents | null;
private items: IDownloadManager[] = [];
private items: IDownloadItem[] = [];
/**
* Sets the window for the download handler
@ -108,7 +108,7 @@ class DownloadHandler {
* Handle a successful download
* @param item Download item
*/
public onDownloadSuccess(item: IDownloadManager): void {
public onDownloadSuccess(item: IDownloadItem): void {
let itemCount = 0;
for (const existingItem of this.items) {
if (item.fileName === existingItem.fileName) {
@ -118,7 +118,7 @@ class DownloadHandler {
item.count = itemCount;
item.fileDisplayName = DownloadHandler.getFileDisplayName(item.fileName, item);
this.items.push(item);
this.sendDownloadCompleted();
this.sendDownloadCompleted(item);
}
/**
@ -131,12 +131,12 @@ class DownloadHandler {
/**
* Send download completed event to the renderer process
*/
private sendDownloadCompleted(): void {
private sendDownloadCompleted(item: IDownloadItem): void {
if (this.window && !this.window.isDestroyed()) {
logger.info(`download-handler: Download completed! Informing the client!`);
this.window.send('download-completed', this.items.map((item) => {
return {id: item._id, fileDisplayName: item.fileDisplayName, fileSize: item.total};
}));
this.window.send('download-completed', {
id: item._id, fileDisplayName: item.fileDisplayName, fileSize: item.total,
});
}
}

View File

@ -14,7 +14,7 @@ import { getGuid } from '../common/utils';
import { whitelistHandler } from '../common/whitelist-handler';
import { autoLaunchInstance } from './auto-launch-controller';
import { CloudConfigDataTypes, config, IConfig, ICustomRectangle } from './config-handler';
import { downloadHandler, IDownloadManager } from './download-handler';
import { downloadHandler, IDownloadItem } from './download-handler';
import { memoryMonitor } from './memory-monitor';
import { screenSnippet } from './screen-snippet-handler';
import { updateAlwaysOnTop } from './window-actions';
@ -401,7 +401,7 @@ export const handleDownloadManager = (_event, item: Electron.DownloadItem, webCo
// Send file path when download is complete
item.once('done', (_e, state) => {
if (state === 'completed') {
const data: IDownloadManager = {
const data: IDownloadItem = {
_id: getGuid(),
savedPath: item.getSavePath() || '',
total: filesize(item.getTotalBytes() || 0),

View File

@ -599,8 +599,8 @@
function onDownload(data) {
if (data && data.status === 'download-completed') {
items = data.items;
console.log('Download completed!', data.items);
downloadedItem = data.item;
console.log('Download completed!', data.item);
} else {
console.log('Download failed!');
}
@ -830,7 +830,7 @@
}
});
let items = [];
let downloadedItem;
/**
* Download Manager api handler
*/
@ -861,10 +861,10 @@
}, false)
document.getElementById('open-download-item').addEventListener('click', () => {
if (!items || items.length < 1) {
if (!downloadedItem) {
alert('No files downloaded! Try again!');
}
const id = items[items.length - 1].id;
const id = downloadedItem.id;
if (window.ssf) {
window.ssf.openDownloadedItem(id);
} else {
@ -873,10 +873,10 @@
});
document.getElementById('show-download-item').addEventListener('click', () => {
if (!items || items.length < 1) {
if (!downloadedItem || downloadedItem.length < 1) {
alert('No files downloaded! Try again!');
}
const id = items[items.length - 1].id;
const id = downloadedItem.id;
if (window.ssf) {
window.ssf.showDownloadedItem(id);
} else {
@ -885,7 +885,7 @@
});
document.getElementById('close-download-manager').addEventListener('click', () => {
items = [];
downloadedItem = undefined;
if (window.ssf) {
window.ssf.clearDownloadedItems();
} else {

View File

@ -4,7 +4,7 @@ import { apiCmds, apiName } from '../../common/api-interface';
import { i18n } from '../../common/i18n-preload';
const DOWNLOAD_MANAGER_NAMESPACE = 'DownloadManager';
interface IDownloadManager {
interface IDownloadItem {
_id: string;
fileName: string;
savedPath: string;
@ -14,14 +14,14 @@ interface IDownloadManager {
}
interface IManagerState {
items: IDownloadManager[];
items: IDownloadItem[];
showMainComponent: boolean;
}
export default class DownloadManager {
private readonly eventHandlers = {
onInjectItem: (_event, item: IDownloadManager) => this.injectItem(item),
onInjectItem: (_event, item: IDownloadItem) => this.injectItem(item),
};
private readonly itemsContainer: HTMLElement | null;
private readonly closeButton: HTMLElement | null;
@ -81,10 +81,10 @@ export default class DownloadManager {
/**
* Loop through the items downloaded
*
* @param item {IDownloadManager}
* @param item {IDownloadItem}
*/
private renderItem(item: IDownloadManager): void {
const { _id, total, fileName }: IDownloadManager = item;
private renderItem(item: IDownloadItem): void {
const { _id, total, fileName }: IDownloadItem = item;
const fileDisplayName = this.getFileDisplayName(fileName, item);
const itemContainer = document.getElementById('download-main');
const parsedItem = this.domParser.parseFromString(`
@ -139,9 +139,9 @@ export default class DownloadManager {
/**
* Inject items to global var
*
* @param args {IDownloadManager}
* @param args {IDownloadItem}
*/
private injectItem(args: IDownloadManager): void {
private injectItem(args: IDownloadItem): void {
const { items } = this.state;
let itemCount = 0;
for (const item of items) {
@ -251,9 +251,9 @@ export default class DownloadManager {
* Checks and constructs file name
*
* @param fileName {String}
* @param item {IDownloadManager}
* @param item {IDownloadItem}
*/
private getFileDisplayName(fileName: string, item: IDownloadManager): string {
private getFileDisplayName(fileName: string, item: IDownloadItem): string {
/* If it exists, add a count to the name like how Chrome does */
if (item.count > 0) {
const extLastIndex = fileName.lastIndexOf('.');

View File

@ -1,13 +1,14 @@
import { ipcRenderer, remote } from 'electron';
const os = remote.require('os');
import { buildNumber, searchAPIVersion } from '../../package.json';
import { IDownloadItem } from '../app/download-handler';
import { ICustomBrowserWindow } from '../app/window-handler';
import {
apiCmds,
apiName,
IBadgeCount,
IBoundsChange,
ICPUUsage, IDownloadManager,
ICPUUsage,
ILogMsg,
IMediaPermission,
IRestartFloaterData,
@ -654,9 +655,9 @@ local.ipcRenderer.on('activity', (_event: Event, idleTime: number) => {
}
});
local.ipcRenderer.on('download-completed', (_event: Event, downloadItems: IDownloadManager[]) => {
if (typeof downloadItems === 'object' && typeof local.downloadManagerCallback === 'function') {
local.downloadManagerCallback({status: 'download-completed', items: downloadItems});
local.ipcRenderer.on('download-completed', (_event: Event, downloadItem: IDownloadItem) => {
if (typeof downloadItem === 'object' && typeof local.downloadManagerCallback === 'function') {
local.downloadManagerCallback({status: 'download-completed', item: downloadItem});
}
});