handle files in rest connector
This commit is contained in:
parent
ae83f637b3
commit
5fc4da8028
|
@ -26,12 +26,13 @@ type ListParams = {
|
|||
type UploadParams = {
|
||||
bucket: string
|
||||
filename: string
|
||||
path: string
|
||||
path?: string
|
||||
type?: string | null
|
||||
// can be undefined, we will remove it
|
||||
metadata?: {
|
||||
[key: string]: string | undefined
|
||||
}
|
||||
body?: Buffer
|
||||
}
|
||||
|
||||
const CONTENT_TYPE_MAP: any = {
|
||||
|
@ -41,6 +42,7 @@ const CONTENT_TYPE_MAP: any = {
|
|||
js: "application/javascript",
|
||||
json: "application/json",
|
||||
gz: "application/gzip",
|
||||
svg: "image/svg+xml",
|
||||
}
|
||||
|
||||
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,
|
||||
* 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)
|
||||
|
||||
try {
|
||||
await client
|
||||
.headBucket({
|
||||
|
@ -146,10 +153,10 @@ export async function upload({
|
|||
path,
|
||||
type,
|
||||
metadata,
|
||||
body,
|
||||
}: UploadParams) {
|
||||
const extension = filename.split(".").pop()
|
||||
const fileBytes = fs.readFileSync(path)
|
||||
|
||||
const fileBytes = path ? fs.readFileSync(path) : body
|
||||
const objectStore = ObjectStore(bucketName)
|
||||
await makeSureBucketExists(objectStore, bucketName)
|
||||
|
||||
|
|
|
@ -20,7 +20,8 @@ import { formatBytes } from "../utilities"
|
|||
import { performance } from "perf_hooks"
|
||||
import FormData from "form-data"
|
||||
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 = {
|
||||
NONE: "none",
|
||||
|
@ -156,7 +157,22 @@ class RestIntegration implements IntegrationBase {
|
|||
}
|
||||
raw = rawXml
|
||||
} 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)
|
||||
} else {
|
||||
data = await response.text()
|
||||
|
|
Loading…
Reference in New Issue