From 93eeca124b0baa75610458d574a3819e3c5a8eaf Mon Sep 17 00:00:00 2001 From: Karen Shaw Date: Thu, 5 Oct 2023 19:34:57 +0000 Subject: [PATCH] wip --- lambdas/assets/email.html | 184 ++++++++++++++++++ lambdas/assets/email.txt | 4 + .../get-download-link.js | 2 +- {lambda => lambdas}/package-lock.json | 0 {lambda => lambdas}/package.json | 0 lambdas/send-email.js | 68 +++++++ .../start-transcode.js | 0 .../transcode-status.js | 0 src/handlers/get-file-set-download.js | 6 +- state_machines/av_download.json | 32 ++- template.yaml | 34 ++-- 11 files changed, 303 insertions(+), 27 deletions(-) create mode 100644 lambdas/assets/email.html create mode 100644 lambdas/assets/email.txt rename lambda/presigned-url.js => lambdas/get-download-link.js (95%) rename {lambda => lambdas}/package-lock.json (100%) rename {lambda => lambdas}/package.json (100%) create mode 100644 lambdas/send-email.js rename lambda/media-transcode.js => lambdas/start-transcode.js (100%) rename lambda/check-transcode-job.js => lambdas/transcode-status.js (100%) diff --git a/lambdas/assets/email.html b/lambdas/assets/email.html new file mode 100644 index 00000000..3cac47b2 --- /dev/null +++ b/lambdas/assets/email.html @@ -0,0 +1,184 @@ + + + + + NUL Meadow Download + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + +
+

+ Northwestern University Logo +

+
+
+ + + + + + +
+

+ Hello, +

+

+ Your request for a zipped archive of the work {{title}} (accession: {{accession}}) has been + fulfilled. Click below to download your zip file: +

+

+ + + {{downloadKey}} + +

+

+ (Your download will be available for 24 hours) +

+
+
+ + + + + + + + + +
+

+ Northwestern University Library +

+
+

+ VISIT MEADOW NOW +

