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

120 lines
3.6 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-06-29 11:27:38 +02:00
const fetch = require("node-fetch")
2020-09-16 13:18:47 +02:00
const uuid = require("uuid")
const { ObjectStoreBuckets } = require("../../../constants")
const { prepareUpload } = require("../deploy/utils")
const { processString } = require("@budibase/string-templates")
const { budibaseTempDir } = require("../../../utilities/budibaseDir")
const { getDeployedApps } = require("../../../utilities/builder/hosting")
const CouchDB = require("../../../db")
const {
loadHandlebarsFile,
NODE_MODULES_PATH,
2021-04-01 17:36:27 +02:00
TOP_LEVEL_PATH,
} = require("../../../utilities/fileSystem")
const env = require("../../../environment")
const { objectStoreUrl, clientLibraryPath } = require("../../../utilities")
2020-09-17 17:36:39 +02:00
async function checkForSelfHostedURL(ctx) {
// the "appId" component of the URL may actually be a specific self hosted URL
let possibleAppUrl = `/${encodeURI(ctx.params.appId).toLowerCase()}`
const apps = await getDeployedApps(ctx)
if (apps[possibleAppUrl] && apps[possibleAppUrl].appId) {
return apps[possibleAppUrl].appId
} else {
return ctx.params.appId
}
}
// this was the version before we started versioning the component library
const COMP_LIB_BASE_APP_VERSION = "0.2.5"
2021-05-03 09:31:09 +02:00
exports.serveBuilder = async function (ctx) {
2021-04-01 17:36:27 +02:00
let builderPath = resolve(TOP_LEVEL_PATH, "builder")
await send(ctx, ctx.file, { root: builderPath })
}
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,
s3Key: `assets/${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) {
let appId = ctx.params.appId
if (env.SELF_HOSTED) {
appId = await checkForSelfHostedURL(ctx)
}
2020-11-09 16:24:29 +01:00
const App = require("./templates/BudibaseApp.svelte").default
const db = new CouchDB(appId, { skip_setup: true })
const appInfo = await db.get(appId)
2020-11-09 16:24:29 +01:00
const { head, html, css } = App.render({
title: appInfo.name,
production: env.isProd(),
appId,
clientLibPath: clientLibraryPath(appId),
})
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,
})
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"),
})
}
2021-05-03 09:31:09 +02:00
exports.serveComponentLibrary = async function (ctx) {
const appId = ctx.query.appId || ctx.appId
if (env.isDev() || env.isTest()) {
const componentLibraryPath = join(
2020-05-11 16:42:42 +02:00
budibaseTempDir(),
decodeURI(ctx.query.library),
"dist"
2020-05-07 11:53:34 +02:00
)
return send(ctx, "/awsDeploy.js", { root: componentLibraryPath })
}
const db = new CouchDB(appId)
const appInfo = await db.get(appId)
let componentLib = "componentlibrary"
if (appInfo && appInfo.version) {
componentLib += `-${appInfo.version}`
} else {
componentLib += `-${COMP_LIB_BASE_APP_VERSION}`
2020-07-06 20:43:40 +02:00
}
const S3_URL = encodeURI(
2021-04-13 21:26:26 +02:00
join(objectStoreUrl(), componentLib, ctx.query.library, "dist", "index.js")
)
const response = await fetch(S3_URL)
const body = await response.text()
ctx.type = "application/javascript"
ctx.body = body
}