Skip to content

Commit

Permalink
allow files to display on frontend (#150)
Browse files Browse the repository at this point in the history
Co-authored-by: Anson He <[email protected]>
  • Loading branch information
victorzheng02 and ansonjwhe authored Jul 30, 2023
1 parent 9bd8305 commit 4474496
Show file tree
Hide file tree
Showing 22 changed files with 1,158 additions and 49 deletions.
23 changes: 22 additions & 1 deletion backend/aws/s3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
const { S3Client, DeleteObjectCommand } = require('@aws-sdk/client-s3')
const {
S3Client,
DeleteObjectCommand,
GetObjectCommand,
} = require('@aws-sdk/client-s3')
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner')

const { HttpRequest } = require('@aws-sdk/protocol-http')
const { formatUrl } = require('@aws-sdk/util-format-url')

const { S3RequestPresigner } = require('@aws-sdk/s3-request-presigner')
const { Hash } = require('@aws-sdk/hash-node')

const s3Client = new S3Client({
region: 'us-east-2',
})
Expand All @@ -16,8 +28,17 @@ const deleteS3File = (bucket, key) => {
return s3Client.send(deleteCommand)
}

const generatePresignedUrl = async (bucket, key) => {
const command = new GetObjectCommand({
Bucket: bucket,
Key: key,
})
return getSignedUrl(s3Client, command, { expiresIn: 60 * 60 })
}

module.exports = {
s3Client,
getS3FileKey,
deleteS3File,
generatePresignedUrl,
}
105 changes: 105 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.370.0",
"@aws-sdk/lib-storage": "^3.370.0",
"@aws-sdk/s3-request-presigner": "^3.374.0",
"aws-sdk": "^2.1416.0",
"axios": "^1.2.3",
"cors": "^2.8.5",
Expand Down
21 changes: 19 additions & 2 deletions backend/service/files.service.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
const File = require('../models/file.model')
const { getS3FileKey, deleteS3File } = require('../aws/s3')
const {
getS3FileKey,
deleteS3File,
generatePresignedUrl,
} = require('../aws/s3')

const getFile = async (id) => {
return File.findById(id)
}

const getAllFilesByReference = async (reference) => {
return File.find({ reference_code: reference })
const files = await File.find({ reference_code: reference })
return Promise.all(
files.map(async (file) => {
// generate a presigned url for each file
const presignedUrl = await generatePresignedUrl(
process.env.AWS_FINANCE_BUCKET_NAME,
getS3FileKey(file.reference_code, file.name)
)
return {
...file.toObject(),
link: presignedUrl,
}
})
)
}

const createFile = async (file, referenceCode, isSupportingDocument) => {
Expand Down
Loading

0 comments on commit 4474496

Please sign in to comment.