Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dev/src/mocks/fsMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { fs: { createReadStream: () => null } }
4 changes: 4 additions & 0 deletions dev/src/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ if (process.env.PAYLOAD_PUBLIC_CLOUD_STORAGE_ADAPTER === 'azure') {
allowContainerCreate: process.env.AZURE_STORAGE_ALLOW_CONTAINER_CREATE === 'true',
baseURL: process.env.AZURE_STORAGE_ACCOUNT_BASEURL,
})
// uploadOptions = {
Comment thread
DanRibbens marked this conversation as resolved.
// useTempFiles: true,
// }
}

if (process.env.PAYLOAD_PUBLIC_CLOUD_STORAGE_ADAPTER === 's3') {
Expand Down Expand Up @@ -69,6 +72,7 @@ export default buildConfig({
'@azure/storage-blob': path.resolve(__dirname, '../../src/adapters/azure/mock.js'),
'@aws-sdk/client-s3': path.resolve(__dirname, '../../src/adapters/s3/mock.js'),
'@google-cloud/storage': path.resolve(__dirname, '../../src/adapters/gcs/mock.js'),
fs: path.resolve(__dirname, './mocks/fsMock.js'),
},
},
}
Expand Down
21 changes: 19 additions & 2 deletions src/adapters/azure/handleUpload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import path from 'path'
import fs from 'fs'
import { Readable } from 'stream'
import type { ContainerClient } from '@azure/storage-blob'
import { AbortController } from '@azure/abort-controller'
import type { CollectionConfig } from 'payload/types'
import type { HandleUpload } from '../../types'

Expand All @@ -9,14 +12,28 @@ interface Args {
prefix?: string
}

const multipartThreshold = 1024 * 1024 * 50 // 50MB
export const getHandleUpload = ({ getStorageClient, prefix = '' }: Args): HandleUpload => {
return async ({ data, file }) => {
const blockBlobClient = getStorageClient().getBlockBlobClient(
path.posix.join(prefix, file.filename),
)

await blockBlobClient.upload(file.buffer, file.buffer.byteLength, {
blobHTTPHeaders: { blobContentType: file.mimeType },
// when there are no temp files, or the upload is less than the threshold size, do not stream files
if (!file.tempFilePath && file.buffer.length > 0 && file.buffer.length < multipartThreshold) {
await blockBlobClient.upload(file.buffer, file.buffer.byteLength, {
blobHTTPHeaders: { blobContentType: file.mimeType },
})

return data
}

const fileBufferOrStream: Readable = file.tempFilePath
? fs.createReadStream(file.tempFilePath)
: Readable.from(file.buffer)

await blockBlobClient.uploadStream(fileBufferOrStream, 4 * 1024 * 1024, 4, {
abortSignal: AbortController.timeout(30 * 60 * 1000),
})

return data
Expand Down
6 changes: 6 additions & 0 deletions src/adapters/azure/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ exports.BlobServiceClient = {
}),
}),
}

exports.AbortController = {
timeout: () => null,
}

exports.Readable = { from: () => null }
3 changes: 3 additions & 0 deletions src/adapters/azure/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export const extendWebpackConfig = (existingWebpackConfig: WebpackConfig): Webpa
...(existingWebpackConfig.resolve || {}),
alias: {
...(existingWebpackConfig.resolve?.alias ? existingWebpackConfig.resolve.alias : {}),
stream: path.resolve(__dirname, './mock.js'),
'@azure/storage-blob': path.resolve(__dirname, './mock.js'),
'@azure/abort-controller': path.resolve(__dirname, './mock.js'),
fs: path.resolve(__dirname, './fileStub.js'),
},
},
}
Expand Down