Skip to content

Commit 7e60bb1

Browse files
authored
fix download issues
1 parent f2cc547 commit 7e60bb1

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed

infrastructure/lib/features/translation/translation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ export class dt_translate extends Construct {
7979
removalPolicy: props.removalPolicy, // ASM-CFN1
8080
cors: [
8181
{
82-
allowedMethods: [s3.HttpMethods.PUT, s3.HttpMethods.POST],
82+
allowedMethods: [s3.HttpMethods.PUT, s3.HttpMethods.POST, s3.HttpMethods.HEAD, s3.HttpMethods.GET],
8383
allowedOrigins: ["*"],
8484
allowedHeaders: ["*"],
8585
exposedHeaders: ["ETag"],
86+
maxAge: 3000
8687
},
8788
],
8889
});

website/src/page/translation/historyTable.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { amplifyConfigureAppend } from "../../util/amplifyConfigure";
2424
import { formatJobNameId } from "../../util/formatJobNameId";
2525
import { formatTimestamp } from "../../util/formatTimestamp";
2626
import { getPresignedUrl } from "../../util/getPresignedUrl";
27+
import { formatEstimatedCompletion } from "../../util/formatEstimatedCompletion";
2728
import { describeS3Key } from "./util/describeS3Key";
2829

2930
const cfnOutputs = require("../../cfnOutputs.json");
@@ -136,11 +137,19 @@ export default function HistoryTable() {
136137
const k = describeS3Key({
137138
key: keys[i],
138139
});
140+
console.log("Fetching presigned url");
139141
const presignedUrl = await getPresignedUrl({
140142
path: `${k.scope}/${k.identity}/${k.jobId}/${k.stage}/${k.translateId}/${k.filename}`,
141143
bucketKey: "awsUserFilesS3Bucket",
142144
});
143-
window.open(presignedUrl, "_blank", "noopener,noreferrer");
145+
const response = await fetch(presignedUrl);
146+
console.log("Retrieved");
147+
148+
const blob = await response.blob();
149+
const link = document.createElement('a');
150+
link.href = window.URL.createObjectURL(blob);
151+
link.download = k.filename;
152+
link.click();
144153
}
145154
} catch (err) {
146155
console.log("error: ", err);
@@ -161,6 +170,11 @@ export default function HistoryTable() {
161170
header: t("generic_created"),
162171
cell: (item: Item) => formatTimestamp(item.createdAt),
163172
},
173+
{
174+
id: "estimatedCompletion",
175+
header: "Estimated Completion",
176+
cell: (item: Item) => formatEstimatedCompletion(item.createdAt, item.jobStatus),
177+
},
164178
{
165179
id: "source",
166180
header: t("generic_source"),
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { renderToStaticMarkup } from "react-dom/server";
2+
import { formatEstimatedCompletion } from "./formatEstimatedCompletion";
3+
4+
const minute = 60;
5+
6+
describe("formatTimestamp", () => {
7+
8+
test("calculates estimated completion correctly", () => {
9+
const currentTimeInSeconds = Math.floor(Date.now() / 1000);
10+
const fiveMinutesAgoInSeconds = currentTimeInSeconds - 5 * minute;
11+
12+
const element = formatEstimatedCompletion(fiveMinutesAgoInSeconds, "processing");
13+
const htmlString = renderToStaticMarkup(element);
14+
15+
expect(htmlString).toContain("15 minutes");
16+
});
17+
18+
test("calculates estimated completion correctly for single minute", () => {
19+
const currentTimeInSeconds = Math.floor(Date.now() / 1000);
20+
const nineteenMinutesAgo = currentTimeInSeconds - 19 * minute;
21+
22+
const element = formatEstimatedCompletion(nineteenMinutesAgo, "uploaded");
23+
const htmlString = renderToStaticMarkup(element);
24+
25+
expect(htmlString).toContain("1 minute");
26+
});
27+
28+
test("returns empty string for completed job", () => {
29+
const currentTimeInSeconds = Math.floor(Date.now() / 1000);
30+
const fiveMinutesAgoInSeconds = currentTimeInSeconds - 19 * minute;
31+
32+
const element = formatEstimatedCompletion(fiveMinutesAgoInSeconds, "COMPLETED");
33+
const htmlString = renderToStaticMarkup(element);
34+
35+
expect(htmlString).toEqual("");
36+
});
37+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0
3+
import React from "react";
4+
5+
function detiledTimestamp(unixSeconds: number) {
6+
const unixMilliseconds = unixSeconds * 1000;
7+
const timestamp = new Date(unixMilliseconds);
8+
return timestamp.toLocaleString();
9+
}
10+
11+
export function formatEstimatedCompletion(unixSeconds: number, jobStatus: string) {
12+
const jobOngoingStatuses = ["UPLOADED", "PROCESSING"]
13+
if (!jobOngoingStatuses.includes(jobStatus.toUpperCase())) {
14+
return ""
15+
}
16+
17+
const estimatedLengthOfJobMins = 20
18+
const timestamp = new Date((unixSeconds + estimatedLengthOfJobMins * 60) * 1000);
19+
const now = new Date();
20+
21+
const diff = timestamp.getTime() - now.getTime();
22+
const diffMinutes = Math.round(diff / (1000 * 60));
23+
24+
if (diffMinutes > 0) {
25+
return (
26+
<span title={detiledTimestamp(unixSeconds)}>
27+
{diffMinutes} minute{diffMinutes > 1 ? "s" : ""}
28+
</span>
29+
);
30+
}
31+
return (
32+
<span title={detiledTimestamp(unixSeconds)}>
33+
Now
34+
</span>
35+
);
36+
}

0 commit comments

Comments
 (0)