handle files in rest connector

This commit is contained in:
Peter Clement 2024-03-12 14:59:22 +00:00
parent ae83f637b3
commit 5fc4da8028
2 changed files with 29 additions and 6 deletions

View File

@ -26,12 +26,13 @@ type ListParams = {
type UploadParams = { type UploadParams = {
bucket: string bucket: string
filename: string filename: string
path: string path?: string
type?: string | null type?: string | null
// can be undefined, we will remove it // can be undefined, we will remove it
metadata?: { metadata?: {
[key: string]: string | undefined [key: string]: string | undefined
} }
body?: Buffer
} }
const CONTENT_TYPE_MAP: any = { const CONTENT_TYPE_MAP: any = {
@ -41,6 +42,7 @@ const CONTENT_TYPE_MAP: any = {
js: "application/javascript", js: "application/javascript",
json: "application/json", json: "application/json",
gz: "application/gzip", gz: "application/gzip",
svg: "image/svg+xml",
} }
const STRING_CONTENT_TYPES = [ const STRING_CONTENT_TYPES = [
@ -105,8 +107,13 @@ export function ObjectStore(
* Given an object store and a bucket name this will make sure the bucket exists, * Given an object store and a bucket name this will make sure the bucket exists,
* if it does not exist then it will create it. * if it does not exist then it will create it.
*/ */
export async function makeSureBucketExists(client: any, bucketName: string) { export async function makeSureBucketExists(
client: any,
bucketName: string,
addLifecycleConfig: boolean = false
) {
bucketName = sanitizeBucket(bucketName) bucketName = sanitizeBucket(bucketName)
try { try {
await client await client
.headBucket({ .headBucket({
@ -146,10 +153,10 @@ export async function upload({
path, path,
type, type,
metadata, metadata,
body,
}: UploadParams) { }: UploadParams) {
const extension = filename.split(".").pop() const extension = filename.split(".").pop()
const fileBytes = fs.readFileSync(path) const fileBytes = path ? fs.readFileSync(path) : body
const objectStore = ObjectStore(bucketName) const objectStore = ObjectStore(bucketName)
await makeSureBucketExists(objectStore, bucketName) await makeSureBucketExists(objectStore, bucketName)

View File

@ -20,7 +20,8 @@ import { formatBytes } from "../utilities"
import { performance } from "perf_hooks" import { performance } from "perf_hooks"
import FormData from "form-data" import FormData from "form-data"
import { URLSearchParams } from "url" import { URLSearchParams } from "url"
import { blacklist } from "@budibase/backend-core" import { blacklist, context, objectStore } from "@budibase/backend-core"
import * as uuid from "uuid"
const BodyTypes = { const BodyTypes = {
NONE: "none", NONE: "none",
@ -156,7 +157,22 @@ class RestIntegration implements IntegrationBase {
} }
raw = rawXml raw = rawXml
} else if (contentType.includes("application/pdf")) { } else if (contentType.includes("application/pdf")) {
data = await response.arrayBuffer() // Save PDF as ArrayBuffer data = await response.arrayBuffer()
raw = Buffer.from(data)
} else if (contentType.includes("image/")) {
const data = await response.arrayBuffer()
let bucketName = `tmp-bucket-${Date.now()}`
// filenames converted to UUIDs so they are unique
const processedFileName = `${uuid.v4()}.svg`
const key = `${context.getProdAppId()}/attachments/${processedFileName}`
await objectStore.upload({
bucket: bucketName,
filename: key,
type: contentType,
body: Buffer.from(data),
})
raw = Buffer.from(data) raw = Buffer.from(data)
} else { } else {
data = await response.text() data = await response.text()