Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.

Commit 2cd83f2

Browse files
committed
merge
2 parents 0685717 + d318e22 commit 2cd83f2

24 files changed

Lines changed: 174 additions & 54 deletions

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,18 @@ However, you can create your own adapter for any third-party service you would l
4545

4646
This plugin is configurable to work across many different Payload collections. A `*` denotes that the property is required.
4747

48-
| Option | Description |
49-
| ----------------------- | ----------- |
50-
| `collections` * | Object with keys set to the slug of collections you want to enable the plugin for, and values set to collection-specific options. |
48+
| Option | Type | Description |
49+
|-------------------------|-----------------------------------------| ----------- |
50+
| `collections` * | Record<string, [CollectionOptions](https://github.com/payloadcms/plugin-cloud-storage/blob/c4a492a62abc2f21b4cd6a7c97778acd8e831212/src/types.ts#L48)> | Object with keys set to the slug of collections you want to enable the plugin for, and values set to collection-specific options. |
5151

5252
**Collection-specific options:**
5353

54-
| Option | Description |
55-
|------------------------------|---------------------------------|
56-
| `adapter` * | Pass in the adapter that you'd like to use for this collection. You can also set this field to `null` for local development if you'd like to bypass cloud storage in certain scenarios and use local storage. |
57-
| `disableLocalStorage` | Choose to disable local storage on this collection. Defaults to `true`. |
58-
| `disablePayloadAccessControl` | Set to `true` to disable Payload's access control. [More](#payload-access-control) |
54+
| Option | Type | Description |
55+
|-------------------------------|-------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------|
56+
| `adapter` * | [Adapter](https://github.com/payloadcms/plugin-cloud-storage/blob/c4a492a62abc2f21b4cd6a7c97778acd8e831212/src/types.ts#L46) | Pass in the adapter that you'd like to use for this collection. You can also set this field to `null` for local development if you'd like to bypass cloud storage in certain scenarios and use local storage. |
57+
| `disableLocalStorage` | `boolean` | Choose to disable local storage on this collection. Defaults to `true`. |
58+
| `disablePayloadAccessControl` | `true` | Set to `true` to disable Payload's access control. [More](#payload-access-control) |
59+
| `prefix` | `string` | Set to `media/images` to upload files inside `media/images` folder in the bucket. |
5960

6061
### Azure Blob Storage Adapter
6162

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@payloadcms/plugin-cloud-storage",
33
"description": "The official cloud storage plugin for Payload CMS",
4-
"version": "1.0.7",
4+
"version": "1.0.8",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"license": "SEE LICENSE IN LICENSE.MD",
@@ -10,6 +10,7 @@
1010
"lint": "eslint src",
1111
"lint:fix": "eslint --fix --ext .ts,.tsx src"
1212
},
13+
"prepublishOnly": "yarn build",
1314
"peerDependencies": {
1415
"@aws-sdk/client-s3": "^3.142.0",
1516
"@azure/storage-blob": "^12.11.0",

src/adapters/azure/generateURL.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path'
12
import type { GenerateURL } from '../../types'
23

34
interface Args {
@@ -7,6 +8,6 @@ interface Args {
78

89
export const getGenerateURL =
910
({ containerName, baseURL }: Args): GenerateURL =>
10-
({ filename }) => {
11-
return `${baseURL}/${containerName}/${filename}`
11+
({ filename, prefix = '' }) => {
12+
return `${baseURL}/${containerName}/${path.posix.join(prefix, filename)}`
1213
}

src/adapters/azure/handleDelete.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path'
12
import type { CollectionConfig } from 'payload/types'
23
import type { ContainerClient } from '@azure/storage-blob'
34
import type { HandleDelete } from '../../types'
@@ -8,8 +9,8 @@ interface Args {
89
}
910

1011
export const getHandleDelete = ({ containerClient }: Args): HandleDelete => {
11-
return async ({ filename }) => {
12-
const blockBlobClient = containerClient.getBlockBlobClient(filename)
12+
return async ({ filename, doc: { prefix = '' } }) => {
13+
const blockBlobClient = containerClient.getBlockBlobClient(path.posix.join(prefix, filename))
1314
await blockBlobClient.deleteIfExists()
1415
}
1516
}

src/adapters/azure/handleUpload.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path'
12
import type { ContainerClient } from '@azure/storage-blob'
23
import type { CollectionConfig } from 'payload/types'
34
import type { HandleUpload } from '../../types'
@@ -6,16 +7,22 @@ interface Args {
67
collection: CollectionConfig
78
containerClient: ContainerClient
89
allowContainerCreate: boolean
10+
prefix?: string
911
}
1012

11-
export const getHandleUpload = ({ allowContainerCreate, containerClient }: Args): HandleUpload => {
13+
export const getHandleUpload = ({
14+
allowContainerCreate,
15+
containerClient,
16+
prefix = '',
17+
}: Args): HandleUpload => {
1218
if (allowContainerCreate) {
1319
containerClient.createIfNotExists({ access: 'blob' })
1420
}
1521

1622
return async ({ data, file }) => {
17-
const blobName = file.filename
18-
const blockBlobClient = containerClient.getBlockBlobClient(blobName)
23+
const blockBlobClient = containerClient.getBlockBlobClient(
24+
path.posix.join(prefix, file.filename),
25+
)
1926

2027
await blockBlobClient.upload(file.buffer, file.buffer.byteLength, {
2128
blobHTTPHeaders: { blobContentType: file.mimeType },

src/adapters/azure/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface Args {
1515

1616
export const azureBlobStorageAdapter =
1717
({ connectionString, containerName, baseURL, allowContainerCreate }: Args): Adapter =>
18-
({ collection }): GeneratedAdapter => {
18+
({ collection, prefix }): GeneratedAdapter => {
1919
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString)
2020
const containerClient = blobServiceClient.getContainerClient(containerName)
2121

@@ -24,10 +24,11 @@ export const azureBlobStorageAdapter =
2424
collection,
2525
containerClient,
2626
allowContainerCreate,
27+
prefix,
2728
}),
2829
handleDelete: getHandleDelete({ collection, containerClient }),
2930
generateURL: getGenerateURL({ containerName, baseURL }),
30-
staticHandler: getHandler({ containerClient }),
31+
staticHandler: getHandler({ containerClient, collection }),
3132
webpack: extendWebpackConfig,
3233
}
3334
}

src/adapters/azure/staticHandler.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
import path from 'path'
12
import type { ContainerClient } from '@azure/storage-blob'
3+
import type { CollectionConfig } from 'payload/types'
24
import type { StaticHandler } from '../../types'
5+
import { getFilePrefix } from '../../utilities/getFilePrefix'
36

47
interface Args {
58
containerClient: ContainerClient
9+
collection: CollectionConfig
610
}
711

8-
export const getHandler = ({ containerClient }: Args): StaticHandler => {
12+
export const getHandler = ({ containerClient, collection }: Args): StaticHandler => {
913
return async (req, res, next) => {
1014
try {
11-
const blockBlobClient = containerClient.getBlockBlobClient(req.params.filename)
15+
const prefix = await getFilePrefix({ req, collection })
16+
const blockBlobClient = containerClient.getBlockBlobClient(
17+
path.posix.join(prefix, req.params.filename),
18+
)
1219

1320
const blob = await blockBlobClient.download(0)
1421

src/adapters/gcs/generateURL.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path'
12
import { Storage } from '@google-cloud/storage'
23
import type { GenerateURL } from '../../types'
34

@@ -8,6 +9,8 @@ interface Args {
89

910
export const getGenerateURL =
1011
({ gcs, bucket }: Args): GenerateURL =>
11-
({ filename }) => {
12-
return gcs.bucket(bucket).file(filename).publicUrl()
12+
({ filename, prefix = '' }) => {
13+
return decodeURIComponent(
14+
gcs.bucket(bucket).file(path.posix.join(prefix, filename)).publicUrl(),
15+
)
1316
}

src/adapters/gcs/handleDelete.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path'
12
import { Storage } from '@google-cloud/storage'
23
import type { HandleDelete } from '../../types'
34

@@ -7,8 +8,8 @@ interface Args {
78
}
89

910
export const getHandleDelete = ({ gcs, bucket }: Args): HandleDelete => {
10-
return async ({ filename }) => {
11-
await gcs.bucket(bucket).file(filename).delete({
11+
return async ({ filename, doc: { prefix = '' } }) => {
12+
await gcs.bucket(bucket).file(path.posix.join(prefix, filename)).delete({
1213
ignoreNotFound: true,
1314
})
1415
}

src/adapters/gcs/handleUpload.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path'
12
import type { Storage } from '@google-cloud/storage'
23
import type { CollectionConfig } from 'payload/types'
34
import type { HandleUpload } from '../../types'
@@ -6,12 +7,13 @@ interface Args {
67
collection: CollectionConfig
78
bucket: string
89
acl?: 'Private' | 'Public'
10+
prefix?: string
911
gcs: Storage
1012
}
1113

12-
export const getHandleUpload = ({ gcs, bucket, acl }: Args): HandleUpload => {
14+
export const getHandleUpload = ({ gcs, bucket, acl, prefix = '' }: Args): HandleUpload => {
1315
return async ({ data, file }) => {
14-
const gcsFile = gcs.bucket(bucket).file(file.filename)
16+
const gcsFile = gcs.bucket(bucket).file(path.posix.join(prefix, file.filename))
1517
await gcsFile.save(file.buffer, {
1618
metadata: {
1719
contentType: file.mimeType,

0 commit comments

Comments
 (0)