-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfile-storage.zod.ts
More file actions
400 lines (355 loc) · 12.1 KB
/
Copy pathfile-storage.zod.ts
File metadata and controls
400 lines (355 loc) · 12.1 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
import {
ConnectorSchema,
} from '../connector.zod';
/**
* File Storage Connector Protocol Template
*
* Specialized connector for file storage systems (S3, Azure Blob, Google Cloud Storage, etc.)
* Extends the base connector with file-specific features like multipart uploads,
* versioning, and metadata extraction.
*/
/**
* File Storage Provider Types
*/
import { lazySchema } from '../../shared/lazy-schema';
export const FileStorageProviderSchema = lazySchema(() => z.enum([
's3', // Amazon S3
'azure_blob', // Azure Blob Storage
'gcs', // Google Cloud Storage
'dropbox', // Dropbox
'box', // Box
'onedrive', // Microsoft OneDrive
'google_drive', // Google Drive
'sharepoint', // SharePoint
'ftp', // FTP/SFTP
'local', // Local file system
'custom', // Custom file storage
]).describe('File storage provider type'));
export type FileStorageProvider = z.infer<typeof FileStorageProviderSchema>;
/**
* File Access Pattern
*/
export const FileAccessPatternSchema = lazySchema(() => z.enum([
'public_read', // Public read access
'private', // Private access
'authenticated_read', // Requires authentication
'bucket_owner_read', // Bucket owner has read access
'bucket_owner_full', // Bucket owner has full control
]).describe('File access pattern'));
export type FileAccessPattern = z.infer<typeof FileAccessPatternSchema>;
/**
* File Metadata Configuration
*/
export const FileMetadataConfigSchema = lazySchema(() => z.object({
extractMetadata: z.boolean().default(true).describe('Extract file metadata'),
metadataFields: z.array(z.enum([
'content_type',
'file_size',
'last_modified',
'etag',
'checksum',
'creator',
'created_at',
'custom',
])).optional().describe('Metadata fields to extract'),
customMetadata: z.record(z.string(), z.string()).optional().describe('Custom metadata key-value pairs'),
}));
export type FileMetadataConfig = z.infer<typeof FileMetadataConfigSchema>;
/**
* Multipart Upload Configuration
*/
export const MultipartUploadConfigSchema = lazySchema(() => z.object({
enabled: z.boolean().default(true).describe('Enable multipart uploads'),
partSize: z.number().min(5 * 1024 * 1024).default(5 * 1024 * 1024).describe('Part size in bytes (min 5MB)'),
maxConcurrentParts: z.number().min(1).max(10).default(5).describe('Maximum concurrent part uploads'),
threshold: z.number().min(5 * 1024 * 1024).default(100 * 1024 * 1024).describe('File size threshold for multipart upload in bytes'),
}));
export type MultipartUploadConfig = z.infer<typeof MultipartUploadConfigSchema>;
/**
* File Versioning Configuration
*/
export const FileVersioningConfigSchema = lazySchema(() => z.object({
enabled: z.boolean().default(false).describe('Enable file versioning'),
maxVersions: z.number().min(1).max(100).optional().describe('Maximum versions to retain'),
retentionDays: z.number().min(1).optional().describe('Version retention period in days'),
}));
export type FileVersioningConfig = z.infer<typeof FileVersioningConfigSchema>;
/**
* File Filter Configuration
*/
export const FileFilterConfigSchema = lazySchema(() => z.object({
includePatterns: z.array(z.string()).optional().describe('File patterns to include (glob)'),
excludePatterns: z.array(z.string()).optional().describe('File patterns to exclude (glob)'),
minFileSize: z.number().min(0).optional().describe('Minimum file size in bytes'),
maxFileSize: z.number().min(1).optional().describe('Maximum file size in bytes'),
allowedExtensions: z.array(z.string()).optional().describe('Allowed file extensions'),
blockedExtensions: z.array(z.string()).optional().describe('Blocked file extensions'),
}));
export type FileFilterConfig = z.infer<typeof FileFilterConfigSchema>;
/**
* File Storage Bucket/Container Configuration
*/
export const StorageBucketSchema = lazySchema(() => z.object({
name: z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Bucket identifier in ObjectStack (snake_case)'),
label: z.string().describe('Display label'),
bucketName: z.string().describe('Actual bucket/container name in storage system'),
region: z.string().optional().describe('Storage region'),
enabled: z.boolean().default(true).describe('Enable sync for this bucket'),
prefix: z.string().optional().describe('Prefix/path within bucket'),
accessPattern: FileAccessPatternSchema.optional().describe('Access pattern'),
fileFilters: FileFilterConfigSchema.optional().describe('File filter configuration'),
}));
export type StorageBucket = z.infer<typeof StorageBucketSchema>;
/**
* File Storage Connector Configuration Schema
*/
export const FileStorageConnectorSchema = lazySchema(() => ConnectorSchema.extend({
type: z.literal('file_storage'),
/**
* File storage provider
*/
provider: FileStorageProviderSchema.describe('File storage provider type'),
/**
* Storage configuration
*/
storageConfig: z.object({
endpoint: z.string().url().optional().describe('Custom endpoint URL'),
region: z.string().optional().describe('Default region'),
pathStyle: z.boolean().optional().default(false).describe('Use path-style URLs (for S3-compatible)'),
}).optional().describe('Storage configuration'),
/**
* Buckets/containers to sync
*/
buckets: z.array(StorageBucketSchema).describe('Buckets/containers to sync'),
/**
* File metadata configuration
*/
metadataConfig: FileMetadataConfigSchema.optional().describe('Metadata extraction configuration'),
/**
* Multipart upload configuration
*/
multipartConfig: MultipartUploadConfigSchema.optional().describe('Multipart upload configuration'),
/**
* File versioning configuration
*/
versioningConfig: FileVersioningConfigSchema.optional().describe('File versioning configuration'),
/**
* Enable server-side encryption
*/
encryption: z.object({
enabled: z.boolean().default(false).describe('Enable server-side encryption'),
algorithm: z.enum(['AES256', 'aws:kms', 'custom']).optional().describe('Encryption algorithm'),
kmsKeyId: z.string().optional().describe('KMS key ID (for aws:kms)'),
}).optional().describe('Encryption configuration'),
/**
* Lifecycle policy
*/
lifecyclePolicy: z.object({
enabled: z.boolean().default(false).describe('Enable lifecycle policy'),
deleteAfterDays: z.number().min(1).optional().describe('Delete files after N days'),
archiveAfterDays: z.number().min(1).optional().describe('Archive files after N days'),
}).optional().describe('Lifecycle policy'),
/**
* Content processing configuration
*/
contentProcessing: z.object({
extractText: z.boolean().default(false).describe('Extract text from documents'),
generateThumbnails: z.boolean().default(false).describe('Generate image thumbnails'),
thumbnailSizes: z.array(z.object({
width: z.number().min(1),
height: z.number().min(1),
})).optional().describe('Thumbnail sizes'),
virusScan: z.boolean().default(false).describe('Scan for viruses'),
}).optional().describe('Content processing configuration'),
/**
* Download/upload buffer size
*/
bufferSize: z.number().min(1024).default(64 * 1024).describe('Buffer size in bytes'),
/**
* Enable transfer acceleration (for supported providers)
*/
transferAcceleration: z.boolean().default(false).describe('Enable transfer acceleration'),
}));
export type FileStorageConnector = z.infer<typeof FileStorageConnectorSchema>;
// ============================================================================
// Helper Functions & Examples
// ============================================================================
/**
* Example: Amazon S3 Connector Configuration
*/
export const s3ConnectorExample = {
name: 's3_production_assets',
label: 'Production S3 Assets',
type: 'file_storage',
provider: 's3',
authentication: {
type: 'api_key',
apiKey: '${AWS_ACCESS_KEY_ID}:${AWS_SECRET_ACCESS_KEY}',
headerName: 'Authorization',
},
storageConfig: {
region: 'us-east-1',
pathStyle: false,
},
buckets: [
{
name: 'product_images',
label: 'Product Images',
bucketName: 'my-company-product-images',
region: 'us-east-1',
enabled: true,
prefix: 'products/',
accessPattern: 'public_read',
fileFilters: {
allowedExtensions: ['.jpg', '.jpeg', '.png', '.webp'],
maxFileSize: 10 * 1024 * 1024, // 10MB
},
},
{
name: 'customer_documents',
label: 'Customer Documents',
bucketName: 'my-company-customer-docs',
region: 'us-east-1',
enabled: true,
accessPattern: 'private',
fileFilters: {
allowedExtensions: ['.pdf', '.docx', '.xlsx'],
maxFileSize: 50 * 1024 * 1024, // 50MB
},
},
],
metadataConfig: {
extractMetadata: true,
metadataFields: ['content_type', 'file_size', 'last_modified', 'etag'],
},
multipartConfig: {
enabled: true,
partSize: 5 * 1024 * 1024, // 5MB
maxConcurrentParts: 5,
threshold: 100 * 1024 * 1024, // 100MB
},
versioningConfig: {
enabled: true,
maxVersions: 10,
},
encryption: {
enabled: true,
algorithm: 'aws:kms',
kmsKeyId: '${AWS_KMS_KEY_ID}',
},
contentProcessing: {
extractText: true,
generateThumbnails: true,
thumbnailSizes: [
{ width: 150, height: 150 },
{ width: 300, height: 300 },
{ width: 600, height: 600 },
],
virusScan: true,
},
syncConfig: {
strategy: 'incremental',
direction: 'bidirectional',
realtimeSync: true,
conflictResolution: 'latest_wins',
batchSize: 100,
},
transferAcceleration: true,
status: 'active',
enabled: true,
};
/**
* Example: Google Drive Connector Configuration
*/
export const googleDriveConnectorExample = {
name: 'google_drive_team',
label: 'Google Drive Team Folder',
type: 'file_storage',
provider: 'google_drive',
authentication: {
type: 'oauth2',
clientId: '${GOOGLE_CLIENT_ID}',
clientSecret: '${GOOGLE_CLIENT_SECRET}',
authorizationUrl: 'https://accounts.google.com/o/oauth2/v2/auth',
tokenUrl: 'https://oauth2.googleapis.com/token',
grantType: 'authorization_code',
scopes: ['https://www.googleapis.com/auth/drive.file'],
},
buckets: [
{
name: 'team_drive',
label: 'Team Drive',
bucketName: 'shared-team-drive',
enabled: true,
fileFilters: {
excludePatterns: ['*.tmp', '~$*'],
},
},
],
metadataConfig: {
extractMetadata: true,
metadataFields: ['content_type', 'file_size', 'last_modified', 'creator', 'created_at'],
},
versioningConfig: {
enabled: true,
maxVersions: 5,
},
syncConfig: {
strategy: 'incremental',
direction: 'bidirectional',
realtimeSync: true,
conflictResolution: 'latest_wins',
batchSize: 50,
},
status: 'active',
enabled: true,
};
/**
* Example: Azure Blob Storage Connector Configuration
*/
export const azureBlobConnectorExample = {
name: 'azure_blob_storage',
label: 'Azure Blob Storage',
type: 'file_storage',
provider: 'azure_blob',
authentication: {
type: 'api_key',
apiKey: '${AZURE_STORAGE_ACCOUNT_KEY}',
headerName: 'x-ms-blob-type',
},
storageConfig: {
endpoint: 'https://myaccount.blob.core.windows.net',
},
buckets: [
{
name: 'archive_container',
label: 'Archive Container',
bucketName: 'archive',
enabled: true,
accessPattern: 'private',
},
],
metadataConfig: {
extractMetadata: true,
metadataFields: ['content_type', 'file_size', 'last_modified', 'etag'],
},
encryption: {
enabled: true,
algorithm: 'AES256',
},
lifecyclePolicy: {
enabled: true,
archiveAfterDays: 90,
deleteAfterDays: 365,
},
syncConfig: {
strategy: 'incremental',
direction: 'import',
schedule: '0 1 * * *', // Daily at 1 AM
batchSize: 200,
},
status: 'active',
enabled: true,
};