-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconfig.ts
More file actions
440 lines (424 loc) · 13.5 KB
/
config.ts
File metadata and controls
440 lines (424 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import type { MediaPreviewProps } from '@/fields/index.js'
import type { CollectionOptions } from '@payloadcms/plugin-cloud-storage/types'
import type { AcceptedLanguages } from '@payloadcms/translations'
import type { CollectionConfig, PayloadRequest, Plugin, TaskConfig, UploadCollectionSlug } from 'payload'
import type { StreamTusAuthRequest } from './core.js'
export type UrlTransformFunction = (args: {
/** Base URL */
baseUrl: string
/** Collection configuration */
collection: CollectionConfig
/** Document data (for streams contains bunnyVideoId) */
data?: Record<string, unknown>
/** Base filename */
filename: string
/** File prefix/path */
prefix?: string
}) => string
export type UrlTransformConfig =
| {
/**
* Append timestamp to the URL
* @default false (true for admin thumbnails)
*/
appendTimestamp?: boolean
/**
* Static query parameters to append to the URL
* Works together with appendTimestamp
*/
queryParams?: Record<string, string>
transformUrl?: never
}
| {
/**
* Custom transform function for complete URL control
*/
transformUrl: UrlTransformFunction
}
export type InsertPosition =
| 'first'
| 'last'
| { after: string; before?: never }
| { after?: never; before: string }
export type ThumbnailConfig = {
/**
* Use a specific size from upload collection's sizes instead of original file
* Only works for image uploads that have sizes configured
*/
sizeName?: string
/**
* Enable animated preview (WebP) instead of static thumbnail for Bunny Stream videos.
* When enabled, uses preview.webp instead of thumbnail.jpg for video thumbnails.
* Only works when stream configuration is enabled for the collection.
* @default false
*/
streamAnimated?: boolean
} & UrlTransformConfig
export type MediaPreviewConfig = {
/**
* Position where to insert mediaPreviewField.
* Supports dot notation for nested fields (e.g., 'meta.title' or 'tabs.0.content').
*
* @example
* ```typescript
* mediaPreview: {
* position: 'first' // Insert at the very beginning
* }
*
* mediaPreview: {
* position: 'last' // Insert at the end (default)
* }
*
* mediaPreview: {
* position: { after: 'filename' } // Insert after filename field
* }
*
* mediaPreview: {
* position: { before: 'title' } // Insert before title field
* }
*
* mediaPreview: {
* position: { after: 'meta.description' } // Insert in meta group after description
* }
* ```
*
* @default 'last'
*/
position?: InsertPosition
} & MediaPreviewProps
export type PurgeConfig = {
/**
* @deprecated Use global `apiKey` instead. This option will be removed in v2.3.0.
* Bunny Account API key (AccessKey) for CDN cache purging operations. If not specified, uses the global apiKey.
*/
apiKey?: string
/**
* Wait for purge to complete before continuing
* @default false
*/
async?: boolean
}
export type StorageConfig = {
/** Bunny Storage API key */
apiKey: string
/** CDN domain from your Pull Zone (e.g., 'example.b-cdn.net') */
hostname: string
/** Storage region code (optional, defaults to primary region) */
region?: 'br' | 'jh' | 'la' | 'ny' | 'se' | 'sg' | 'syd' | 'uk' | ({} & string)
/** Security key for signing storage URLs. Used to generate signed URLs for secure file access */
tokenSecurityKey?: string
/**
* Upload timeout in milliseconds
* @default 120000
*/
uploadTimeout?: number
/** Storage zone name from your Bunny Storage settings */
zoneName: string
}
export type StreamTusConfig = {
/**
* Automatically enable TUS mode when file MIME type is supported.
* When enabled, hides the toggle button for switching between standard and TUS upload modes.
* @default true
*/
autoMode?: boolean
/**
* Custom authorization check for TUS API endpoints.
*
* By default, checks if user has admin access and create access to at least one collection
* configured in the plugin.
*/
checkAccess?: (req: PayloadRequest,body:StreamTusAuthRequest) => boolean | Promise<boolean>
/**
* @deprecated Use stream.mimeTypes instead. This option will be removed in v2.3.0.
* Video and audio file types allowed for TUS uploads.
* Moved to stream.mimeTypes for better organization.
*/
mimeTypes?: string[]
/**
* Time in seconds for TUS upload session to expire
* @default 3600
*/
uploadTimeout?: number
}
export type StreamConfig = {
/** Bunny Stream API key */
apiKey: string
/**
* Automatic cleanup of incomplete uploads that failed or were abandoned
*/
cleanup?: {
/**
* Time in seconds after which incomplete uploads are considered dead
* @default 86400
*/
maxAge?: number
/**
* Cron schedule configuration for cleanup task
* @default { cron: '0 2 * * *', queue: 'storage-bunny' }
*/
schedule?: Exclude<TaskConfig['schedule'], undefined>[0]
} | boolean
/** Stream CDN domain (e.g., 'vz-example-123.b-cdn.net') */
hostname: string
/** Video library ID from your Bunny Stream settings */
libraryId: number
/**
* Video and audio file types that should use Bunny Stream. Defaults include:
* - video/mp4 (mp4, m4p, m4v)
* - video/x-matroska (mkv)
* - video/webm (webm)
* - video/x-flv (flv)
* - video/x-ms-vod (vod)
* - video/x-msvideo (avi)
* - video/quicktime (mov)
* - video/x-ms-wmv (wmv)
* - video/x-amv (amv)
* - video/mpeg (mpeg, mpg)
* - video/4mv (4mv)
* - video/mp2t (ts)
* - video/mxf (mxf)
* - audio/mpeg (mp3)
* - audio/ogg (ogg)
* - audio/wav (wav)
*
* Collection mimeTypes settings override these stream settings.
* If you allow a format here but block it in your collection config,
* the collection setting wins.
*/
mimeTypes?: string[]
/**
* Enable MP4 downloads (required when using Payload access control, unless signed URLs with redirect are enabled)
* @default false
*/
mp4Fallback?: boolean
/**
* Default thumbnail time in milliseconds for Bunny Stream videos.
* Specifies which moment in videos to capture as thumbnail.
* Can be overridden per collection. Use with thumbnail: true to display thumbnails.
*/
thumbnailTime?: number
/** Security key for signing stream URLs. Used to generate signed URLs for secure video access */
tokenSecurityKey?: string
/** Enable TUS resumable uploads for large video files */
tus?: boolean | StreamTusConfig
/**
* Upload timeout in milliseconds
* @default 300000
*/
uploadTimeout?: number
/**
* Webhook configuration for receiving video status updates from Bunny Stream.
* When enabled, creates an endpoint at: /api/storage-bunny/stream/webhook?secret=<your-secret>
*
* Configure this URL with secret query parameter in your Bunny Stream library settings.
*/
webhook?: {
/**
* Secret for webhook authentication.
* Must match the secret in your Bunny Stream webhook URL query parameter.
* Example webhook URL: https://your-site.com/api/storage-bunny/stream/webhook?secret=my-secret-123
*/
secret: string
}
}
export type StaticHandlerConfig = {
/**
* Link expiration time in seconds for redirect URLs
* If not specified, uses the main expiresIn value from SignedUrlsConfig
*
* Useful for setting shorter expiration for redirects vs direct signed URLs
*/
expiresIn?: number
/**
* HTTP status code for redirects
* @default 302
*/
redirectStatus?: 301 | 302 | 307 | 308
/**
* Redirect to signed URL instead of proxying content through Payload
*
* When enabled, static handler responds with HTTP redirect instead of streaming content.
* Only works when enablePayloadAccessControl is true.
*
* @default false
*/
useRedirect?: boolean
}
export type SignedUrlsConfig = {
/** Allowed countries (ISO 3166-1 alpha-2 codes). Only requests from these countries will be allowed */
allowedCountries?: string[]
/** Blocked countries (ISO 3166-1 alpha-2 codes). Requests from these countries will be rejected */
blockedCountries?: string[]
/**
* Link expiration time in seconds
* @default 7200
*/
expiresIn?: number
/** Custom function to determine if a file should use signed URLs */
shouldUseSignedUrl?(args: {
collection: CollectionConfig
filename: string
}): boolean
/**
* Static handler behavior when Payload access control is enabled
* Has no effect when disablePayloadAccessControl is true
*/
staticHandler?: StaticHandlerConfig
}
export type BunnyStorageCollectionConfig = {
/**
* Override global media preview config for this collection.
* Set to false to disable media preview for this collection.
*/
mediaPreview?: boolean | MediaPreviewConfig
/**
* Override global CDN cache purging config for this collection.
* Set to false to disable cache purging for this collection.
*/
purge?: boolean | Partial<PurgeConfig>
/** Override global signed URLs config for this collection */
signedUrls?: boolean | SignedUrlsConfig
/**
* Storage settings for this collection.
* Set to false to disable Bunny Storage uploads for this collection.
* When disabled, only stream can be used.
*/
storage?: {
/**
* Override upload timeout in milliseconds for this collection
*/
uploadTimeout?: number
} | false
/**
* Stream settings for this collection.
* Set to false to disable Bunny Stream uploads for this collection.
* When disabled, only storage can be used.
*/
stream?: {
/**
* Enable media preview in admin panel.
* Adds a virtual field showing preview for videos, images, and documents.
* @default false
*/
mediaPreview?: boolean
/**
* Override allowed MIME types for Bunny Stream uploads in this collection.
* Replaces the global stream.mimeTypes setting for this collection.
*/
mimeTypes?: string[]
/**
* Override MP4 fallback setting for this collection
*/
mp4Fallback?: boolean
/**
* Override default thumbnail time in milliseconds for Bunny Stream videos.
* Specifies which moment in the video to capture as thumbnail.
* Use with thumbnail: true to display the thumbnail in admin and API responses.
*/
thumbnailTime?: number
/**
* Override TUS resumable uploads config for this collection
*/
tus?: {
/**
* Override TUS upload timeout in seconds for this collection
*/
uploadTimeout?: number
}
/**
* Override upload timeout in milliseconds for this collection
*/
uploadTimeout?: number
} | false
/**
* Enable thumbnail display in admin panel and thumbnailURL field in API responses.
*
* For Bunny Stream videos: combines with stream.thumbnailTime to show video thumbnails.
* For images: can specify sizeName to use a particular image size as thumbnail.
*
* Uses Payload's adminThumbnail mechanism internally.
*/
thumbnail?: boolean | ThumbnailConfig
/**
* Override global URL transformation config for this collection
* @note Does not work when `disablePayloadAccessControl` is true
*/
urlTransform?: UrlTransformConfig
} & Omit<CollectionOptions, 'adapter'>
/** Configuration for which collections use Bunny Storage */
export type CollectionsConfig = Partial<
Record<
UploadCollectionSlug,
| BunnyStorageCollectionConfig
| true
>
>
type BunnyStorageBaseConfig = {
/**
* Bunny Account API key (AccessKey) for account-level operations.
* Required for CDN cache purging feature.
*/
apiKey?: string
/** Which collections should use Bunny Storage */
collections: CollectionsConfig
/**
* Enable or disable the plugin
* @default true
*/
enabled?: boolean
/** Experimental features that may change in future versions */
experimental?: Record<string, never>
/** Internationalization settings for UI elements */
i18n?: {
translations: {
[key in AcceptedLanguages]?: {
mediaPreviewClose?: string
mediaPreviewOpen?: string
tusUploadDisableMode?: string
tusUploadEnableMode?: string
}
}
}
/**
* Enable media preview in admin panel for uploads.
* Supports videos (Bunny Stream and regular files), images, and documents.
* Can be overridden per collection.
* @default false
*/
mediaPreview?: boolean | MediaPreviewConfig
/** CDN cache purging configuration */
purge?: boolean | PurgeConfig
/** Global signed URLs config (can be overridden per collection) */
signedUrls?: boolean | SignedUrlsConfig
/**
* Global thumbnail settings for all collections.
*
* Enables thumbnail display in admin panel and thumbnailURL field in API responses.
* For Bunny Stream videos: works with stream.thumbnailTime setting.
* For images: can specify sizeName to use a particular image size.
*
* Uses Payload's adminThumbnail mechanism internally.
*/
thumbnail?: boolean | ThumbnailConfig
/**
* Global URL transformation config for all collections (can be overridden per collection)
* @note Does not work when `disablePayloadAccessControl` is true for the collection
*/
urlTransform?: UrlTransformConfig
}
export type BunnyStorageConfig = (
| {
/** Bunny Storage configuration */
storage: StorageConfig
/** Bunny Stream configuration */
stream?: StreamConfig
}
| {
/** Bunny Storage configuration */
storage?: StorageConfig
/** Bunny Stream configuration */
stream: StreamConfig
}
) & BunnyStorageBaseConfig
export type BunnyStoragePlugin = (pluginConfig: BunnyStorageConfig) => Plugin