From 499583ff22a57209631da54816a0bbfa19a930e0 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 8 Jun 2021 12:50:58 +0100 Subject: [PATCH] fixing attachments for public apps --- packages/client/src/api/attachments.js | 4 ++-- packages/server/src/api/routes/static.js | 6 +++--- .../standard-components/src/forms/AttachmentField.svelte | 8 ++++++-- packages/standard-components/src/forms/Form.svelte | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/client/src/api/attachments.js b/packages/client/src/api/attachments.js index ff5c48c911..2693034d2e 100644 --- a/packages/client/src/api/attachments.js +++ b/packages/client/src/api/attachments.js @@ -3,9 +3,9 @@ import API from "./api" /** * Uploads an attachment to the server. */ -export const uploadAttachment = async data => { +export const uploadAttachment = async (data, tableId = "") => { return await API.post({ - url: "/api/attachments/upload", + url: `/api/attachments/${tableId}/upload`, body: data, json: false, }) diff --git a/packages/server/src/api/routes/static.js b/packages/server/src/api/routes/static.js index 1ec0ba8e12..6eef718fd1 100644 --- a/packages/server/src/api/routes/static.js +++ b/packages/server/src/api/routes/static.js @@ -7,8 +7,8 @@ const { PermissionTypes, PermissionLevels, } = require("@budibase/auth/permissions") -const usage = require("../../middleware/usageQuota") const env = require("../../environment") +const { paramResource } = require("../../middleware/resourceId") const router = Router() @@ -39,9 +39,9 @@ router .get("/builder/:file*", controller.serveBuilder) .post("/api/attachments/process", authorized(BUILDER), controller.uploadFile) .post( - "/api/attachments/upload", + "/api/attachments/:tableId/upload", + paramResource("tableId"), authorized(PermissionTypes.TABLE, PermissionLevels.WRITE), - usage, controller.uploadFile ) .get("/componentlibrary", controller.serveComponentLibrary) diff --git a/packages/standard-components/src/forms/AttachmentField.svelte b/packages/standard-components/src/forms/AttachmentField.svelte index facfea45e3..e88a7b0a44 100644 --- a/packages/standard-components/src/forms/AttachmentField.svelte +++ b/packages/standard-components/src/forms/AttachmentField.svelte @@ -9,8 +9,10 @@ let fieldState let fieldApi + let fieldSchema const { API, notifications } = getContext("sdk") + const formContext = getContext("form") const BYTES_IN_MB = 1000000 export let files = [] @@ -28,7 +30,7 @@ for (let i = 0; i < fileList.length; i++) { data.append("file", fileList[i]) } - return await API.uploadAttachment(data) + return await API.uploadAttachment(data, formContext?.dataSource?.tableId) } @@ -45,7 +47,9 @@ fieldApi.setValue(e.detail)} + on:change={e => { + fieldApi.setValue(e.detail) + }} {processFiles} {handleFileTooLarge} /> diff --git a/packages/standard-components/src/forms/Form.svelte b/packages/standard-components/src/forms/Form.svelte index afa4aeeeb4..d73af72a47 100644 --- a/packages/standard-components/src/forms/Form.svelte +++ b/packages/standard-components/src/forms/Form.svelte @@ -80,7 +80,7 @@ } // Provide both form API and state to children - setContext("form", { formApi, formState }) + setContext("form", { formApi, formState, dataSource }) // Action context to pass to children $: actions = [{ type: ActionTypes.ValidateForm, callback: formApi.validate }]