ensuring s3 upload works in prod

This commit is contained in:
Martin McKeaveney 2020-09-23 20:23:40 +01:00
parent fd97c14a50
commit 31dd25331e
2 changed files with 25 additions and 22 deletions

View File

@ -64,29 +64,23 @@ function walkDir(dirPath, callback) {
} }
} }
async function prepareUploadForS3({ async function prepareUploadForS3({ s3Key, metadata, s3, file }) {
filePath, const extension = [...file.name.split(".")].pop()
s3Key, const fileBytes = fs.readFileSync(file.path)
metadata,
fileType,
s3,
...file
}) {
const contentType =
fileType || CONTENT_TYPE_MAP[[...filePath.split(".")].pop().toLowerCase()]
const fileBytes = fs.readFileSync(filePath)
const upload = await s3 const upload = await s3
.upload({ .upload({
Key: s3Key, Key: s3Key,
Body: fileBytes, Body: fileBytes,
ContentType: contentType, ContentType: file.type || CONTENT_TYPE_MAP[extension.toLowerCase()],
Metadata: metadata, Metadata: metadata,
}) })
.promise() .promise()
return { return {
...file, size: file.size,
name: file.name,
extension,
url: upload.Location, url: upload.Location,
key: upload.Key, key: upload.Key,
} }
@ -124,7 +118,10 @@ exports.uploadAppAssets = async function({
// Upload HTML, CSS and JS for each page of the web app // Upload HTML, CSS and JS for each page of the web app
walkDir(`${appAssetsPath}/${page}`, function(filePath) { walkDir(`${appAssetsPath}/${page}`, function(filePath) {
const appAssetUpload = prepareUploadForS3({ const appAssetUpload = prepareUploadForS3({
filePath, file: {
path: filePath,
name: [...filePath.split("/")].pop(),
},
s3Key: filePath.replace(appAssetsPath, `assets/${appId}`), s3Key: filePath.replace(appAssetsPath, `assets/${appId}`),
s3, s3,
metadata: { accountId }, metadata: { accountId },
@ -141,8 +138,7 @@ exports.uploadAppAssets = async function({
if (file.uploaded) continue if (file.uploaded) continue
const attachmentUpload = prepareUploadForS3({ const attachmentUpload = prepareUploadForS3({
fileType: file.type, file,
filePath: file.path,
s3Key: `assets/${appId}/attachments/${file.processedFileName}`, s3Key: `assets/${appId}/attachments/${file.processedFileName}`,
s3, s3,
metadata: { accountId }, metadata: { accountId },

View File

@ -50,9 +50,7 @@ exports.uploadFile = async function(ctx) {
const processedFileName = `${uuid.v4()}.${fileExtension}` const processedFileName = `${uuid.v4()}.${fileExtension}`
return prepareUploadForS3({ return prepareUploadForS3({
...file, file,
fileType: file.type,
filePath: file.path,
s3Key: `assets/${ctx.user.appId}/attachments/${processedFileName}`, s3Key: `assets/${ctx.user.appId}/attachments/${processedFileName}`,
s3, s3,
}) })
@ -70,6 +68,7 @@ exports.uploadFile = async function(ctx) {
} }
async function processLocalFileUploads({ files, outputPath, instanceId }) { async function processLocalFileUploads({ files, outputPath, instanceId }) {
console.log("files", files)
// create attachments dir if it doesnt exist // create attachments dir if it doesnt exist
!fs.existsSync(outputPath) && fs.mkdirSync(outputPath, { recursive: true }) !fs.existsSync(outputPath) && fs.mkdirSync(outputPath, { recursive: true })
@ -78,12 +77,20 @@ async function processLocalFileUploads({ files, outputPath, instanceId }) {
// filenames converted to UUIDs so they are unique // filenames converted to UUIDs so they are unique
const processedFileName = `${uuid.v4()}.${fileExtension}` const processedFileName = `${uuid.v4()}.${fileExtension}`
console.log(file) // {
// name: 'backspace-solid.svg',
// path: '/Users/martinmckeaveney/Downloads/backspace-solid.svg',
// size: 813,
// type: 'image/svg+xml'
// }
return { return {
...file, name: file.name,
path: file.path,
size: file.size,
type: file.type,
processedFileName, processedFileName,
extension: fileExtension, // extension: fileExtension,
outputPath: join(outputPath, processedFileName), outputPath: join(outputPath, processedFileName),
url: join("/attachments", processedFileName), url: join("/attachments", processedFileName),
} }