2020-11-09 16:24:29 +01:00
|
|
|
require("svelte/register")
|
|
|
|
|
2020-05-07 11:53:34 +02:00
|
|
|
const send = require("koa-send")
|
2020-11-12 11:25:25 +01:00
|
|
|
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")
|
2021-05-07 15:27:46 +02:00
|
|
|
const { ObjectStoreBuckets } = require("../../../constants")
|
2021-03-16 14:51:50 +01:00
|
|
|
const { prepareUpload } = require("../deploy/utils")
|
2021-01-20 14:32:15 +01:00
|
|
|
const { processString } = require("@budibase/string-templates")
|
2021-03-25 19:03:58 +01:00
|
|
|
const { budibaseTempDir } = require("../../../utilities/budibaseDir")
|
2021-05-11 18:49:26 +02:00
|
|
|
const { getDeployedApps } = require("../../../utilities/workerRequests")
|
2020-11-12 11:25:25 +01:00
|
|
|
const CouchDB = require("../../../db")
|
2021-04-01 17:19:31 +02:00
|
|
|
const {
|
|
|
|
loadHandlebarsFile,
|
|
|
|
NODE_MODULES_PATH,
|
2021-04-01 17:36:27 +02:00
|
|
|
TOP_LEVEL_PATH,
|
2021-04-01 17:19:31 +02:00
|
|
|
} = require("../../../utilities/fileSystem")
|
2020-11-12 11:25:25 +01:00
|
|
|
const env = require("../../../environment")
|
2021-04-01 13:48:38 +02:00
|
|
|
const { objectStoreUrl, clientLibraryPath } = require("../../../utilities")
|
2020-09-17 17:36:39 +02:00
|
|
|
|
2021-01-14 18:01:31 +01:00
|
|
|
async function checkForSelfHostedURL(ctx) {
|
|
|
|
// the "appId" component of the URL may actually be a specific self hosted URL
|
2021-01-14 18:31:17 +01:00
|
|
|
let possibleAppUrl = `/${encodeURI(ctx.params.appId).toLowerCase()}`
|
2021-04-09 16:11:49 +02:00
|
|
|
const apps = await getDeployedApps(ctx)
|
2021-01-14 18:01:31 +01:00
|
|
|
if (apps[possibleAppUrl] && apps[possibleAppUrl].appId) {
|
|
|
|
return apps[possibleAppUrl].appId
|
|
|
|
} else {
|
|
|
|
return ctx.params.appId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 16:28:30 +02:00
|
|
|
// 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")
|
2021-03-31 20:55:55 +02:00
|
|
|
await send(ctx, ctx.file, { root: builderPath })
|
2020-04-14 16:14:57 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.uploadFile = async function (ctx) {
|
2021-03-22 18:19:45 +01:00
|
|
|
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()
|
2021-03-22 18:19:45 +01:00
|
|
|
// filenames converted to UUIDs so they are unique
|
2020-09-23 17:15:09 +02:00
|
|
|
const processedFileName = `${uuid.v4()}.${fileExtension}`
|
2020-09-15 17:22:13 +02:00
|
|
|
|
2021-03-19 20:07:47 +01:00
|
|
|
return prepareUpload({
|
|
|
|
file,
|
2021-03-29 18:32:05 +02:00
|
|
|
s3Key: `assets/${ctx.appId}/attachments/${processedFileName}`,
|
2021-05-07 14:55:30 +02:00
|
|
|
bucket: ObjectStoreBuckets.APPS,
|
2020-09-23 18:29:32 +02:00
|
|
|
})
|
2021-03-19 20:07:47 +01:00
|
|
|
})
|
2020-09-23 18:29:32 +02:00
|
|
|
|
2021-03-19 20:07:47 +01:00
|
|
|
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) {
|
2021-01-14 18:01:31 +01:00
|
|
|
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
|
2021-01-14 18:01:31 +01:00
|
|
|
const db = new CouchDB(appId, { skip_setup: true })
|
|
|
|
const appInfo = await db.get(appId)
|
2020-11-09 16:24:29 +01:00
|
|
|
|
2020-11-12 11:25:25 +01:00
|
|
|
const { head, html, css } = App.render({
|
|
|
|
title: appInfo.name,
|
2021-03-24 19:21:23 +01:00
|
|
|
production: env.isProd(),
|
2021-01-14 18:01:31 +01:00
|
|
|
appId,
|
2021-04-01 13:48:38 +02:00
|
|
|
clientLibPath: clientLibraryPath(appId),
|
2020-11-12 11:25:25 +01:00
|
|
|
})
|
2020-11-09 16:24:29 +01:00
|
|
|
|
2021-03-19 20:07:47 +01:00
|
|
|
const appHbs = loadHandlebarsFile(`${__dirname}/templates/app.hbs`)
|
2021-01-20 14:32:15 +01:00
|
|
|
ctx.body = await processString(appHbs, {
|
2020-11-12 11:25:25 +01:00
|
|
|
head,
|
|
|
|
body: html,
|
|
|
|
style: css.code,
|
2021-01-14 18:01:31 +01:00
|
|
|
appId,
|
2020-11-12 11:25:25 +01:00
|
|
|
})
|
2020-11-09 16:24:29 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.serveClientLibrary = async function (ctx) {
|
2021-04-01 13:48:38 +02:00
|
|
|
return send(ctx, "budibase-client.js", {
|
2021-04-01 17:36:27 +02:00
|
|
|
root: join(NODE_MODULES_PATH, "@budibase", "client", "dist"),
|
2021-04-01 13:48:38 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.serveComponentLibrary = async function (ctx) {
|
2020-11-06 22:13:21 +01:00
|
|
|
const appId = ctx.query.appId || ctx.appId
|
2020-05-02 16:29:10 +02:00
|
|
|
|
2021-03-25 19:03:58 +01:00
|
|
|
if (env.isDev() || env.isTest()) {
|
|
|
|
const componentLibraryPath = join(
|
2020-05-11 16:42:42 +02:00
|
|
|
budibaseTempDir(),
|
2021-05-10 19:07:57 +02:00
|
|
|
appId,
|
|
|
|
"node_modules",
|
|
|
|
"@budibase",
|
|
|
|
"standard-components",
|
|
|
|
"package",
|
2020-05-06 11:33:30 +02:00
|
|
|
"dist"
|
2020-05-07 11:53:34 +02:00
|
|
|
)
|
2021-05-10 19:07:57 +02:00
|
|
|
return send(ctx, "/index.js", { root: componentLibraryPath })
|
2021-03-25 19:03:58 +01:00
|
|
|
}
|
2021-04-13 16:27:47 +02:00
|
|
|
const db = new CouchDB(appId)
|
|
|
|
const appInfo = await db.get(appId)
|
|
|
|
|
2021-03-25 19:03:58 +01:00
|
|
|
let componentLib = "componentlibrary"
|
2021-04-13 16:27:47 +02:00
|
|
|
if (appInfo && appInfo.version) {
|
|
|
|
componentLib += `-${appInfo.version}`
|
2021-03-24 19:21:23 +01:00
|
|
|
} else {
|
2021-03-25 19:03:58 +01:00
|
|
|
componentLib += `-${COMP_LIB_BASE_APP_VERSION}`
|
2020-07-06 20:43:40 +02:00
|
|
|
}
|
2021-03-25 19:03:58 +01:00
|
|
|
const S3_URL = encodeURI(
|
2021-04-13 21:26:26 +02:00
|
|
|
join(objectStoreUrl(), componentLib, ctx.query.library, "dist", "index.js")
|
2021-03-25 19:03:58 +01:00
|
|
|
)
|
|
|
|
const response = await fetch(S3_URL)
|
|
|
|
const body = await response.text()
|
|
|
|
ctx.type = "application/javascript"
|
|
|
|
ctx.body = body
|
2020-05-06 11:33:30 +02:00
|
|
|
}
|