-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
441 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const LAMBDA_ENDPOINT = '/api/generate-receipt'; | ||
export const LAMBDA_PRESIGN_UPLOAD_ENDPOINT = '/api/presign-upload-url'; | ||
|
||
export const OW4_ADDRESS = process.env.NEXT_PUBLIC_OW4_ADDRESS || 'https://online.ntnu.no'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { NextApiRequest, NextApiResponse, PageConfig } from 'next'; | ||
|
||
import { SuccessBody } from 'lambda/handler'; | ||
import { sentryMiddleware } from 'lambda/sentry'; | ||
import { ErrorData } from 'lambda/errors'; | ||
import { getPresignedS3URL } from "../../utils/getPresignedS3URL"; | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse<SuccessBody | ErrorData>) => { | ||
const { filename, contentType } = req.body; | ||
|
||
try { | ||
const data = await getPresignedS3URL(filename, contentType); | ||
res.status(200).json({ message: "Presigned URL", data: JSON.stringify(data) }); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to get presigned URL", data: error }); | ||
} | ||
}; | ||
|
||
export const config: PageConfig = { | ||
api: { | ||
bodyParser: { | ||
sizeLimit: '25mb', | ||
}, | ||
}, | ||
}; | ||
|
||
export default sentryMiddleware(handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
AWS.config.update({ | ||
region: process.env.NEXT_AWS_REGION, | ||
credentials: { | ||
accessKeyId: process.env.NEXT_AWS_ACCESS_KEY_ID as string, | ||
secretAccessKey: process.env.NEXT_AWS_SECRET_ACCESS_KEY as string, | ||
}, | ||
}) | ||
|
||
export async function downloadFileFromS3Bucket(key: string): Promise<File> { | ||
const s3 = new AWS.S3({ | ||
apiVersion: '2006-03-01', | ||
}); | ||
|
||
const params = { | ||
Bucket: process.env.NEXT_AWS_S3_BUCKET_NAME as string, | ||
Key: key, | ||
}; | ||
|
||
const data = await s3.getObject(params).promise(); | ||
|
||
return new File([data.Body as Blob], key, { type: data.ContentType }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
if (process.env.NEXT_AWS_ACCESS_KEY_ID === undefined) { | ||
throw new Error('NEXT_AWS_ACCESS_KEY_ID is not defined') | ||
} | ||
|
||
if (process.env.NEXT_AWS_SECRET_ACCESS_KEY === undefined) { | ||
throw new Error('NEXT_AWS_SECRET_ACCESS_KEY is not defined') | ||
} | ||
|
||
AWS.config.update({ | ||
region: process.env.NEXT_AWS_REGION, | ||
credentials: { | ||
accessKeyId: process.env.NEXT_AWS_ACCESS_KEY_ID as string, | ||
secretAccessKey: process.env.NEXT_AWS_SECRET_ACCESS_KEY as string, | ||
}, | ||
}) | ||
|
||
export const getPresignedS3URL = async (name: string, contentType: string) => { | ||
const s3 = new AWS.S3({ | ||
apiVersion: '2006-03-01', | ||
params: { Bucket: process.env.NEXT_AWS_S3_BUCKET_NAME }, | ||
}) | ||
|
||
const params = { | ||
Bucket: process.env.NEXT_AWS_S3_BUCKET_NAME, | ||
Key: `uploads/${+new Date()}-${name}`, | ||
ContentType: contentType, | ||
} | ||
|
||
return { url: await s3.getSignedUrlPromise('putObject', params), key: params.Key } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { LAMBDA_PRESIGN_UPLOAD_ENDPOINT } from "constants/backend"; | ||
import { SuccessBody } from 'lambda/handler'; | ||
|
||
export const uploadFile = async (file: File): Promise<string> => { | ||
if (!LAMBDA_PRESIGN_UPLOAD_ENDPOINT) { | ||
throw new Error('LAMBDA_PRESIGN_UPLOAD_ENDPOINT is not set'); | ||
} | ||
const response = await fetch(LAMBDA_PRESIGN_UPLOAD_ENDPOINT, { | ||
body: JSON.stringify({ filename: file.name, contentType: file.type }), | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
const body = await response.json(); | ||
|
||
if (!response.ok) { | ||
throw new Error('Failed to get presigned URL'); | ||
} | ||
|
||
const { data } = body as SuccessBody; | ||
|
||
if (!data) { | ||
throw new Error('Failed to get presigned URL'); | ||
} | ||
|
||
const { url, key } = JSON.parse(data); | ||
|
||
const uploadResponse = await fetch(url, { | ||
body: file, | ||
method: 'PUT', | ||
headers: { | ||
'Content-Type': file.type, | ||
}, | ||
}); | ||
|
||
if (!uploadResponse.ok) { | ||
throw new Error('Failed to upload file'); | ||
} | ||
|
||
return key; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
AWS.config.update({ | ||
region: process.env.NEXT_AWS_REGION, | ||
credentials: { | ||
accessKeyId: process.env.NEXT_AWS_ACCESS_KEY_ID as string, | ||
secretAccessKey: process.env.NEXT_AWS_SECRET_ACCESS_KEY as string, | ||
}, | ||
}) | ||
|
||
// upload file to S3 bucket and make it publicly downloadable | ||
export async function uploadFileToS3Bucket(file: Uint8Array, key: string): Promise<string> { | ||
const s3 = new AWS.S3({ | ||
apiVersion: '2006-03-01', | ||
params: { Bucket: process.env.NEXT_AWS_S3_BUCKET_NAME as string }, | ||
}); | ||
|
||
const params = { | ||
Bucket: process.env.NEXT_AWS_S3_BUCKET_NAME as string, | ||
Key: key, | ||
Body: file, | ||
ACL: 'public-read', | ||
}; | ||
|
||
const result = await s3.upload(params).promise(); | ||
|
||
return result.Location; | ||
} |
Oops, something went wrong.