From 164c5594e343324e37a079ff97a2e68fbea7cb8c Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 1 Feb 2021 18:08:06 +0000 Subject: [PATCH] Fixing attachment issue in self hosting, urls are enriched on way out to point directly to MINIO. --- packages/server/src/api/controllers/row.js | 46 ++++++++++++++++--- .../src/api/controllers/static/index.js | 3 +- packages/server/src/constants/index.js | 1 + 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/packages/server/src/api/controllers/row.js b/packages/server/src/api/controllers/row.js index 6cf094cd1f..8e33be1f22 100644 --- a/packages/server/src/api/controllers/row.js +++ b/packages/server/src/api/controllers/row.js @@ -10,6 +10,8 @@ const { } = require("../../db/utils") const usersController = require("./user") const { coerceRowValues } = require("../../utilities") +const env = require("../../environment") +const { OBJ_STORE_DIRECTORY } = require("../../constants") const TABLE_VIEW_BEGINS_WITH = `all${SEPARATOR}${DocumentTypes.TABLE}${SEPARATOR}` @@ -51,6 +53,32 @@ async function findRow(db, appId, tableId, rowId) { return row } +/** + * This function "enriches" the input rows with anything they are supposed to contain, for example + * link records or attachment links. + */ +async function enrichRows(appId, table, rows) { + // attach any linked row information + const enriched = await linkRows.attachLinkInfo(appId, rows) + // update the attachments URL depending on hosting + if (env.CLOUD && env.SELF_HOSTED) { + for (let [property, column] of Object.entries(table.schema)) { + if (column.type === "attachment") { + for (let row of enriched) { + if (row[property] == null || row[property].length === 0) { + continue + } + row[property].forEach(attachment => { + attachment.url = `${OBJ_STORE_DIRECTORY}/${appId}/${attachment.url}` + attachment.url = attachment.url.replace("//", "/") + }) + } + } + } + } + return enriched +} + exports.patch = async function(ctx) { const appId = ctx.user.appId const db = new CouchDB(appId) @@ -190,7 +218,8 @@ exports.fetchView = async function(ctx) { if (!calculation) { response.rows = response.rows.map(row => row.doc) - ctx.body = await linkRows.attachLinkInfo(appId, response.rows) + const table = await db.get(ctx.params.tableId) + ctx.body = await enrichRows(appId, table, response.rows) } if (calculation === CALCULATION_TYPES.STATS) { @@ -217,14 +246,15 @@ exports.fetchView = async function(ctx) { exports.fetchTableRows = async function(ctx) { const appId = ctx.user.appId + const db = new CouchDB(appId) // special case for users, fetch through the user controller - let rows + let rows, table = await db.get(ctx.params.tableId) if (ctx.params.tableId === ViewNames.USERS) { await usersController.fetch(ctx) rows = ctx.body } else { - const db = new CouchDB(appId) + const response = await db.allDocs( getRowParams(ctx.params.tableId, null, { include_docs: true, @@ -232,15 +262,16 @@ exports.fetchTableRows = async function(ctx) { ) rows = response.rows.map(row => row.doc) } - ctx.body = await linkRows.attachLinkInfo(appId, rows) + ctx.body = await enrichRows(appId, table, rows) } exports.find = async function(ctx) { const appId = ctx.user.appId const db = new CouchDB(appId) try { + const table = await db.get(ctx.params.tableId) const row = await findRow(db, appId, ctx.params.tableId, ctx.params.rowId) - ctx.body = await linkRows.attachLinkInfo(appId, row) + ctx.body = await enrichRows(appId, table, row) } catch (err) { ctx.throw(400, err) } @@ -325,9 +356,10 @@ exports.fetchEnrichedRow = async function(ctx) { keys: linkVals.map(linkVal => linkVal.id), }) // need to include the IDs in these rows for any links they may have - let linkedRows = await linkRows.attachLinkInfo( + let linkedRows = await enrichRows( appId, - response.rows.map(row => row.doc) + table, + response.rows.map(row => row.doc), ) // insert the link rows in the correct place throughout the main row for (let fieldName of Object.keys(table.schema)) { diff --git a/packages/server/src/api/controllers/static/index.js b/packages/server/src/api/controllers/static/index.js index 770cbaf088..bf01314795 100644 --- a/packages/server/src/api/controllers/static/index.js +++ b/packages/server/src/api/controllers/static/index.js @@ -17,11 +17,12 @@ const CouchDB = require("../../../db") const setBuilderToken = require("../../../utilities/builder/setBuilderToken") const fileProcessor = require("../../../utilities/fileProcessor") const env = require("../../../environment") +const { OBJ_STORE_DIRECTORY } = require("../../../constants") function objectStoreUrl() { if (env.SELF_HOSTED) { // can use a relative url for this as all goes through the proxy (this is hosted in minio) - return `/app-assets/assets` + return OBJ_STORE_DIRECTORY } else { return "https://cdn.app.budi.live/assets" } diff --git a/packages/server/src/constants/index.js b/packages/server/src/constants/index.js index 5a97a62af6..2e18de98af 100644 --- a/packages/server/src/constants/index.js +++ b/packages/server/src/constants/index.js @@ -43,3 +43,4 @@ exports.AuthTypes = AuthTypes exports.USERS_TABLE_SCHEMA = USERS_TABLE_SCHEMA exports.BUILDER_CONFIG_DB = "builder-config-db" exports.HOSTING_DOC = "hosting-doc" +exports.OBJ_STORE_DIRECTORY = "/app-assets/assets"