Skip to content

Commit

Permalink
fix: Added Notification for handling S3 download expiration (#2488)
Browse files Browse the repository at this point in the history
* fix: Download link expiry handled

* fix: Variable Name Changed

* fix: body and subtitle added

* fix: UI Changed for Expired Download

Co-authored-by: Jean Brito <[email protected]>
  • Loading branch information
henit-chobisa and jeanfbrito authored Sep 1, 2022
1 parent 0c5ace0 commit 94391db
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/downloads/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ export const DownloadStatus = {

export type Download = {
itemId: number;
state: 'progressing' | 'paused' | 'completed' | 'cancelled' | 'interrupted';
state:
| 'progressing'
| 'paused'
| 'completed'
| 'cancelled'
| 'interrupted'
| 'expired';
status: typeof DownloadStatus[keyof typeof DownloadStatus];
fileName: string;
receivedBytes: number;
Expand Down
50 changes: 45 additions & 5 deletions src/downloads/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,60 @@ export const setupDownloads = (): void => {
});

handle('downloads/retry', async (_webContent, itemId) => {
const { url, webContentsId } = select(({ downloads, servers }) => {
const { url, serverUrl } = downloads[itemId];
const { download, webContentsId } = select(({ downloads, servers }) => {
const download = downloads[itemId];
const { webContentsId } =
servers.find((server) => server.url === serverUrl) ?? {};
return { url, webContentsId };
servers.find((server) => server.url === download.serverUrl) ?? {};
return { download, webContentsId };
});

const downloadStartTimestamp = new URL(download.url).searchParams.get(
'X-Amz-Date'
);
const expiresIn =
new URL(download.url).searchParams.get('X-Amz-Expires') ?? 120;
const parsedStartTime = {
year: downloadStartTimestamp?.substring(0, 4),
month: downloadStartTimestamp?.substring(4, 6),
day: downloadStartTimestamp?.substring(6, 8),
hour: downloadStartTimestamp?.substring(9, 11),
minute: downloadStartTimestamp?.substring(11, 13),
second: downloadStartTimestamp?.substring(13, 15),
};

const s3Expired =
new Date().getTime() >
new Date(
`${parsedStartTime.year}-${parsedStartTime.month}-${parsedStartTime.day}T${parsedStartTime.hour}:${parsedStartTime.minute}:${parsedStartTime.second}Z`
).getTime() +
+expiresIn * 1000;

if (s3Expired) {
createNotification({
title: t('downloads.notifications.downloadExpired'),
body: t('downloads.notifications.downloadExpiredMessage'),
subtitle: download.fileName,
});

dispatch({
type: DOWNLOAD_UPDATED,
payload: {
...download,
state: 'expired',
status: DownloadStatus.CANCELLED,
},
});

return;
}

dispatch({
type: DOWNLOAD_REMOVED,
payload: itemId,
});

if (webContentsId) {
webContents.fromId(webContentsId).downloadURL(url);
webContents.fromId(webContentsId).downloadURL(download.url);
}
});

Expand Down
4 changes: 3 additions & 1 deletion src/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@
"downloadFinished": "Download Finished",
"downloadInterrupted": "Download Interrupted",
"downloadCancelled": "Download Cancelled",
"downloadFailed": "Download could not be completed"
"downloadFailed": "Download Failed",
"downloadExpired" : "Downlaod Expired",
"downloadExpiredMessage" : "Please retry downloading from source."
},
"filters": {
"search": "Search",
Expand Down
16 changes: 12 additions & 4 deletions src/ui/components/DownloadsManagerView/DownloadItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const DownloadItem: FC<DownloadItemProps> = ({
}, [itemId]);

const errored = state === 'interrupted' || state === 'cancelled';
const expired = state === 'expired';
const percentage = useMemo(
() => Math.floor((receivedBytes / totalBytes) * 100),
[receivedBytes, totalBytes]
Expand All @@ -131,7 +132,7 @@ const DownloadItem: FC<DownloadItemProps> = ({
<Box width={344} mis={8}>
<Box
mbe={4}
color={errored ? 'danger-500' : 'default'}
color={errored || expired ? 'danger-500' : 'default'}
fontScale='p1'
withTruncatedText
>
Expand Down Expand Up @@ -179,9 +180,16 @@ const DownloadItem: FC<DownloadItemProps> = ({
) : null}
</Box>
<Box display='flex' fontScale='c1'>
<ActionButton onClick={handleCopyLink}>
{t('downloads.item.copyLink')}
</ActionButton>
{expired && (
<ActionButton onClick={handleRemove}>
{t('downloads.item.remove')}
</ActionButton>
)}
{!expired && (
<ActionButton onClick={handleCopyLink}>
{t('downloads.item.copyLink')}
</ActionButton>
)}
{state === 'progressing' && (
<>
<ActionButton onClick={handlePause}>
Expand Down

0 comments on commit 94391db

Please sign in to comment.