2020-07-01 22:56:53 +02:00
|
|
|
const fs = require("fs")
|
|
|
|
const AWS = require("aws-sdk")
|
|
|
|
const fetch = require("node-fetch")
|
|
|
|
const {
|
|
|
|
budibaseAppsDir,
|
|
|
|
} = require("../../../utilities/budibaseDir")
|
|
|
|
|
|
|
|
async function fetchTemporaryCredentials() {
|
|
|
|
const CREDENTIALS_URL = "https://dt4mpwwap8.execute-api.eu-west-1.amazonaws.com/prod/"
|
|
|
|
|
2020-07-03 00:22:20 +02:00
|
|
|
const BUDIBASE_API_KEY = process.env.BUDIBASE_API_KEY
|
|
|
|
|
2020-07-01 22:56:53 +02:00
|
|
|
const response = await fetch(CREDENTIALS_URL, {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({
|
2020-07-03 00:22:20 +02:00
|
|
|
apiKey: BUDIBASE_API_KEY
|
2020-07-01 22:56:53 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if (response.status !== 200) {
|
2020-07-03 00:22:20 +02:00
|
|
|
throw new Error(`Error fetching temporary credentials for api key: ${BUDIBASE_API_KEY}`)
|
2020-07-01 22:56:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const json = await response.json()
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2020-07-03 00:22:20 +02:00
|
|
|
const CONTENT_TYPE_MAP = {
|
|
|
|
html: "text/html",
|
|
|
|
css: "text/css",
|
|
|
|
js: "application/javascript"
|
|
|
|
};
|
|
|
|
|
2020-07-01 22:56:53 +02:00
|
|
|
exports.uploadAppAssets = async function ({ appId }) {
|
|
|
|
const { credentials, accountId } = await fetchTemporaryCredentials()
|
|
|
|
|
|
|
|
AWS.config.update({
|
|
|
|
accessKeyId: credentials.AccessKeyId,
|
|
|
|
secretAccessKey: credentials.SecretAccessKey,
|
|
|
|
sessionToken: credentials.SessionToken
|
|
|
|
});
|
|
|
|
|
|
|
|
const s3 = new AWS.S3({
|
|
|
|
params: {
|
|
|
|
Bucket: process.env.BUDIBASE_APP_ASSETS_BUCKET
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const appAssetsPath = `${budibaseAppsDir()}/${appId}/public`
|
|
|
|
|
|
|
|
const appPages = fs.readdirSync(appAssetsPath)
|
|
|
|
|
|
|
|
const uploads = []
|
|
|
|
|
|
|
|
for (let page of appPages) {
|
|
|
|
for (let filename of fs.readdirSync(`${appAssetsPath}/${page}`)) {
|
|
|
|
const filePath = `${appAssetsPath}/${page}/${filename}`
|
|
|
|
const stat = await fs.lstatSync(filePath)
|
|
|
|
|
|
|
|
// TODO: need to account for recursively traversing dirs
|
|
|
|
if (stat.isFile()) {
|
|
|
|
const fileBytes = fs.readFileSync(`${appAssetsPath}/${page}/${filename}`)
|
|
|
|
|
2020-07-03 00:22:20 +02:00
|
|
|
console.log(`${appId}/${page}/${filename}`)
|
|
|
|
|
|
|
|
const fileExtension = [...filename.split(".")].pop()
|
2020-07-01 22:56:53 +02:00
|
|
|
|
|
|
|
const upload = s3.upload({
|
2020-07-03 00:22:20 +02:00
|
|
|
Key: `assets/${appId}/${page}/${filename}`,
|
|
|
|
Body: fileBytes,
|
|
|
|
ContentType: CONTENT_TYPE_MAP[fileExtension],
|
|
|
|
Metadata: {
|
|
|
|
accountId
|
|
|
|
}
|
2020-07-01 22:56:53 +02:00
|
|
|
}).promise()
|
|
|
|
|
|
|
|
uploads.push(upload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-07-03 00:22:20 +02:00
|
|
|
return Promise.all(uploads)
|
2020-07-01 22:56:53 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error("Error uploading budibase app assets to s3", err)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|