Skip to content
This repository has been archived by the owner on May 22, 2020. It is now read-only.

Commit

Permalink
RUN-4646 Differentiate between original file name and user chosen nam…
Browse files Browse the repository at this point in the history
…e on download events (#562)
  • Loading branch information
nicholasdgoodman authored and HarsimranSingh committed Sep 28, 2018
1 parent a43618e commit a45071c
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/browser/api/file_download.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { app } from 'electron';
import * as path from 'path';
import { Identity } from '../../shapes';
import { writeToLog } from '../log';
import ofEvents from '../of_events';
Expand All @@ -19,6 +20,7 @@ interface FileEvent {
url: string;
mimeType: string;
fileName: string;
originalFileName: string;
totalBytes: number;
startTime: number;
contentDisposition: string;
Expand All @@ -27,6 +29,9 @@ interface FileEvent {
topic: string;
uuid: string;
name: string;
type: string;
state: string;
downloadedBytes: number;
}

interface FileDownloadLocation {
Expand Down Expand Up @@ -55,20 +60,24 @@ export function createWillDownloadEventListener(identity: Identity): (event: any

try {
const fileUuid: string = app.generateGUID();
const baseFileEvent: FileEvent = {
fileUuid,
const getFileEventData = (type: string, state: DownloadState): FileEvent => ({
type,
state,
url: item.getURL(),
mimeType: item.getMimeType(),
fileName: item.getFilename(),
fileName: path.parse(item.getSavePath()).base,
originalFileName: item.getFilename(),
totalBytes: item.getTotalBytes(),
startTime: item.getStartTime(),
contentDisposition: item.getContentDisposition(),
lastModifiedTime: item.getLastModifiedTime(),
eTag: item.getETag(),
downloadedBytes: item.getReceivedBytes(),
topic: EVENT_TOPIC,
uuid,
name
};
name,
fileUuid
});

const progressTracker = (event: any, state: DownloadState): void => {
try {
Expand All @@ -78,19 +87,19 @@ export function createWillDownloadEventListener(identity: Identity): (event: any
reportedState = 'paused';
}

ofEvents.emit(route.window(FILE_DOWNLOAD_EVENTS.PROGRESS, uuid, name), Object.assign({
type: FILE_DOWNLOAD_EVENTS.PROGRESS,
state,
downloadedBytes: item.getReceivedBytes()
}, baseFileEvent));
ofEvents.emit(
route.window(FILE_DOWNLOAD_EVENTS.PROGRESS, uuid, name),
getFileEventData(FILE_DOWNLOAD_EVENTS.PROGRESS, state)
);
} catch (e) {
writeToLog('info', e);
}
};

ofEvents.emit(route.window(FILE_DOWNLOAD_EVENTS.STARTED, uuid, name), Object.assign({
type: FILE_DOWNLOAD_EVENTS.STARTED
}, baseFileEvent));
ofEvents.emit(
route.window(FILE_DOWNLOAD_EVENTS.STARTED, uuid, name),
getFileEventData(FILE_DOWNLOAD_EVENTS.STARTED, 'started')
);

item.on('updated', progressTracker);
item.once('done', (event: Event, state: DownloadState) => {
Expand All @@ -105,13 +114,13 @@ export function createWillDownloadEventListener(identity: Identity): (event: any

//log that the download failed.
if (state !== 'completed') {
writeToLog('info', `download ${baseFileEvent.fileUuid } failed, state: ${state}`);
writeToLog('info', `download ${fileUuid} failed, state: ${state}`);
}

ofEvents.emit(route.window(FILE_DOWNLOAD_EVENTS.COMPLETED, uuid, name), Object.assign({
type: FILE_DOWNLOAD_EVENTS.COMPLETED,
state
}, baseFileEvent));
ofEvents.emit(
route.window(FILE_DOWNLOAD_EVENTS.COMPLETED, uuid, name),
getFileEventData(FILE_DOWNLOAD_EVENTS.COMPLETED, state)
);

} catch (e) {
writeToLog('info', e);
Expand Down

0 comments on commit a45071c

Please sign in to comment.