SDA-2443 Fixed incorrect file name in edge cases

This commit is contained in:
psjostrom 2020-09-07 14:44:15 +02:00
parent b66d59d8cb
commit d325d89373
2 changed files with 3 additions and 31 deletions

View File

@ -9,32 +9,11 @@ const DOWNLOAD_MANAGER_NAMESPACE = 'DownloadManager';
export interface IDownloadItem { export interface IDownloadItem {
_id: string; _id: string;
fileName: string; fileName: string;
fileDisplayName?: string;
savedPath: string; savedPath: string;
total: string; total: string;
flashing?: boolean;
count?: number;
} }
class DownloadHandler { class DownloadHandler {
/**
* Checks and constructs file name
*
* @param fileName {String} Filename
* @param item {IDownloadItem} Download Item
*/
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('.');
const fileCount = ' (' + item.count + ')';
fileName = fileName.slice(0, extLastIndex) + fileCount + fileName.slice(extLastIndex);
}
return fileName;
}
/** /**
* Show dialog for failed cases * Show dialog for failed cases
*/ */
@ -112,14 +91,6 @@ class DownloadHandler {
* @param item Download item * @param item Download item
*/ */
public onDownloadSuccess(item: IDownloadItem): void { public onDownloadSuccess(item: IDownloadItem): void {
let itemCount = 0;
for (const existingItem of this.items) {
if (item.fileName === existingItem.fileName) {
itemCount++;
}
}
item.count = itemCount;
item.fileDisplayName = DownloadHandler.getFileDisplayName(item.fileName, item);
this.items.push(item); this.items.push(item);
this.sendDownloadCompleted(item); this.sendDownloadCompleted(item);
} }
@ -138,7 +109,7 @@ class DownloadHandler {
if (this.window && !this.window.isDestroyed()) { if (this.window && !this.window.isDestroyed()) {
logger.info(`download-handler: Download completed! Informing the client!`); logger.info(`download-handler: Download completed! Informing the client!`);
this.window.send('download-completed', { this.window.send('download-completed', {
id: item._id, fileDisplayName: item.fileDisplayName, fileSize: item.total, id: item._id, fileDisplayName: item.fileName, fileSize: item.total,
}); });
} }
} }

View File

@ -403,11 +403,12 @@ export const handleDownloadManager = (_event, item: Electron.DownloadItem, webCo
// Send file path when download is complete // Send file path when download is complete
item.once('done', (_e, state) => { item.once('done', (_e, state) => {
if (state === 'completed') { if (state === 'completed') {
const savePathSplit = item.getSavePath()?.split('/');
const data: IDownloadItem = { const data: IDownloadItem = {
_id: getGuid(), _id: getGuid(),
savedPath: item.getSavePath() || '', savedPath: item.getSavePath() || '',
total: filesize(item.getTotalBytes() || 0), total: filesize(item.getTotalBytes() || 0),
fileName: item.getFilename() || 'No name', fileName: savePathSplit[savePathSplit.length - 1] || 'No name',
}; };
logger.info('window-utils: Download completed, informing download manager'); logger.info('window-utils: Download completed, informing download manager');
webContents.send('downloadCompleted', data); webContents.send('downloadCompleted', data);