|
| 1 | +import type { ContainerClient } from '@azure/storage-blob' |
| 2 | +import type { CollectionSlug, Payload } from 'payload' |
| 3 | + |
| 4 | +import { BlobServiceClient } from '@azure/storage-blob' |
| 5 | +import { readFile } from 'node:fs/promises' |
| 6 | +import path from 'path' |
| 7 | +import { fileURLToPath } from 'url' |
| 8 | +import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest' |
| 9 | + |
| 10 | +import type { NextRESTClient } from '../__helpers/shared/NextRESTClient.js' |
| 11 | + |
| 12 | +import { initPayloadInt } from '../__helpers/shared/initPayloadInt.js' |
| 13 | +import { mediaSlug, mediaWithPrefixSlug, prefix } from './shared.js' |
| 14 | + |
| 15 | +const filename = fileURLToPath(import.meta.url) |
| 16 | +const dirname = path.dirname(filename) |
| 17 | + |
| 18 | +let restClient: NextRESTClient |
| 19 | +let payload: Payload |
| 20 | + |
| 21 | +describe('@payloadcms/storage-azure streamingUploads', () => { |
| 22 | + let TEST_CONTAINER: string |
| 23 | + let client: ContainerClient |
| 24 | + |
| 25 | + beforeAll(async () => { |
| 26 | + ;({ payload, restClient } = await initPayloadInt( |
| 27 | + dirname, |
| 28 | + undefined, |
| 29 | + undefined, |
| 30 | + path.resolve(dirname, 'streamingUploads.config.ts'), |
| 31 | + )) |
| 32 | + TEST_CONTAINER = process.env.AZURE_STORAGE_CONTAINER_NAME! |
| 33 | + |
| 34 | + const blobServiceClient = BlobServiceClient.fromConnectionString( |
| 35 | + process.env.AZURE_STORAGE_CONNECTION_STRING!, |
| 36 | + ) |
| 37 | + client = blobServiceClient.getContainerClient(TEST_CONTAINER) |
| 38 | + |
| 39 | + await client.createIfNotExists() |
| 40 | + await clearContainer() |
| 41 | + }, 90000) |
| 42 | + |
| 43 | + afterAll(async () => { |
| 44 | + await payload.destroy() |
| 45 | + }) |
| 46 | + |
| 47 | + afterEach(async () => { |
| 48 | + await clearContainer() |
| 49 | + }) |
| 50 | + |
| 51 | + it('preserves mime type when uploaded via rest endpoint', async () => { |
| 52 | + const fileBuffer = await readFile(path.resolve(dirname, '../uploads/image.png')) |
| 53 | + |
| 54 | + const data = new FormData() |
| 55 | + data.append('file', new Blob([fileBuffer], { type: 'image/png' }), 'image1.png') |
| 56 | + const newMedia: { doc: { url: string } } = await ( |
| 57 | + await restClient.POST('/media', { |
| 58 | + body: data, |
| 59 | + }) |
| 60 | + ).json() |
| 61 | + const response = await restClient.GET(newMedia.doc.url.replace(/^\/api/, '') as `/${string}`) |
| 62 | + expect(response.headers.get('content-type')).toEqual('image/png') |
| 63 | + }) |
| 64 | + |
| 65 | + it('can upload', async () => { |
| 66 | + const upload = await payload.create({ |
| 67 | + collection: mediaSlug, |
| 68 | + data: {}, |
| 69 | + filePath: path.resolve(dirname, '../uploads/image.png'), |
| 70 | + }) |
| 71 | + |
| 72 | + expect(upload.id).toBeTruthy() |
| 73 | + await verifyUploads({ collectionSlug: mediaSlug, uploadId: upload.id }) |
| 74 | + expect(upload.url).toEqual(`/api/${mediaSlug}/file/${String(upload.filename)}`) |
| 75 | + }) |
| 76 | + |
| 77 | + it('can upload with prefix', async () => { |
| 78 | + const upload = await payload.create({ |
| 79 | + collection: mediaWithPrefixSlug, |
| 80 | + data: {}, |
| 81 | + filePath: path.resolve(dirname, '../uploads/image.png'), |
| 82 | + }) |
| 83 | + |
| 84 | + expect(upload.id).toBeTruthy() |
| 85 | + await verifyUploads({ |
| 86 | + collectionSlug: mediaWithPrefixSlug, |
| 87 | + uploadId: upload.id, |
| 88 | + prefix, |
| 89 | + }) |
| 90 | + expect(upload.url).toEqual(`/api/${mediaWithPrefixSlug}/file/${String(upload.filename)}`) |
| 91 | + }) |
| 92 | + |
| 93 | + it('returns 404 for non-existing file', async () => { |
| 94 | + const response = await restClient.GET(`/${mediaSlug}/file/nonexistent.png`) |
| 95 | + expect(response.status).toBe(404) |
| 96 | + }) |
| 97 | + |
| 98 | + async function clearContainer() { |
| 99 | + for await (const blob of client.listBlobsFlat()) { |
| 100 | + await client.deleteBlob(blob.name) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + async function verifyUploads({ |
| 105 | + collectionSlug, |
| 106 | + uploadId, |
| 107 | + prefix = '', |
| 108 | + }: { |
| 109 | + collectionSlug: CollectionSlug |
| 110 | + prefix?: string |
| 111 | + uploadId: number | string |
| 112 | + }) { |
| 113 | + const uploadData = (await payload.findByID({ |
| 114 | + collection: collectionSlug, |
| 115 | + id: uploadId, |
| 116 | + })) as unknown as { filename: string; sizes: Record<string, { filename: string }> } |
| 117 | + |
| 118 | + const fileKeys = Object.keys(uploadData.sizes || {}).map((key) => { |
| 119 | + const rawFilename = uploadData.sizes[key].filename |
| 120 | + return prefix ? `${prefix}/${rawFilename}` : rawFilename |
| 121 | + }) |
| 122 | + |
| 123 | + fileKeys.push(`${prefix ? `${prefix}/` : ''}${uploadData.filename}`) |
| 124 | + |
| 125 | + for (const key of fileKeys) { |
| 126 | + const blobClient = client.getBlobClient(key) |
| 127 | + try { |
| 128 | + const props = await blobClient.getProperties() |
| 129 | + expect(props).toBeDefined() |
| 130 | + expect(props.contentLength).toBeGreaterThan(0) |
| 131 | + } catch (error) { |
| 132 | + console.error('Error verifying uploads:', key, error) |
| 133 | + throw error |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +}) |
0 commit comments