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-03-16 14:51:50 +01:00
|
|
|
const AWS = require("aws-sdk")
|
|
|
|
const { prepareUpload } = require("../deploy/utils")
|
2021-01-20 14:32:15 +01:00
|
|
|
const { processString } = require("@budibase/string-templates")
|
2020-09-17 17:36:39 +02:00
|
|
|
const {
|
|
|
|
budibaseAppsDir,
|
|
|
|
budibaseTempDir,
|
2020-11-12 11:25:25 +01:00
|
|
|
} = require("../../../utilities/budibaseDir")
|
2021-01-14 18:01:31 +01:00
|
|
|
const { getDeployedApps } = require("../../../utilities/builder/hosting")
|
2020-11-12 11:25:25 +01:00
|
|
|
const CouchDB = require("../../../db")
|
|
|
|
const setBuilderToken = require("../../../utilities/builder/setBuilderToken")
|
2021-03-19 20:07:47 +01:00
|
|
|
const { loadHandlebarsFile } = require("../../../utilities/fileSystem")
|
2020-11-12 11:25:25 +01:00
|
|
|
const env = require("../../../environment")
|
2021-02-01 19:08:06 +01:00
|
|
|
const { OBJ_STORE_DIRECTORY } = require("../../../constants")
|
2020-09-17 17:36:39 +02:00
|
|
|
|
2020-12-14 19:31:48 +01:00
|
|
|
function objectStoreUrl() {
|
|
|
|
if (env.SELF_HOSTED) {
|
2021-01-07 16:37:41 +01:00
|
|
|
// can use a relative url for this as all goes through the proxy (this is hosted in minio)
|
2021-02-01 19:08:06 +01:00
|
|
|
return OBJ_STORE_DIRECTORY
|
2020-12-14 19:31:48 +01:00
|
|
|
} else {
|
|
|
|
return "https://cdn.app.budi.live/assets"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-14 18:01:31 +01:00
|
|
|
const apps = await getDeployedApps()
|
|
|
|
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"
|
|
|
|
|
2020-04-14 16:14:57 +02:00
|
|
|
exports.serveBuilder = async function(ctx) {
|
2020-11-12 11:25:25 +01:00
|
|
|
let builderPath = resolve(__dirname, "../../../../builder")
|
2020-06-19 17:59:46 +02:00
|
|
|
if (ctx.file === "index.html") {
|
2020-11-02 16:46:08 +01:00
|
|
|
await setBuilderToken(ctx)
|
2020-06-19 17:59:46 +02:00
|
|
|
}
|
2020-05-06 13:17:15 +02:00
|
|
|
await send(ctx, ctx.file, { root: ctx.devPath || builderPath })
|
2020-04-14 16:14:57 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 17:15:09 +02:00
|
|
|
exports.uploadFile = async function(ctx) {
|
|
|
|
let files
|
|
|
|
files =
|
|
|
|
ctx.request.files.file.length > 1
|
|
|
|
? Array.from(ctx.request.files.file)
|
|
|
|
: [ctx.request.files.file]
|
|
|
|
|
2021-03-19 20:07:47 +01:00
|
|
|
const s3 = new AWS.S3({
|
|
|
|
params: {
|
|
|
|
Bucket: "prod-budi-app-assets",
|
|
|
|
},
|
2020-09-23 18:29:32 +02:00
|
|
|
})
|
2020-09-15 17:22:13 +02:00
|
|
|
|
2021-03-19 20:07:47 +01:00
|
|
|
const uploads = files.map(file => {
|
2020-09-23 18:02:06 +02:00
|
|
|
const fileExtension = [...file.name.split(".")].pop()
|
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,
|
|
|
|
s3Key: `assets/${ctx.user.appId}/attachments/${processedFileName}`,
|
|
|
|
s3,
|
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
|
|
|
}
|
|
|
|
|
2020-11-12 11:41:49 +01: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,
|
|
|
|
production: env.CLOUD,
|
2021-01-14 18:01:31 +01:00
|
|
|
appId,
|
2021-01-07 16:37:41 +01:00
|
|
|
objectStoreUrl: objectStoreUrl(),
|
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
|
|
|
}
|
|
|
|
|
2020-09-15 17:22:13 +02:00
|
|
|
exports.serveAttachment = async function(ctx) {
|
2020-09-17 17:36:39 +02:00
|
|
|
const appId = ctx.user.appId
|
2020-09-15 17:22:13 +02:00
|
|
|
const attachmentsPath = resolve(budibaseAppsDir(), appId, "attachments")
|
|
|
|
|
2021-01-14 18:01:31 +01:00
|
|
|
// Serve from object store
|
2020-10-28 21:35:06 +01:00
|
|
|
if (env.CLOUD) {
|
2020-12-14 19:31:48 +01:00
|
|
|
const S3_URL = join(objectStoreUrl(), appId, "attachments", ctx.file)
|
2020-09-15 17:22:13 +02:00
|
|
|
const response = await fetch(S3_URL)
|
|
|
|
const body = await response.text()
|
2020-10-21 14:00:40 +02:00
|
|
|
ctx.set("Content-Type", response.headers.get("Content-Type"))
|
2020-09-15 17:22:13 +02:00
|
|
|
ctx.body = body
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
await send(ctx, ctx.file, { root: attachmentsPath })
|
|
|
|
}
|
|
|
|
|
2020-06-12 21:42:55 +02:00
|
|
|
exports.serveAppAsset = async function(ctx) {
|
|
|
|
// default to homedir
|
2020-12-02 18:42:59 +01:00
|
|
|
const appPath = resolve(budibaseAppsDir(), ctx.user.appId, "public")
|
2020-05-03 12:33:20 +02:00
|
|
|
|
2020-07-06 20:43:40 +02:00
|
|
|
await send(ctx, ctx.file, { root: ctx.devPath || appPath })
|
2020-04-14 16:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.serveComponentLibrary = async function(ctx) {
|
2020-11-06 22:13:21 +01:00
|
|
|
const appId = ctx.query.appId || ctx.appId
|
2020-05-06 13:17:15 +02:00
|
|
|
// default to homedir
|
|
|
|
let componentLibraryPath = resolve(
|
2020-05-11 16:42:42 +02:00
|
|
|
budibaseAppsDir(),
|
2020-11-06 22:13:21 +01:00
|
|
|
appId,
|
2020-05-07 11:53:34 +02:00
|
|
|
"node_modules",
|
2020-05-02 16:29:10 +02:00
|
|
|
decodeURI(ctx.query.library),
|
2020-07-14 22:07:53 +02:00
|
|
|
"package",
|
2020-05-02 16:29:10 +02:00
|
|
|
"dist"
|
2020-05-07 11:53:34 +02:00
|
|
|
)
|
2020-05-02 16:29:10 +02:00
|
|
|
|
2020-05-06 13:17:15 +02:00
|
|
|
if (ctx.isDev) {
|
|
|
|
componentLibraryPath = join(
|
2020-05-11 16:42:42 +02:00
|
|
|
budibaseTempDir(),
|
2020-05-06 11:33:30 +02:00
|
|
|
decodeURI(ctx.query.library),
|
|
|
|
"dist"
|
2020-05-07 11:53:34 +02:00
|
|
|
)
|
2020-05-06 11:33:30 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 21:35:06 +01:00
|
|
|
if (env.CLOUD) {
|
2020-10-21 16:28:30 +02:00
|
|
|
let componentLib = "componentlibrary"
|
|
|
|
if (ctx.user.version) {
|
|
|
|
componentLib += `-${ctx.user.version}`
|
|
|
|
} else {
|
|
|
|
componentLib += `-${COMP_LIB_BASE_APP_VERSION}`
|
|
|
|
}
|
2020-07-07 22:29:20 +02:00
|
|
|
const S3_URL = encodeURI(
|
2020-12-14 19:31:48 +01:00
|
|
|
join(
|
2020-12-18 13:54:20 +01:00
|
|
|
objectStoreUrl(appId),
|
2020-12-14 19:31:48 +01:00
|
|
|
componentLib,
|
|
|
|
ctx.query.library,
|
|
|
|
"dist",
|
|
|
|
"index.js"
|
|
|
|
)
|
2020-07-07 22:29:20 +02:00
|
|
|
)
|
2020-07-06 20:43:40 +02:00
|
|
|
const response = await fetch(S3_URL)
|
|
|
|
const body = await response.text()
|
2020-07-07 22:29:20 +02:00
|
|
|
ctx.type = "application/javascript"
|
|
|
|
ctx.body = body
|
2020-07-06 20:43:40 +02:00
|
|
|
return
|
|
|
|
}
|
2020-06-29 11:27:38 +02:00
|
|
|
|
2020-12-01 14:39:34 +01:00
|
|
|
await send(ctx, "/awsDeploy.js", { root: componentLibraryPath })
|
2020-05-06 11:33:30 +02:00
|
|
|
}
|