Fixing attachment issue in self hosting, urls are enriched on way out to point directly to MINIO.

This commit is contained in:
mike12345567 2021-02-01 18:08:06 +00:00
parent 846af6d789
commit 164c5594e3
3 changed files with 42 additions and 8 deletions

View File

@ -10,6 +10,8 @@ const {
} = require("../../db/utils") } = require("../../db/utils")
const usersController = require("./user") const usersController = require("./user")
const { coerceRowValues } = require("../../utilities") const { coerceRowValues } = require("../../utilities")
const env = require("../../environment")
const { OBJ_STORE_DIRECTORY } = require("../../constants")
const TABLE_VIEW_BEGINS_WITH = `all${SEPARATOR}${DocumentTypes.TABLE}${SEPARATOR}` const TABLE_VIEW_BEGINS_WITH = `all${SEPARATOR}${DocumentTypes.TABLE}${SEPARATOR}`
@ -51,6 +53,32 @@ async function findRow(db, appId, tableId, rowId) {
return row 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) { exports.patch = async function(ctx) {
const appId = ctx.user.appId const appId = ctx.user.appId
const db = new CouchDB(appId) const db = new CouchDB(appId)
@ -190,7 +218,8 @@ exports.fetchView = async function(ctx) {
if (!calculation) { if (!calculation) {
response.rows = response.rows.map(row => row.doc) 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) { if (calculation === CALCULATION_TYPES.STATS) {
@ -217,14 +246,15 @@ exports.fetchView = async function(ctx) {
exports.fetchTableRows = async function(ctx) { exports.fetchTableRows = async function(ctx) {
const appId = ctx.user.appId const appId = ctx.user.appId
const db = new CouchDB(appId)
// special case for users, fetch through the user controller // 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) { if (ctx.params.tableId === ViewNames.USERS) {
await usersController.fetch(ctx) await usersController.fetch(ctx)
rows = ctx.body rows = ctx.body
} else { } else {
const db = new CouchDB(appId)
const response = await db.allDocs( const response = await db.allDocs(
getRowParams(ctx.params.tableId, null, { getRowParams(ctx.params.tableId, null, {
include_docs: true, include_docs: true,
@ -232,15 +262,16 @@ exports.fetchTableRows = async function(ctx) {
) )
rows = response.rows.map(row => row.doc) 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) { exports.find = async function(ctx) {
const appId = ctx.user.appId const appId = ctx.user.appId
const db = new CouchDB(appId) const db = new CouchDB(appId)
try { try {
const table = await db.get(ctx.params.tableId)
const row = await findRow(db, appId, ctx.params.tableId, ctx.params.rowId) 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) { } catch (err) {
ctx.throw(400, err) ctx.throw(400, err)
} }
@ -325,9 +356,10 @@ exports.fetchEnrichedRow = async function(ctx) {
keys: linkVals.map(linkVal => linkVal.id), keys: linkVals.map(linkVal => linkVal.id),
}) })
// need to include the IDs in these rows for any links they may have // need to include the IDs in these rows for any links they may have
let linkedRows = await linkRows.attachLinkInfo( let linkedRows = await enrichRows(
appId, 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 // insert the link rows in the correct place throughout the main row
for (let fieldName of Object.keys(table.schema)) { for (let fieldName of Object.keys(table.schema)) {

View File

@ -17,11 +17,12 @@ const CouchDB = require("../../../db")
const setBuilderToken = require("../../../utilities/builder/setBuilderToken") const setBuilderToken = require("../../../utilities/builder/setBuilderToken")
const fileProcessor = require("../../../utilities/fileProcessor") const fileProcessor = require("../../../utilities/fileProcessor")
const env = require("../../../environment") const env = require("../../../environment")
const { OBJ_STORE_DIRECTORY } = require("../../../constants")
function objectStoreUrl() { function objectStoreUrl() {
if (env.SELF_HOSTED) { if (env.SELF_HOSTED) {
// can use a relative url for this as all goes through the proxy (this is hosted in minio) // 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 { } else {
return "https://cdn.app.budi.live/assets" return "https://cdn.app.budi.live/assets"
} }

View File

@ -43,3 +43,4 @@ exports.AuthTypes = AuthTypes
exports.USERS_TABLE_SCHEMA = USERS_TABLE_SCHEMA exports.USERS_TABLE_SCHEMA = USERS_TABLE_SCHEMA
exports.BUILDER_CONFIG_DB = "builder-config-db" exports.BUILDER_CONFIG_DB = "builder-config-db"
exports.HOSTING_DOC = "hosting-doc" exports.HOSTING_DOC = "hosting-doc"
exports.OBJ_STORE_DIRECTORY = "/app-assets/assets"