Skip to content

Commit

Permalink
Add a filename to the presigned url... makes it so when you click the…
Browse files Browse the repository at this point in the history
… indidvidual files, if they cant be previewed, they download with the correct name
  • Loading branch information
mdial89f committed Oct 25, 2023
1 parent ef139a5 commit f00892b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/services/api/handlers/getAttachmentUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ export const handler = async (event: APIGatewayEvent) => {
}

// Now we can generate the presigned url
const url = await generateLegacyPresignedS3Url(body.bucket, body.key, 60);
const url = await generatePresignedUrl(
body.bucket,
body.key,
body.filename,
60
);

return response<unknown>({
statusCode: 200,
Expand Down Expand Up @@ -112,14 +117,20 @@ async function getClient(bucket) {
}
}

async function generateLegacyPresignedS3Url(bucket, key, expirationInSeconds) {
async function generatePresignedUrl(
bucket,
key,
filename,
expirationInSeconds
) {
// Get an S3 client
const client = await getClient(bucket);

// Create a command to get the object (you can adjust this according to your use case)
const getObjectCommand = new GetObjectCommand({
Bucket: bucket,
Key: key,
ResponseContentDisposition: `filename ="${filename}"`,
});

// Generate a presigned URL
Expand Down
4 changes: 3 additions & 1 deletion src/services/ui/src/api/getAttachmentUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { API } from "aws-amplify";
export const getAttachmentUrl = async (
id: string,
bucket: string,
key: string
key: string,
filename: string
) => {
const response = await API.post("os", "/getAttachmentUrl", {
body: {
id,
bucket,
key,
filename,
},
});
return response.url as string;
Expand Down
6 changes: 4 additions & 2 deletions src/services/ui/src/components/AttachmentsList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const handleDownloadAll = async (data: AttachmentList) => {
const url = await getAttachmentUrl(
data.id,
attachment.bucket,
attachment.key
attachment.key,
attachment.filename
);
return { ...attachment, url };
});
Expand Down Expand Up @@ -84,7 +85,8 @@ export const Attachmentslist = (data: AttachmentList) => {
const url = await getAttachmentUrl(
data.id,
attachment.bucket,
attachment.key
attachment.key,
attachment.filename
);
console.log(url);
window.open(url);
Expand Down

0 comments on commit f00892b

Please sign in to comment.