2020-10-29 09:35:06 +13:00
|
|
|
const env = require("../environment")
|
2021-08-06 03:57:23 +12:00
|
|
|
const { OBJ_STORE_DIRECTORY } = require("../constants")
|
2021-05-25 08:30:46 +12:00
|
|
|
const { sanitizeKey } = require("@budibase/auth/src/objectStore")
|
2021-09-09 06:29:28 +12:00
|
|
|
const CouchDB = require("../db")
|
|
|
|
const { generateMetadataID } = require("../db/utils")
|
2020-11-04 02:45:49 +13:00
|
|
|
|
2021-08-16 22:42:21 +12:00
|
|
|
const BB_CDN = "https://cdn.budi.live"
|
2020-10-29 09:35:06 +13:00
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.wait = ms => new Promise(resolve => setTimeout(resolve, ms))
|
2020-10-27 06:49:33 +13:00
|
|
|
|
2021-03-24 04:37:11 +13:00
|
|
|
exports.isDev = env.isDev
|
2021-03-26 12:42:50 +13:00
|
|
|
|
2021-04-02 02:11:58 +13:00
|
|
|
/**
|
|
|
|
* Makes sure that a URL has the correct number of slashes, while maintaining the
|
|
|
|
* http(s):// double slashes.
|
|
|
|
* @param {string} url The URL to test and remove any extra double slashes.
|
|
|
|
* @return {string} The updated url.
|
|
|
|
*/
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.checkSlashesInUrl = url => {
|
2021-03-26 12:42:50 +13:00
|
|
|
return url.replace(/(https?:\/\/)|(\/)+/g, "$1$2")
|
|
|
|
}
|
2021-04-02 00:48:38 +13:00
|
|
|
|
2021-04-02 02:11:58 +13:00
|
|
|
/**
|
|
|
|
* Gets the address of the object store, depending on whether self hosted or in cloud.
|
|
|
|
* @return {string} The base URL of the object store (MinIO or S3).
|
|
|
|
*/
|
2021-04-02 00:48:38 +13:00
|
|
|
exports.objectStoreUrl = () => {
|
|
|
|
if (env.SELF_HOSTED) {
|
|
|
|
// can use a relative url for this as all goes through the proxy (this is hosted in minio)
|
|
|
|
return OBJ_STORE_DIRECTORY
|
|
|
|
} else {
|
|
|
|
return BB_CDN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 02:11:58 +13:00
|
|
|
/**
|
|
|
|
* In production the client library is stored in the object store, however in development
|
|
|
|
* we use the symlinked version produced by lerna, located in node modules. We link to this
|
|
|
|
* via a specific endpoint (under /api/assets/client).
|
|
|
|
* @param {string} appId In production we need the appId to look up the correct bucket, as the
|
|
|
|
* version of the client lib may differ between apps.
|
|
|
|
* @return {string} The URL to be inserted into appPackage response or server rendered
|
|
|
|
* app index file.
|
|
|
|
*/
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.clientLibraryPath = appId => {
|
2021-04-02 00:48:38 +13:00
|
|
|
if (env.isProd()) {
|
2021-05-25 08:30:46 +12:00
|
|
|
return `${exports.objectStoreUrl()}/${sanitizeKey(
|
|
|
|
appId
|
|
|
|
)}/budibase-client.js`
|
2021-04-02 00:48:38 +13:00
|
|
|
} else {
|
2021-04-02 02:11:58 +13:00
|
|
|
return `/api/assets/client`
|
2021-04-02 00:48:38 +13:00
|
|
|
}
|
|
|
|
}
|
2021-04-10 00:49:16 +12:00
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.attachmentsRelativeURL = attachmentKey => {
|
2021-04-10 00:49:16 +12:00
|
|
|
return exports.checkSlashesInUrl(
|
2021-08-06 03:40:39 +12:00
|
|
|
`${exports.objectStoreUrl()}/${attachmentKey}`
|
2021-04-10 00:49:16 +12:00
|
|
|
)
|
|
|
|
}
|
2021-09-09 06:29:28 +12:00
|
|
|
|
|
|
|
exports.saveEntityMetadata = async (appId, type, entityId, metadata) => {
|
|
|
|
const db = new CouchDB(appId)
|
|
|
|
const id = generateMetadataID(type, entityId)
|
|
|
|
// read it to see if it exists, we'll overwrite it no matter what
|
|
|
|
let rev
|
|
|
|
try {
|
|
|
|
const oldMetadata = await db.get(id)
|
|
|
|
rev = oldMetadata._rev
|
|
|
|
} catch (err) {
|
|
|
|
rev = null
|
|
|
|
}
|
|
|
|
metadata._id = id
|
|
|
|
if (rev) {
|
|
|
|
metadata._rev = rev
|
|
|
|
}
|
|
|
|
const response = await db.put(metadata)
|
|
|
|
return {
|
|
|
|
...metadata,
|
|
|
|
_id: id,
|
|
|
|
_rev: response.rev,
|
|
|
|
}
|
|
|
|
}
|