From 08ea461caab833cf9ac27ffab398080ebe864269 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 15 Mar 2021 12:10:21 +0000 Subject: [PATCH 1/5] Remove electron specific attachment uploads inside the builder --- packages/builder/src/builderStore/api.js | 3 +- .../src/components/common/Dropzone.svelte | 17 +++----- .../src/api/controllers/static/index.js | 43 ------------------- packages/server/src/api/routes/static.js | 6 +-- 4 files changed, 8 insertions(+), 61 deletions(-) diff --git a/packages/builder/src/builderStore/api.js b/packages/builder/src/builderStore/api.js index 46304e1486..0202c5e8ab 100644 --- a/packages/builder/src/builderStore/api.js +++ b/packages/builder/src/builderStore/api.js @@ -7,9 +7,10 @@ const apiCall = method => async ( headers = { "Content-Type": "application/json" } ) => { headers["x-budibase-app-id"] = svelteGet(store).appId + const json = headers["Content-Type"] === "application/json" return await fetch(url, { method: method, - body: body && JSON.stringify(body), + body: json ? JSON.stringify(body) : body, headers, }) } diff --git a/packages/builder/src/components/common/Dropzone.svelte b/packages/builder/src/components/common/Dropzone.svelte index be93829084..0b98f2b256 100644 --- a/packages/builder/src/components/common/Dropzone.svelte +++ b/packages/builder/src/components/common/Dropzone.svelte @@ -15,18 +15,11 @@ } async function processFiles(fileList) { - const fileArray = Array.from(fileList) - - const filesToProcess = fileArray.map(({ name, path, size, type }) => ({ - name, - path, - size, - type, - })) - - const response = await api.post(`/api/attachments/process`, { - files: filesToProcess, - }) + let data = new FormData() + for (let i = 0; i < fileList.length; i++) { + data.append("file", fileList[i]) + } + const response = await api.post(`/api/attachments/process`, data, {}) return await response.json() } diff --git a/packages/server/src/api/controllers/static/index.js b/packages/server/src/api/controllers/static/index.js index bf01314795..bc9384b892 100644 --- a/packages/server/src/api/controllers/static/index.js +++ b/packages/server/src/api/controllers/static/index.js @@ -74,29 +74,6 @@ exports.uploadFile = async function(ctx) { "attachments" ) - if (env.CLOUD) { - // remote upload - const s3 = new AWS.S3({ - params: { - Bucket: "prod-budi-app-assets", - }, - }) - - const uploads = files.map(file => { - const fileExtension = [...file.name.split(".")].pop() - const processedFileName = `${uuid.v4()}.${fileExtension}` - - return prepareUpload({ - file, - s3Key: `assets/${ctx.user.appId}/attachments/${processedFileName}`, - s3, - }) - }) - - ctx.body = await Promise.all(uploads) - return - } - ctx.body = await processLocalFileUploads({ files, outputPath: attachmentsPath, @@ -152,26 +129,6 @@ async function processLocalFileUploads({ files, outputPath, appId }) { return processedFiles } -exports.performLocalFileProcessing = async function(ctx) { - const { files } = ctx.request.body - - const processedFileOutputPath = resolve( - budibaseAppsDir(), - ctx.user.appId, - "attachments" - ) - - try { - ctx.body = await processLocalFileUploads({ - files, - outputPath: processedFileOutputPath, - appId: ctx.user.appId, - }) - } catch (err) { - ctx.throw(500, err) - } -} - exports.serveApp = async function(ctx) { let appId = ctx.params.appId if (env.SELF_HOSTED) { diff --git a/packages/server/src/api/routes/static.js b/packages/server/src/api/routes/static.js index c812c4d3b1..30701d578b 100644 --- a/packages/server/src/api/routes/static.js +++ b/packages/server/src/api/routes/static.js @@ -29,11 +29,7 @@ if (env.SELF_HOSTED) { } router - .post( - "/api/attachments/process", - authorized(BUILDER), - controller.performLocalFileProcessing - ) + .post("/api/attachments/process", authorized(BUILDER), controller.uploadFile) .post("/api/attachments/upload", usage, controller.uploadFile) .get("/componentlibrary", controller.serveComponentLibrary) .get("/assets/:file*", controller.serveAppAsset) From 78c6f9ca3d1ea53741478196db6d1475e8908339 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 15 Mar 2021 18:32:20 +0000 Subject: [PATCH 2/5] Remove electron specific app import --- .../src/components/start/CreateAppModal.svelte | 18 ++++++++++++------ .../src/components/start/Steps/Info.svelte | 5 ++--- .../server/src/api/controllers/application.js | 15 ++++++++++----- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/packages/builder/src/components/start/CreateAppModal.svelte b/packages/builder/src/components/start/CreateAppModal.svelte index 7f1d73f744..5102179cde 100644 --- a/packages/builder/src/components/start/CreateAppModal.svelte +++ b/packages/builder/src/components/start/CreateAppModal.svelte @@ -123,13 +123,19 @@ async function createNewApp() { submitting = true try { - // Create App - const appResp = await post("/api/applications", { - name: $createAppStore.values.applicationName, - template, - }) - const appJson = await appResp.json() + // Create form data to create app + let data = new FormData() + data.append("name", $createAppStore.values.applicationName) + data.append("useTemplate", template != null) + if (template) { + data.append("templateName", template.name) + data.append("templateKey", template.key) + data.append("templateFile", template.file) + } + // Create App + const appResp = await post("/api/applications", data, {}) + const appJson = await appResp.json() if (!appResp.ok) { throw new Error(appJson.message) } diff --git a/packages/builder/src/components/start/Steps/Info.svelte b/packages/builder/src/components/start/Steps/Info.svelte index e14e327bce..fa74e8b3a0 100644 --- a/packages/builder/src/components/start/Steps/Info.svelte +++ b/packages/builder/src/components/start/Steps/Info.svelte @@ -1,6 +1,5 @@ diff --git a/packages/server/src/api/controllers/application.js b/packages/server/src/api/controllers/application.js index cba3e6455a..2023385a7f 100644 --- a/packages/server/src/api/controllers/application.js +++ b/packages/server/src/api/controllers/application.js @@ -91,7 +91,6 @@ async function getAppUrlIfNotInUse(ctx) { async function createInstance(template) { const appId = generateAppID() - const db = new CouchDB(appId) await db.put({ _id: "_design/database", @@ -106,10 +105,10 @@ async function createInstance(template) { // replicate the template data to the instance DB // this is currently very hard to test, downloading and importing template files /* istanbul ignore next */ - if (template) { + if (template && template.useTemplate === "true") { let dbDumpReadStream - if (template.fileImportPath) { - dbDumpReadStream = fs.createReadStream(template.fileImportPath) + if (template.file) { + dbDumpReadStream = fs.createReadStream(template.file.path) } else { const templatePath = await downloadTemplate(...template.key.split("/")) dbDumpReadStream = fs.createReadStream( @@ -162,8 +161,14 @@ exports.fetchAppPackage = async function(ctx) { } exports.create = async function(ctx) { + const { useTemplate, templateKey } = ctx.request.body + const instance = await createInstance({ + useTemplate, + key: templateKey, + file: ctx.request.files.templateFile, + }) + const url = await getAppUrlIfNotInUse(ctx) - const instance = await createInstance(ctx.request.body.template) const appId = instance._id const version = packageJson.version const newApplication = { From 05b6e825b4fb231b9d5a35a30cd31da8ffd97d79 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 16 Mar 2021 09:28:58 +0000 Subject: [PATCH 3/5] Lint --- .../PropertyControls/ColorPicker.svelte | 34 +++++++++++-------- .../src/api/controllers/static/index.js | 2 -- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ColorPicker.svelte b/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ColorPicker.svelte index 6235e744f8..c777f79666 100644 --- a/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ColorPicker.svelte +++ b/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ColorPicker.svelte @@ -1,38 +1,42 @@ diff --git a/packages/server/src/api/controllers/static/index.js b/packages/server/src/api/controllers/static/index.js index bc9384b892..f5de271d36 100644 --- a/packages/server/src/api/controllers/static/index.js +++ b/packages/server/src/api/controllers/static/index.js @@ -5,8 +5,6 @@ const { resolve, join } = require("../../../utilities/centralPath") const fetch = require("node-fetch") const fs = require("fs-extra") const uuid = require("uuid") -const AWS = require("aws-sdk") -const { prepareUpload } = require("../deploy/utils") const { processString } = require("@budibase/string-templates") const { budibaseAppsDir, From f114f68021109b9e3f8b39d3598bf272282ab5c9 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 16 Mar 2021 13:43:46 +0000 Subject: [PATCH 4/5] Fixing issue with applications not being created due to a lack of template file. --- packages/server/src/api/controllers/application.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/controllers/application.js b/packages/server/src/api/controllers/application.js index 2023385a7f..9e50319d5d 100644 --- a/packages/server/src/api/controllers/application.js +++ b/packages/server/src/api/controllers/application.js @@ -162,11 +162,14 @@ exports.fetchAppPackage = async function(ctx) { exports.create = async function(ctx) { const { useTemplate, templateKey } = ctx.request.body - const instance = await createInstance({ + const instanceConfig = { useTemplate, key: templateKey, - file: ctx.request.files.templateFile, - }) + } + if (ctx.request.files && ctx.request.files.templateFile) { + instanceConfig.file = ctx.request.files.templateFile + } + const instance = await createInstance(instanceConfig) const url = await getAppUrlIfNotInUse(ctx) const appId = instance._id From 9212a2f623270992c64e1ded6ff740329c4192e5 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 16 Mar 2021 13:51:50 +0000 Subject: [PATCH 5/5] Revert deletion of S3 upload of attachments in cloud env --- .../src/api/controllers/static/index.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/server/src/api/controllers/static/index.js b/packages/server/src/api/controllers/static/index.js index f5de271d36..ff1ecf5b2e 100644 --- a/packages/server/src/api/controllers/static/index.js +++ b/packages/server/src/api/controllers/static/index.js @@ -5,6 +5,8 @@ const { resolve, join } = require("../../../utilities/centralPath") const fetch = require("node-fetch") const fs = require("fs-extra") const uuid = require("uuid") +const AWS = require("aws-sdk") +const { prepareUpload } = require("../deploy/utils") const { processString } = require("@budibase/string-templates") const { budibaseAppsDir, @@ -72,6 +74,29 @@ exports.uploadFile = async function(ctx) { "attachments" ) + if (env.CLOUD) { + // remote upload + const s3 = new AWS.S3({ + params: { + Bucket: "prod-budi-app-assets", + }, + }) + + const uploads = files.map(file => { + const fileExtension = [...file.name.split(".")].pop() + const processedFileName = `${uuid.v4()}.${fileExtension}` + + return prepareUpload({ + file, + s3Key: `assets/${ctx.user.appId}/attachments/${processedFileName}`, + s3, + }) + }) + + ctx.body = await Promise.all(uploads) + return + } + ctx.body = await processLocalFileUploads({ files, outputPath: attachmentsPath,