Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow files to display on frontend #150

Merged
merged 5 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading