This commit is contained in:
Peter Clement 2024-03-18 12:27:23 +00:00
parent 59a76b7a8f
commit 35f62f9960
2 changed files with 25 additions and 8 deletions

View File

@ -29,6 +29,7 @@ import { default as RestIntegration } from "../rest"
import { RestAuthType } from "@budibase/types" import { RestAuthType } from "@budibase/types"
import fetch from "node-fetch" import fetch from "node-fetch"
import { objectStoreTestProviders } from "./utils" import { objectStoreTestProviders } from "./utils"
import { Readable } from "stream"
const FormData = require("form-data") const FormData = require("form-data")
const { URLSearchParams } = require("url") const { URLSearchParams } = require("url")
@ -638,7 +639,9 @@ describe("REST Integration", () => {
const responseData = Buffer.from("teest file contnt") const responseData = Buffer.from("teest file contnt")
const filename = "test.tar.gz" const filename = "test.tar.gz"
const contentType = "application/gzip" const contentType = "application/gzip"
const mockReadable = new Readable()
mockReadable.push(responseData)
mockReadable.push(null)
;(fetch as unknown as jest.Mock).mockImplementationOnce(() => ;(fetch as unknown as jest.Mock).mockImplementationOnce(() =>
Promise.resolve({ Promise.resolve({
headers: { headers: {
@ -652,7 +655,7 @@ describe("REST Integration", () => {
return `attachment; filename="${filename}"` return `attachment; filename="${filename}"`
}, },
}, },
arrayBuffer: jest.fn(() => Promise.resolve(responseData)), body: mockReadable,
}) })
) )
@ -675,9 +678,10 @@ describe("REST Integration", () => {
it("uploads file with non ascii filename to object store and returns signed URL ", async () => { it("uploads file with non ascii filename to object store and returns signed URL ", async () => {
const responseData = Buffer.from("teest file contnt") const responseData = Buffer.from("teest file contnt")
const non_ascii_filename = "ex%C3%A4mple.txt"
const contentType = "text/plain" const contentType = "text/plain"
const mockReadable = new Readable()
mockReadable.push(responseData)
mockReadable.push(null)
;(fetch as unknown as jest.Mock).mockImplementationOnce(() => ;(fetch as unknown as jest.Mock).mockImplementationOnce(() =>
Promise.resolve({ Promise.resolve({
headers: { headers: {
@ -693,7 +697,7 @@ describe("REST Integration", () => {
return `attachment; filename="£ and ? rates.pdf"; filename*=UTF-8\'\'%C2%A3%20and%20%E2%82%AC%20rates.pdf` return `attachment; filename="£ and ? rates.pdf"; filename*=UTF-8\'\'%C2%A3%20and%20%E2%82%AC%20rates.pdf`
}, },
}, },
arrayBuffer: jest.fn(() => Promise.resolve(responseData)), body: mockReadable,
}) })
) )

View File

@ -468,7 +468,8 @@ export async function handleFileResponse(
filename: string, filename: string,
startTime: number startTime: number
) { ) {
let presignedUrl let presignedUrl,
size = 0
const fileExtension = filename.includes(".") const fileExtension = filename.includes(".")
? filename.split(".").slice(1).join(".") ? filename.split(".").slice(1).join(".")
: "" : ""
@ -478,7 +479,19 @@ export async function handleFileResponse(
const bucket = objectStore.ObjectStoreBuckets.TEMP const bucket = objectStore.ObjectStoreBuckets.TEMP
const stream = response.body.pipe(bl((error, data) => data)) const stream = response.body.pipe(bl((error, data) => data))
if (response.body) { if (response.body) {
const contentLength = response.headers.get("content-length")
if (contentLength) {
size = parseInt(contentLength, 10)
} else {
const chunks: Buffer[] = []
for await (const chunk of response.body) {
chunks.push(chunk)
size += chunk.length
}
}
await objectStore.streamUpload({ await objectStore.streamUpload({
bucket, bucket,
filename: key, filename: key,
@ -490,7 +503,7 @@ export async function handleFileResponse(
presignedUrl = await objectStore.getPresignedUrl(bucket, key, 600) presignedUrl = await objectStore.getPresignedUrl(bucket, key, 600)
return { return {
data: { data: {
size: stream.byteLength, size,
name: processedFileName, name: processedFileName,
url: presignedUrl, url: presignedUrl,
extension: fileExtension, extension: fileExtension,
@ -498,7 +511,7 @@ export async function handleFileResponse(
}, },
info: { info: {
code: response.status, code: response.status,
size: formatBytes(stream.byteLength), size: formatBytes(size.toString()),
time: `${Math.round(performance.now() - startTime)}ms`, time: `${Math.round(performance.now() - startTime)}ms`,
}, },
} }