+
+
+ + + diff --git a/lambdas/assets/email.txt b/lambdas/assets/email.txt new file mode 100644 index 00000000..60f77c98 --- /dev/null +++ b/lambdas/assets/email.txt @@ -0,0 +1,4 @@ +Hello, +A zipped archive of {{title}} with accession: {{accession}} has been created. Your download will be available for 24 hours. + +The zip file can be downloaded from {{downloadKey}} diff --git a/lambda/presigned-url.js b/lambdas/get-download-link.js similarity index 95% rename from lambda/presigned-url.js rename to lambdas/get-download-link.js index be4b093f..34797f5b 100644 --- a/lambda/presigned-url.js +++ b/lambdas/get-download-link.js @@ -19,7 +19,7 @@ module.exports.handler = async (event) => { console.log("url", url) - return {presignedUrl: url} + return {downloadLink: url} }; diff --git a/lambda/package-lock.json b/lambdas/package-lock.json similarity index 100% rename from lambda/package-lock.json rename to lambdas/package-lock.json diff --git a/lambda/package.json b/lambdas/package.json similarity index 100% rename from lambda/package.json rename to lambdas/package.json diff --git a/lambdas/send-email.js b/lambdas/send-email.js new file mode 100644 index 00000000..da981299 --- /dev/null +++ b/lambdas/send-email.js @@ -0,0 +1,68 @@ +const { SESClient, SendTemplatedEmailCommand } = require("@aws-sdk/client-ses"); + +module.exports.handler = async (event) => { + console.log("SEND EMAIL LAMBDA"); + console.log(event); + + const sesClient = new SESClient(); + const templateName = event.template; + const toAddress = event.to || "karen.shaw@northwestern.edu"; + const senderAddress = event.sender; + const downloadLink = event.downloadLink; + const today = new Date().toLocaleDateString("en-us", { + weekday: "long", + year: "numeric", + month: "short", + day: "numeric", + }); + const params = { + email: toAddress, + downloadKey: downloadLink, + referer: "Meadow", + workId: "foo", + today, + title: "[Untitled]", + accession: "1234", + }; + + const sendTemplatedEmailCommand = createSendTemplatedEmailCommand( + toAddress, + senderAddress, + templateName, + params + ); + + try { + const result = await sesClient.send(sendTemplatedEmailCommand); + console.log("sendTemplatedEmailCommand", result) + } catch (err) { + console.log("Failed to send template email", err); + return err; + } + return {success: true} +}; + +/** + * + * @param { { emailAddress: string, firstName: string } } user + * @param { string } templateName - The name of an existing template in Amazon SES. + * @returns { SendTemplatedEmailCommand } + */ +const createSendTemplatedEmailCommand = ( + toAddress, + senderAddress, + templateName, + params +) => { + return new SendTemplatedEmailCommand({ + /** + * Here's an example of how a template would be replaced with user data: + * Template:

Hello {{contact.firstName}},

Don't forget about the party gifts!

+ * Destination:

Hello Bilbo,

Don't forget about the party gifts!

+ */ + Destination: { ToAddresses: [toAddress] }, + TemplateData: JSON.stringify(params), + Source: `Northwestern University Libraries <${senderAddress}>`, + Template: templateName, + }); +}; diff --git a/lambda/media-transcode.js b/lambdas/start-transcode.js similarity index 100% rename from lambda/media-transcode.js rename to lambdas/start-transcode.js diff --git a/lambda/check-transcode-job.js b/lambdas/transcode-status.js similarity index 100% rename from lambda/check-transcode-job.js rename to lambdas/transcode-status.js diff --git a/src/handlers/get-file-set-download.js b/src/handlers/get-file-set-download.js index fba8c76e..7c6591c5 100644 --- a/src/handlers/get-file-set-download.js +++ b/src/handlers/get-file-set-download.js @@ -77,15 +77,17 @@ function processDownload(streaming_url, email) { "arn:aws:states:us-east-1:123456789012:stateMachine:hlsStitcherStepFunction", input: JSON.stringify({ transcodeInput: { - //source:s3Location(streaming_url) settings: settings, }, presignedUrlInput: { bucket: destinationBucket, key: destinationKey, }, - sendMailInput: { + sendEmailInput: { to: email, + downloadLink: "", + template: "work-archiver-template", + sender: process.env.REPOSITORY_EMAIL }, }), }; diff --git a/state_machines/av_download.json b/state_machines/av_download.json index 25bba21d..cf2ab00d 100644 --- a/state_machines/av_download.json +++ b/state_machines/av_download.json @@ -1,17 +1,17 @@ { "Comment": "HLS stiching and save as file in s3 and email download link", - "StartAt": "createTranscodeJob", + "StartAt": "startTranscode", "States": { - "createTranscodeJob": { + "startTranscode": { "Type": "Task", - "Resource": "arn:aws:lambda:us-east-1:123456789012:function:mediaTranscodeFunction", - "Next": "checkTranscodeJobStatus", + "Resource": "arn:aws:lambda:us-east-1:123456789012:function:startTranscodeFunction", + "Next": "transcodeStatus", "InputPath": "$.transcodeInput", "ResultPath": "$.transcodeOutput" }, - "checkTranscodeJobStatus": { + "transcodeStatus": { "Type": "Task", - "Resource": "arn:aws:lambda:us-east-1:123456789012:function:transcodeCompleteFunction", + "Resource": "arn:aws:lambda:us-east-1:123456789012:function:transcodeStatusFunction", "InputPath": "$.transcodeOutput", "ResultPath": "$.transcodeOutput", "Next": "transcodeCompleted?" @@ -22,7 +22,7 @@ { "Variable": "$.transcodeOutput.status", "StringEquals": "COMPLETE", - "Next": "generateDownloadLink" + "Next": "getDownloadLink" } ], "Default": "Wait 10 seconds" @@ -30,12 +30,24 @@ "Wait 10 seconds": { "Type": "Wait", "Seconds": 10, - "Next": "checkTranscodeJobStatus" + "Next": "transcodeStatus" }, - "generateDownloadLink": { + "getDownloadLink": { "Type": "Task", - "Resource": "arn:aws:lambda:us-east-1:123456789012:function:generateDownloadLinkFunction", + "Resource": "arn:aws:lambda:us-east-1:123456789012:function:getDownloadLinkFunction", "InputPath": "$.presignedUrlInput", + "ResultPath": "$.downloadLinkOutput", + "Next": "sendEmail" + }, + "sendEmail": { + "Type": "Task", + "Resource": "arn:aws:lambda:us-east-1:123456789012:function:sendEmailFunction", + "Parameters": { + "to.$": "$.sendEmailInput.to", + "downloadLink.$": "$.downloadLinkOutput.downloadLink", + "sender.$": "$.sendEmailInput.sender", + "template.$": "$.sendEmailInput.template" + }, "End": true } } diff --git a/template.yaml b/template.yaml index 5bad84a5..26e32f62 100644 --- a/template.yaml +++ b/template.yaml @@ -94,6 +94,9 @@ Parameters: ReadingRoomIPs: Type: String Description: Comma-delimited list of IP addresses to serve private resources to + RepositoryEmail: + Type: String + Description: Verified email address to use as sender StreamingBucket: Type: String Description: Meadow streaming bucket @@ -289,6 +292,7 @@ Resources: MEDIA_CONVERT_ENDPOINT: !Ref MediaConvertEndpoint MEDIA_CONVERT_JOB_QUEUE_ARN: !Ref MediaConvertJobQueueArn MEDIA_CONVERT_ROLE_ARN: !Ref MediaConvertRoleArn + REPOSITORY_EMAIL: !Ref RepositoryEmail Policies: Version: 2012-10-17 Statement: @@ -583,40 +587,42 @@ Resources: ApiId: !Ref dcApi Path: $default Method: ANY - mediaTranscodeFunction: + startTranscodeFunction: Type: AWS::Serverless::Function Properties: Runtime: nodejs18.x - CodeUri: ./lambda - Handler: media-transcode.handler + CodeUri: ./lambdas + Handler: start-transcode.handler Description: Creates MediaConvert Job to transcode HLS stream Environment: Variables: - AV_DOWNLOAD_STATE_MACHINE_ARN: !Ref AvDownloadStateMachineArn - MEDIA_CONVERT_DESTINATION_BUCKET: !Ref MediaConvertDestinationBucket MEDIA_CONVERT_ENDPOINT: !Ref MediaConvertEndpoint MEDIA_CONVERT_JOB_QUEUE_ARN: !Ref MediaConvertJobQueueArn MEDIA_CONVERT_ROLE_ARN: !Ref MediaConvertRoleArn - transcodeCompleteFunction: + transcodeStatusFunction: Type: AWS::Serverless::Function Properties: Runtime: nodejs18.x - CodeUri: ./lambda - Handler: check-transcode-job.handler + CodeUri: ./lambdas + Handler: transcode-status.handler Description: Determines when transcode job has completed or errored Environment: Variables: MEDIA_CONVERT_ENDPOINT: !Ref MediaConvertEndpoint - generateDownloadLinkFunction: + getDownloadLinkFunction: Type: AWS::Serverless::Function Properties: Runtime: nodejs18.x - CodeUri: ./lambda - Handler: presigned-url.handler + CodeUri: ./lambdas + Handler: get-download-link.handler Description: Creates presigned url - Environment: - Variables: - MEDIA_CONVERT_ENDPOINT: !Ref MediaConvertEndpoint + sendEmailFunction: + Type: AWS::Serverless::Function + Properties: + Runtime: nodejs18.x + CodeUri: ./lambdas + Handler: send-email.handler + Description: Sends email dcApi: Type: AWS::Serverless::HttpApi Properties: