fetch presigned url and return

This commit is contained in:
Peter Clement 2024-03-13 09:53:14 +00:00
parent 5fc4da8028
commit cf82ef057f
2 changed files with 48 additions and 13 deletions

View File

@ -110,10 +110,9 @@ export function ObjectStore(
export async function makeSureBucketExists( export async function makeSureBucketExists(
client: any, client: any,
bucketName: string, bucketName: string,
addLifecycleConfig: boolean = false addLifecycleConfig: boolean = true
) { ) {
bucketName = sanitizeBucket(bucketName) bucketName = sanitizeBucket(bucketName)
try { try {
await client await client
.headBucket({ .headBucket({
@ -128,12 +127,38 @@ export async function makeSureBucketExists(
await promises[bucketName] await promises[bucketName]
} else if (doesntExist || noAccess) { } else if (doesntExist || noAccess) {
if (doesntExist) { if (doesntExist) {
// bucket doesn't exist create it // bucket doesn't exist, create it
promises[bucketName] = client promises[bucketName] = client
.createBucket({ .createBucket({
Bucket: bucketName, Bucket: bucketName,
}) })
.promise() .promise()
.then(() => {
if (addLifecycleConfig) {
return client
.putBucketLifecycleConfiguration({
Bucket: bucketName,
LifecycleConfiguration: {
Rules: [
{
ID: "TTL Rule",
Status: "Enabled",
NoncurrentVersionExpiration: {
NoncurrentDays: 1,
},
Filter: {
Prefix: "",
},
AbortIncompleteMultipartUpload: {
DaysAfterInitiation: 1,
},
},
],
},
})
.promise()
}
})
await promises[bucketName] await promises[bucketName]
delete promises[bucketName] delete promises[bucketName]
} }
@ -142,7 +167,6 @@ export async function makeSureBucketExists(
} }
} }
} }
/** /**
* Uploads the contents of a file given the required parameters, useful when * Uploads the contents of a file given the required parameters, useful when
* temp files in use (for example file uploaded as an attachment). * temp files in use (for example file uploaded as an attachment).

View File

@ -130,8 +130,19 @@ class RestIntegration implements IntegrationBase {
} }
async parseResponse(response: any, pagination: PaginationConfig | null) { async parseResponse(response: any, pagination: PaginationConfig | null) {
let data, raw, headers let data, raw, headers, presignedUrl, fileExtension
const contentType = response.headers.get("content-type") || "" const contentType = response.headers.get("content-type") || ""
const contentDisposition = response.headers.get("content-disposition") || ""
const filenameMatch = contentDisposition.match(/filename="?(.+)"?/i)
if (filenameMatch) {
const filename = filenameMatch[1]
const lastDotIndex = filename.lastIndexOf(".")
if (lastDotIndex !== -1) {
fileExtension = filename.slice(lastDotIndex + 1)
}
}
try { try {
if (response.status === 204) { if (response.status === 204) {
data = [] data = []
@ -156,15 +167,13 @@ class RestIntegration implements IntegrationBase {
data = data[keys[0]] data = data[keys[0]]
} }
raw = rawXml raw = rawXml
} else if (contentType.includes("application/pdf")) { } else if (/^(image|video|audio|application|text)\//.test(contentType)) {
data = await response.arrayBuffer()
raw = Buffer.from(data)
} else if (contentType.includes("image/")) {
const data = await response.arrayBuffer() const data = await response.arrayBuffer()
let bucketName = `tmp-bucket-${Date.now()}` let bucketName = `tmp-bucket-attachments-${context.getTenantId()}`
// filenames converted to UUIDs so they are unique const processedFileName = `${uuid.v4()}.${
const processedFileName = `${uuid.v4()}.svg` fileExtension || contentType.split("/")[1]
}`
const key = `${context.getProdAppId()}/attachments/${processedFileName}` const key = `${context.getProdAppId()}/attachments/${processedFileName}`
await objectStore.upload({ await objectStore.upload({
@ -173,6 +182,8 @@ class RestIntegration implements IntegrationBase {
type: contentType, type: contentType,
body: Buffer.from(data), body: Buffer.from(data),
}) })
presignedUrl = await objectStore.getPresignedUrl(bucketName, key, 600)
raw = Buffer.from(data) raw = Buffer.from(data)
} else { } else {
data = await response.text() data = await response.text()
@ -195,7 +206,6 @@ class RestIntegration implements IntegrationBase {
if (pagination?.responseParam) { if (pagination?.responseParam) {
nextCursor = get(data, pagination.responseParam) nextCursor = get(data, pagination.responseParam)
} }
return { return {
data, data,
info: { info: {
@ -206,6 +216,7 @@ class RestIntegration implements IntegrationBase {
extra: { extra: {
raw, raw,
headers, headers,
presignedUrl,
}, },
pagination: { pagination: {
cursor: nextCursor, cursor: nextCursor,