budibase/packages/server/src/api/controllers/static/index.js

196 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-11-09 16:24:29 +01:00
require("svelte/register")
2020-05-07 11:53:34 +02:00
const send = require("koa-send")
const { resolve, join } = require("../../../utilities/centralPath")
2020-09-16 13:18:47 +02:00
const uuid = require("uuid")
const { ObjectStoreBuckets } = require("../../../constants")
const { processString } = require("@budibase/string-templates")
const {
loadHandlebarsFile,
NODE_MODULES_PATH,
2021-04-01 17:36:27 +02:00
TOP_LEVEL_PATH,
} = require("../../../utilities/fileSystem")
const env = require("../../../environment")
const { clientLibraryPath } = require("../../../utilities")
2021-05-11 18:53:54 +02:00
const { upload } = require("../../../utilities/fileSystem")
const { attachmentsRelativeURL } = require("../../../utilities")
const { DocumentTypes, isDevAppID } = require("../../../db/utils")
2022-03-23 17:45:06 +01:00
const { getAppDB, getAppId } = require("@budibase/backend-core/context")
2022-06-03 13:50:38 +02:00
const { setCookie, clearCookie } = require("@budibase/backend-core/utils")
const AWS = require("aws-sdk")
const { events } = require("@budibase/backend-core")
2021-05-11 18:53:54 +02:00
const fs = require("fs")
const {
downloadTarballDirect,
} = require("../../../utilities/fileSystem/utilities")
2021-05-11 18:53:54 +02:00
async function prepareUpload({ s3Key, bucket, metadata, file }) {
const response = await upload({
bucket,
metadata,
filename: s3Key,
path: file.path,
type: file.type,
})
// don't store a URL, work this out on the way out as the URL could change
return {
size: file.size,
name: file.name,
url: attachmentsRelativeURL(response.Key),
extension: [...file.name.split(".")].pop(),
key: response.Key,
}
}
2020-09-17 17:36:39 +02:00
2022-06-03 13:50:38 +02:00
exports.toggleBetaUiFeature = async function (ctx) {
const cookieName = `beta:${ctx.params.feature}`
2022-06-01 14:03:59 +02:00
2022-06-03 13:50:38 +02:00
if (ctx.cookies.get(cookieName)) {
clearCookie(ctx, cookieName)
ctx.body = {
message: `${ctx.params.feature} disabled`,
}
return
2022-06-01 14:03:59 +02:00
}
let builderPath = resolve(TOP_LEVEL_PATH, "new_design_ui")
// // download it from S3
if (!fs.existsSync(builderPath)) {
fs.mkdirSync(builderPath)
}
await downloadTarballDirect(
"https://cdn.budi.live/beta:design_ui/new_ui.tar.gz",
builderPath
)
2022-06-03 13:50:38 +02:00
setCookie(ctx, {}, cookieName)
ctx.body = {
message: `${ctx.params.feature} enabled`,
}
}
exports.serveBuilder = async function (ctx) {
// Temporary: New Design UI
const designUiCookie = ctx.cookies.get("beta:design_ui")
// TODO: get this from the tmp Dir that we downloaded from MinIO
const uiPath = designUiCookie ? "new_design_ui" : "builder"
2022-06-03 13:50:38 +02:00
let builderPath = resolve(TOP_LEVEL_PATH, uiPath)
await send(ctx, ctx.file, { root: builderPath })
2022-06-15 13:38:10 +02:00
if (!ctx.file.includes("assets/")) {
await events.serve.servedBuilder()
}
}
2021-05-03 09:31:09 +02:00
exports.uploadFile = async function (ctx) {
let files =
2020-09-23 17:15:09 +02:00
ctx.request.files.file.length > 1
? Array.from(ctx.request.files.file)
: [ctx.request.files.file]
2021-05-04 12:32:22 +02:00
const uploads = files.map(async file => {
2020-09-23 18:02:06 +02:00
const fileExtension = [...file.name.split(".")].pop()
// filenames converted to UUIDs so they are unique
2020-09-23 17:15:09 +02:00
const processedFileName = `${uuid.v4()}.${fileExtension}`
return prepareUpload({
file,
2021-06-10 12:34:37 +02:00
s3Key: `${ctx.appId}/attachments/${processedFileName}`,
bucket: ObjectStoreBuckets.APPS,
})
})
ctx.body = await Promise.all(uploads)
2020-09-23 18:02:06 +02:00
}
2021-05-03 09:31:09 +02:00
exports.serveApp = async function (ctx) {
const db = getAppDB({ skip_setup: true })
2021-05-16 22:25:37 +02:00
const appInfo = await db.get(DocumentTypes.APP_METADATA)
2022-03-23 17:45:06 +01:00
let appId = getAppId()
2020-11-09 16:24:29 +01:00
if (!env.isJest()) {
const App = require("./templates/BudibaseApp.svelte").default
const { head, html, css } = App.render({
title: appInfo.name,
production: env.isProd(),
appId,
2022-06-28 13:16:23 +02:00
clientLibPath: clientLibraryPath(appId, appInfo.version, ctx),
})
2020-11-09 16:24:29 +01:00
const appHbs = loadHandlebarsFile(`${__dirname}/templates/app.hbs`)
ctx.body = await processString(appHbs, {
head,
body: html,
style: css.code,
appId,
})
} else {
// just return the app info for jest to assert on
ctx.body = appInfo
}
if (isDevAppID(appInfo.appId)) {
2022-05-23 23:14:44 +02:00
await events.serve.servedAppPreview(appInfo)
} else {
2022-05-23 23:14:44 +02:00
await events.serve.servedApp(appInfo)
}
2020-11-09 16:24:29 +01:00
}
2021-05-03 09:31:09 +02:00
exports.serveClientLibrary = async function (ctx) {
return send(ctx, "budibase-client.js", {
2021-04-01 17:36:27 +02:00
root: join(NODE_MODULES_PATH, "@budibase", "client", "dist"),
})
}
exports.getSignedUploadURL = async function (ctx) {
const database = getAppDB()
// Ensure datasource is valid
let datasource
try {
const { datasourceId } = ctx.params
datasource = await database.get(datasourceId)
if (!datasource) {
ctx.throw(400, "The specified datasource could not be found")
}
} catch (error) {
ctx.throw(400, "The specified datasource could not be found")
}
// Ensure we aren't using a custom endpoint
if (datasource?.config?.endpoint) {
ctx.throw(400, "S3 datasources with custom endpoints are not supported")
}
// Determine type of datasource and generate signed URL
let signedUrl
let publicUrl
2022-06-15 17:29:11 +02:00
const awsRegion = datasource?.config?.region || "eu-west-1"
if (datasource.source === "S3") {
const { bucket, key } = ctx.request.body || {}
if (!bucket || !key) {
ctx.throw(400, "bucket and key values are required")
return
}
try {
const s3 = new AWS.S3({
2022-06-15 17:27:07 +02:00
region: awsRegion,
accessKeyId: datasource?.config?.accessKeyId,
secretAccessKey: datasource?.config?.secretAccessKey,
apiVersion: "2006-03-01",
signatureVersion: "v4",
})
const params = { Bucket: bucket, Key: key }
signedUrl = s3.getSignedUrl("putObject", params)
2022-06-15 17:27:07 +02:00
publicUrl = `https://${bucket}.s3.${awsRegion}.amazonaws.com/${key}`
} catch (error) {
ctx.throw(400, error)
}
}
ctx.body = { signedUrl, publicUrl }
}