@opensaas/stack-storage 0.39.2 → 0.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +6 -0
- package/dist/config/types.d.ts +3 -47
- package/dist/config/types.d.ts.map +1 -1
- package/dist/fields/index.d.ts +2 -16
- package/dist/fields/index.d.ts.map +1 -1
- package/dist/fields/index.js +13 -47
- package/dist/fields/index.js.map +1 -1
- package/dist/providers/local.d.ts +0 -10
- package/dist/providers/local.d.ts.map +1 -1
- package/dist/providers/local.js +0 -12
- package/dist/providers/local.js.map +1 -1
- package/dist/runtime/index.d.ts +0 -6
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +0 -21
- package/dist/runtime/index.js.map +1 -1
- package/dist/utils/image.d.ts +0 -10
- package/dist/utils/image.d.ts.map +1 -1
- package/dist/utils/image.js +0 -16
- package/dist/utils/image.js.map +1 -1
- package/dist/utils/multi-column.d.ts +1 -4
- package/dist/utils/multi-column.d.ts.map +1 -1
- package/dist/utils/multi-column.js +1 -3
- package/dist/utils/multi-column.js.map +1 -1
- package/dist/utils/upload.d.ts +1 -25
- package/dist/utils/upload.d.ts.map +1 -1
- package/dist/utils/upload.js +1 -19
- package/dist/utils/upload.js.map +1 -1
- package/package.json +2 -2
- package/src/config/types.ts +4 -47
- package/src/fields/index.ts +15 -63
- package/src/providers/local.ts +0 -12
- package/src/runtime/index.ts +0 -21
- package/src/utils/image.ts +0 -16
- package/src/utils/multi-column.ts +1 -4
- package/src/utils/upload.ts +1 -28
package/src/fields/index.ts
CHANGED
|
@@ -74,12 +74,10 @@ export interface FileDbConfig {
|
|
|
74
74
|
| { mode: 'keystone'; map?: Partial<FileColumnMap>; parts?: readonly FileColumnPart[] }
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
/** Whether a field-level `db.columns` option requests multi-column mode. */
|
|
78
77
|
function isMultiColumn(columns: ImageDbConfig['columns'] | FileDbConfig['columns']): boolean {
|
|
79
78
|
return columns === 'keystone' || (typeof columns === 'object' && columns?.mode === 'keystone')
|
|
80
79
|
}
|
|
81
80
|
|
|
82
|
-
/** Extract any per-part `@map` overrides from a `db.columns` option. */
|
|
83
81
|
function imageColumnOverrides(
|
|
84
82
|
columns: ImageDbConfig['columns'],
|
|
85
83
|
): Partial<ImageColumnMap> | undefined {
|
|
@@ -90,15 +88,14 @@ function fileColumnOverrides(columns: FileDbConfig['columns']): Partial<FileColu
|
|
|
90
88
|
return typeof columns === 'object' ? columns.map : undefined
|
|
91
89
|
}
|
|
92
90
|
|
|
93
|
-
/** The file parts a field-level `db.columns` option opts into (defaults to the base three). */
|
|
94
91
|
function fileColumnPartsFor(columns: FileDbConfig['columns']): readonly FileColumnPart[] {
|
|
95
92
|
return typeof columns === 'object' && columns.parts ? columns.parts : DEFAULT_FILE_COLUMN_PARTS
|
|
96
93
|
}
|
|
97
94
|
|
|
98
95
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
96
|
+
* An already-shaped metadata value or populated multi-column row is
|
|
97
|
+
* authoritative and must never trigger a re-upload (the no-re-upload
|
|
98
|
+
* guarantee — see ADR-0006).
|
|
102
99
|
*/
|
|
103
100
|
function isFileLike(value: unknown): value is File {
|
|
104
101
|
return (
|
|
@@ -109,9 +106,6 @@ function isFileLike(value: unknown): value is File {
|
|
|
109
106
|
)
|
|
110
107
|
}
|
|
111
108
|
|
|
112
|
-
/**
|
|
113
|
-
* File field configuration
|
|
114
|
-
*/
|
|
115
109
|
export interface FileFieldConfig<
|
|
116
110
|
TTypeInfo extends TypeInfo = TypeInfo,
|
|
117
111
|
> extends BaseFieldConfig<TTypeInfo> {
|
|
@@ -124,11 +118,7 @@ export interface FileFieldConfig<
|
|
|
124
118
|
cleanupOnDelete?: boolean
|
|
125
119
|
/** Automatically delete old file from storage when replaced with new file */
|
|
126
120
|
cleanupOnReplace?: boolean
|
|
127
|
-
/**
|
|
128
|
-
* Database configuration. By default a file is backed by a single `Json?`
|
|
129
|
-
* column; set `db.columns: 'keystone'` to map onto existing Keystone per-part
|
|
130
|
-
* columns in place (non-destructive migration). See ADR-0006.
|
|
131
|
-
*/
|
|
121
|
+
/** Database configuration; see {@link FileDbConfig} for multi-column mode. */
|
|
132
122
|
db?: FileDbConfig
|
|
133
123
|
/** UI options */
|
|
134
124
|
ui?: {
|
|
@@ -147,9 +137,6 @@ export interface FileFieldConfig<
|
|
|
147
137
|
}
|
|
148
138
|
}
|
|
149
139
|
|
|
150
|
-
/**
|
|
151
|
-
* Image field configuration
|
|
152
|
-
*/
|
|
153
140
|
export interface ImageFieldConfig<
|
|
154
141
|
TTypeInfo extends TypeInfo = TypeInfo,
|
|
155
142
|
> extends BaseFieldConfig<TTypeInfo> {
|
|
@@ -164,11 +151,7 @@ export interface ImageFieldConfig<
|
|
|
164
151
|
cleanupOnDelete?: boolean
|
|
165
152
|
/** Automatically delete old file from storage when replaced with new file */
|
|
166
153
|
cleanupOnReplace?: boolean
|
|
167
|
-
/**
|
|
168
|
-
* Database configuration. By default an image is backed by a single `Json?`
|
|
169
|
-
* column; set `db.columns: 'keystone'` to map onto existing Keystone per-part
|
|
170
|
-
* columns in place (non-destructive migration). See ADR-0006.
|
|
171
|
-
*/
|
|
154
|
+
/** Database configuration; see {@link ImageDbConfig} for multi-column mode. */
|
|
172
155
|
db?: ImageDbConfig
|
|
173
156
|
/** UI options */
|
|
174
157
|
ui?: {
|
|
@@ -214,9 +197,8 @@ export function file<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
214
197
|
): FileFieldConfig<TTypeInfo> {
|
|
215
198
|
const { hooks: userHooks, ...restOptions } = options
|
|
216
199
|
|
|
217
|
-
//
|
|
218
|
-
//
|
|
219
|
-
// field name so default `<field>_<part>` names line up with the live columns.
|
|
200
|
+
// Column map resolves lazily per field name so default `<field>_<part>`
|
|
201
|
+
// names line up with the live columns.
|
|
220
202
|
const multiColumn = isMultiColumn(options.db?.columns)
|
|
221
203
|
const columnMapFor = (fieldName: string): FileColumnMap =>
|
|
222
204
|
resolveFileColumnMap(fieldName, fileColumnOverrides(options.db?.columns))
|
|
@@ -240,38 +222,31 @@ export function file<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
240
222
|
resolveInput: async ({ resolvedData, fieldKey, context, item }: any) => {
|
|
241
223
|
const inputValue = resolvedData?.[fieldKey]
|
|
242
224
|
|
|
243
|
-
// If null/undefined, return as-is (deletion or no change)
|
|
244
225
|
if (inputValue === null || inputValue === undefined) {
|
|
245
226
|
return inputValue
|
|
246
227
|
}
|
|
247
228
|
|
|
248
|
-
//
|
|
249
|
-
// uploaded). An existing metadata value is AUTHORITATIVE and must never
|
|
229
|
+
// An existing metadata value is AUTHORITATIVE and must never
|
|
250
230
|
// re-upload. See ADR-0006.
|
|
251
231
|
if (typeof inputValue === 'object' && 'filename' in inputValue && 'url' in inputValue) {
|
|
252
232
|
return inputValue as FileMetadata
|
|
253
233
|
}
|
|
254
234
|
|
|
255
|
-
// Only a File-like input triggers an upload.
|
|
256
235
|
if (isFileLike(inputValue)) {
|
|
257
|
-
// Convert File to buffer
|
|
258
236
|
const fileObj = inputValue
|
|
259
237
|
const arrayBuffer = await fileObj.arrayBuffer()
|
|
260
238
|
const buffer = Buffer.from(arrayBuffer)
|
|
261
239
|
|
|
262
|
-
// Upload file using context.storage utilities
|
|
263
240
|
const metadata = (await context.storage.uploadFile(fieldConfig.storage, fileObj, buffer, {
|
|
264
241
|
validation: fieldConfig.validation,
|
|
265
242
|
})) as FileMetadata
|
|
266
243
|
|
|
267
|
-
// If cleanupOnReplace is enabled and there was an old file, delete it
|
|
268
244
|
if (fieldConfig.cleanupOnReplace && item && fieldKey) {
|
|
269
245
|
const oldMetadata = item[fieldKey] as FileMetadata | null
|
|
270
246
|
if (oldMetadata && oldMetadata.filename) {
|
|
271
247
|
try {
|
|
272
248
|
await context.storage.deleteFile(oldMetadata.storageProvider, oldMetadata.filename)
|
|
273
249
|
} catch (error) {
|
|
274
|
-
// Log error but don't fail the operation
|
|
275
250
|
console.error(`Failed to cleanup old file: ${oldMetadata.filename}`, error)
|
|
276
251
|
}
|
|
277
252
|
}
|
|
@@ -286,7 +261,7 @@ export function file<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
286
261
|
|
|
287
262
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Field builder hooks are generic and resolved at runtime
|
|
288
263
|
afterOperation: async ({ operation, originalItem, fieldKey, context }: any) => {
|
|
289
|
-
//
|
|
264
|
+
// The deleted row is `originalItem`.
|
|
290
265
|
if (operation === 'delete' && fieldConfig.cleanupOnDelete) {
|
|
291
266
|
const fileMetadata = originalItem?.[fieldKey] as FileMetadata | null
|
|
292
267
|
|
|
@@ -294,18 +269,15 @@ export function file<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
294
269
|
try {
|
|
295
270
|
await context.storage.deleteFile(fileMetadata.storageProvider, fileMetadata.filename)
|
|
296
271
|
} catch (error) {
|
|
297
|
-
// Log error but don't fail the operation
|
|
298
272
|
console.error(`Failed to cleanup file on delete: ${fileMetadata.filename}`, error)
|
|
299
273
|
}
|
|
300
274
|
}
|
|
301
275
|
}
|
|
302
276
|
},
|
|
303
|
-
// Merge with user-provided hooks if any
|
|
304
277
|
...userHooks,
|
|
305
278
|
},
|
|
306
279
|
|
|
307
280
|
getZodSchema: (_fieldName: string, _operation: 'create' | 'update') => {
|
|
308
|
-
// File metadata follows the FileMetadata schema
|
|
309
281
|
const fileMetadataSchema = z.object({
|
|
310
282
|
filename: z.string(),
|
|
311
283
|
originalFilename: z.string(),
|
|
@@ -325,12 +297,10 @@ export function file<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
325
297
|
},
|
|
326
298
|
|
|
327
299
|
getPrismaType: (_fieldName: string) => {
|
|
328
|
-
// Store as JSON in database (single-column default mode).
|
|
329
300
|
return { type: 'Json', modifiers: '?' }
|
|
330
301
|
},
|
|
331
302
|
|
|
332
303
|
getTypeScriptType: () => {
|
|
333
|
-
// TypeScript type is FileMetadata | null
|
|
334
304
|
return {
|
|
335
305
|
type: 'FileMetadata | null',
|
|
336
306
|
optional: true,
|
|
@@ -348,8 +318,7 @@ export function file<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
348
318
|
},
|
|
349
319
|
}
|
|
350
320
|
|
|
351
|
-
//
|
|
352
|
-
// so the single-Json? default is completely unaffected.
|
|
321
|
+
// Only attached in multi-column mode; the single-Json? default is unaffected.
|
|
353
322
|
if (multiColumn) {
|
|
354
323
|
fieldConfig.getPrismaColumns = (fieldName: string): MultiColumnPrismaResult[] => {
|
|
355
324
|
const map = columnMapFor(fieldName)
|
|
@@ -398,8 +367,7 @@ export function image<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
398
367
|
): ImageFieldConfig<TTypeInfo> {
|
|
399
368
|
const { hooks: userHooks, ...restOptions } = options
|
|
400
369
|
|
|
401
|
-
//
|
|
402
|
-
// instead of a single Json? column. See ADR-0006.
|
|
370
|
+
// See ADR-0006.
|
|
403
371
|
const multiColumn = isMultiColumn(options.db?.columns)
|
|
404
372
|
const columnMapFor = (fieldName: string): ImageColumnMap =>
|
|
405
373
|
resolveImageColumnMap(fieldName, imageColumnOverrides(options.db?.columns))
|
|
@@ -422,13 +390,11 @@ export function image<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
422
390
|
resolveInput: async ({ resolvedData, fieldKey, context, item }: any) => {
|
|
423
391
|
const inputValue = resolvedData?.[fieldKey]
|
|
424
392
|
|
|
425
|
-
// If null/undefined, return as-is (deletion or no change)
|
|
426
393
|
if (inputValue === null || inputValue === undefined) {
|
|
427
394
|
return inputValue
|
|
428
395
|
}
|
|
429
396
|
|
|
430
|
-
//
|
|
431
|
-
// uploaded). An existing metadata value is AUTHORITATIVE and must never
|
|
397
|
+
// An existing metadata value is AUTHORITATIVE and must never
|
|
432
398
|
// re-upload. See ADR-0006.
|
|
433
399
|
if (
|
|
434
400
|
typeof inputValue === 'object' &&
|
|
@@ -440,14 +406,11 @@ export function image<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
440
406
|
return inputValue as ImageMetadata
|
|
441
407
|
}
|
|
442
408
|
|
|
443
|
-
// Only a File-like input triggers an upload.
|
|
444
409
|
if (isFileLike(inputValue)) {
|
|
445
|
-
// Convert File to buffer
|
|
446
410
|
const fileObj = inputValue
|
|
447
411
|
const arrayBuffer = await fileObj.arrayBuffer()
|
|
448
412
|
const buffer = Buffer.from(arrayBuffer)
|
|
449
413
|
|
|
450
|
-
// Upload image using context.storage utilities
|
|
451
414
|
const metadata = (await context.storage.uploadImage(
|
|
452
415
|
fieldConfig.storage,
|
|
453
416
|
fileObj,
|
|
@@ -458,14 +421,12 @@ export function image<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
458
421
|
},
|
|
459
422
|
)) as ImageMetadata
|
|
460
423
|
|
|
461
|
-
// If cleanupOnReplace is enabled and there was an old file, delete it
|
|
462
424
|
if (fieldConfig.cleanupOnReplace && item && fieldKey) {
|
|
463
425
|
const oldMetadata = item[fieldKey] as ImageMetadata | null
|
|
464
426
|
if (oldMetadata && oldMetadata.filename) {
|
|
465
427
|
try {
|
|
466
428
|
await context.storage.deleteImage(oldMetadata)
|
|
467
429
|
} catch (error) {
|
|
468
|
-
// Log error but don't fail the operation
|
|
469
430
|
console.error(`Failed to cleanup old image: ${oldMetadata.filename}`, error)
|
|
470
431
|
}
|
|
471
432
|
}
|
|
@@ -480,7 +441,7 @@ export function image<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
480
441
|
|
|
481
442
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Field builder hooks are generic and resolved at runtime
|
|
482
443
|
afterOperation: async ({ operation, originalItem, fieldKey, context }: any) => {
|
|
483
|
-
//
|
|
444
|
+
// The deleted row is `originalItem`.
|
|
484
445
|
if (operation === 'delete' && fieldConfig.cleanupOnDelete) {
|
|
485
446
|
const imageMetadata = originalItem?.[fieldKey] as ImageMetadata | null
|
|
486
447
|
|
|
@@ -488,18 +449,15 @@ export function image<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
488
449
|
try {
|
|
489
450
|
await context.storage.deleteImage(imageMetadata)
|
|
490
451
|
} catch (error) {
|
|
491
|
-
// Log error but don't fail the operation
|
|
492
452
|
console.error(`Failed to cleanup image on delete: ${imageMetadata.filename}`, error)
|
|
493
453
|
}
|
|
494
454
|
}
|
|
495
455
|
}
|
|
496
456
|
},
|
|
497
|
-
// Merge with user-provided hooks if any
|
|
498
457
|
...userHooks,
|
|
499
458
|
},
|
|
500
459
|
|
|
501
460
|
getZodSchema: (_fieldName: string, _operation: 'create' | 'update') => {
|
|
502
|
-
// Image metadata follows the ImageMetadata schema (extends FileMetadata)
|
|
503
461
|
const imageMetadataSchema = z.object({
|
|
504
462
|
filename: z.string(),
|
|
505
463
|
originalFilename: z.string(),
|
|
@@ -524,20 +482,15 @@ export function image<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
524
482
|
.optional(),
|
|
525
483
|
})
|
|
526
484
|
|
|
527
|
-
//
|
|
528
|
-
// makes the object KEY optional in Zod 4 — a bare union that merely accepts
|
|
529
|
-
// an `undefined` value does NOT (see issue #618, and the #570 precedent in
|
|
530
|
-
// core's validation tests).
|
|
485
|
+
// Same `.nullish()` KEY-optionality rationale as file()'s getZodSchema above.
|
|
531
486
|
return imageMetadataSchema.nullish()
|
|
532
487
|
},
|
|
533
488
|
|
|
534
489
|
getPrismaType: (_fieldName: string) => {
|
|
535
|
-
// Store as JSON in database
|
|
536
490
|
return { type: 'Json', modifiers: '?' }
|
|
537
491
|
},
|
|
538
492
|
|
|
539
493
|
getTypeScriptType: () => {
|
|
540
|
-
// TypeScript type is ImageMetadata | null
|
|
541
494
|
return {
|
|
542
495
|
type: 'ImageMetadata | null',
|
|
543
496
|
optional: true,
|
|
@@ -555,8 +508,7 @@ export function image<TTypeInfo extends TypeInfo = TypeInfo>(
|
|
|
555
508
|
},
|
|
556
509
|
}
|
|
557
510
|
|
|
558
|
-
//
|
|
559
|
-
// so the single-Json? default is completely unaffected.
|
|
511
|
+
// Only attached in multi-column mode; the single-Json? default is unaffected.
|
|
560
512
|
if (multiColumn) {
|
|
561
513
|
fieldConfig.getPrismaColumns = (fieldName: string): MultiColumnPrismaResult[] => {
|
|
562
514
|
const map = columnMapFor(fieldName)
|
package/src/providers/local.ts
CHANGED
|
@@ -8,10 +8,6 @@ import type {
|
|
|
8
8
|
LocalStorageConfig,
|
|
9
9
|
} from '../config/types.js'
|
|
10
10
|
|
|
11
|
-
/**
|
|
12
|
-
* Local filesystem storage provider
|
|
13
|
-
* Stores files on the local filesystem
|
|
14
|
-
*/
|
|
15
11
|
export class LocalStorageProvider implements StorageProvider {
|
|
16
12
|
private config: LocalStorageConfig
|
|
17
13
|
|
|
@@ -19,9 +15,6 @@ export class LocalStorageProvider implements StorageProvider {
|
|
|
19
15
|
this.config = config
|
|
20
16
|
}
|
|
21
17
|
|
|
22
|
-
/**
|
|
23
|
-
* Ensures the upload directory exists
|
|
24
|
-
*/
|
|
25
18
|
private async ensureUploadDir(): Promise<void> {
|
|
26
19
|
try {
|
|
27
20
|
await fs.access(this.config.uploadDir)
|
|
@@ -30,9 +23,6 @@ export class LocalStorageProvider implements StorageProvider {
|
|
|
30
23
|
}
|
|
31
24
|
}
|
|
32
25
|
|
|
33
|
-
/**
|
|
34
|
-
* Generates a unique filename if configured
|
|
35
|
-
*/
|
|
36
26
|
private generateFilename(originalFilename: string): string {
|
|
37
27
|
if (this.config.generateUniqueFilenames === false) {
|
|
38
28
|
return originalFilename
|
|
@@ -54,10 +44,8 @@ export class LocalStorageProvider implements StorageProvider {
|
|
|
54
44
|
const generatedFilename = this.generateFilename(filename)
|
|
55
45
|
const filePath = path.join(this.config.uploadDir, generatedFilename)
|
|
56
46
|
|
|
57
|
-
// Write file to disk
|
|
58
47
|
await fs.writeFile(filePath, file)
|
|
59
48
|
|
|
60
|
-
// Get file stats for size
|
|
61
49
|
const stats = await fs.stat(filePath)
|
|
62
50
|
|
|
63
51
|
return {
|
package/src/runtime/index.ts
CHANGED
|
@@ -88,7 +88,6 @@ export async function uploadFile(
|
|
|
88
88
|
): Promise<FileMetadata> {
|
|
89
89
|
const { file, buffer } = data
|
|
90
90
|
|
|
91
|
-
// Validate file
|
|
92
91
|
if (options?.validation) {
|
|
93
92
|
const validation = validateFile(
|
|
94
93
|
{
|
|
@@ -104,19 +103,15 @@ export async function uploadFile(
|
|
|
104
103
|
}
|
|
105
104
|
}
|
|
106
105
|
|
|
107
|
-
// Get storage provider
|
|
108
106
|
const provider = createStorageProvider(config, storageProviderName)
|
|
109
107
|
|
|
110
|
-
// Determine content type
|
|
111
108
|
const contentType = file.type || getMimeType(file.name)
|
|
112
109
|
|
|
113
|
-
// Upload file
|
|
114
110
|
const result = await provider.upload(buffer, file.name, {
|
|
115
111
|
contentType,
|
|
116
112
|
metadata: options?.metadata,
|
|
117
113
|
})
|
|
118
114
|
|
|
119
|
-
// Return metadata
|
|
120
115
|
return {
|
|
121
116
|
filename: result.filename,
|
|
122
117
|
originalFilename: file.name,
|
|
@@ -159,7 +154,6 @@ export async function uploadImage(
|
|
|
159
154
|
): Promise<ImageMetadata> {
|
|
160
155
|
const { file, buffer } = data
|
|
161
156
|
|
|
162
|
-
// Validate file
|
|
163
157
|
if (options?.validation) {
|
|
164
158
|
const validation = validateFile(
|
|
165
159
|
{
|
|
@@ -175,22 +169,17 @@ export async function uploadImage(
|
|
|
175
169
|
}
|
|
176
170
|
}
|
|
177
171
|
|
|
178
|
-
// Get storage provider
|
|
179
172
|
const provider = createStorageProvider(config, storageProviderName)
|
|
180
173
|
|
|
181
|
-
// Determine content type
|
|
182
174
|
const contentType = file.type || getMimeType(file.name)
|
|
183
175
|
|
|
184
|
-
// Get original image dimensions
|
|
185
176
|
const { width, height } = await getImageDimensions(buffer)
|
|
186
177
|
|
|
187
|
-
// Upload original image
|
|
188
178
|
const result = await provider.upload(buffer, file.name, {
|
|
189
179
|
contentType,
|
|
190
180
|
metadata: options?.metadata,
|
|
191
181
|
})
|
|
192
182
|
|
|
193
|
-
// Process transformations if provided
|
|
194
183
|
let transformations:
|
|
195
184
|
Record<string, { url: string; width: number; height: number; size: number }> | undefined
|
|
196
185
|
if (options?.transformations) {
|
|
@@ -203,7 +192,6 @@ export async function uploadImage(
|
|
|
203
192
|
)
|
|
204
193
|
}
|
|
205
194
|
|
|
206
|
-
// Return metadata
|
|
207
195
|
return {
|
|
208
196
|
filename: result.filename,
|
|
209
197
|
originalFilename: file.name,
|
|
@@ -219,9 +207,6 @@ export async function uploadImage(
|
|
|
219
207
|
}
|
|
220
208
|
}
|
|
221
209
|
|
|
222
|
-
/**
|
|
223
|
-
* Deletes a file from storage
|
|
224
|
-
*/
|
|
225
210
|
export async function deleteFile(
|
|
226
211
|
config: OpenSaasConfig,
|
|
227
212
|
storageProviderName: string,
|
|
@@ -231,19 +216,13 @@ export async function deleteFile(
|
|
|
231
216
|
await provider.delete(filename)
|
|
232
217
|
}
|
|
233
218
|
|
|
234
|
-
/**
|
|
235
|
-
* Deletes an image and all its transformations from storage
|
|
236
|
-
*/
|
|
237
219
|
export async function deleteImage(config: OpenSaasConfig, metadata: ImageMetadata): Promise<void> {
|
|
238
220
|
const provider = createStorageProvider(config, metadata.storageProvider)
|
|
239
221
|
|
|
240
|
-
// Delete original image
|
|
241
222
|
await provider.delete(metadata.filename)
|
|
242
223
|
|
|
243
|
-
// Delete all transformations
|
|
244
224
|
if (metadata.transformations) {
|
|
245
225
|
for (const transformationResult of Object.values(metadata.transformations)) {
|
|
246
|
-
// Extract filename from URL
|
|
247
226
|
const filename = transformationResult.url.split('/').pop()
|
|
248
227
|
if (filename) {
|
|
249
228
|
await provider.delete(filename)
|
package/src/utils/image.ts
CHANGED
|
@@ -5,9 +5,6 @@ import type {
|
|
|
5
5
|
StorageProvider,
|
|
6
6
|
} from '../config/types.js'
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
* Gets image dimensions from a buffer
|
|
10
|
-
*/
|
|
11
8
|
export async function getImageDimensions(
|
|
12
9
|
buffer: Buffer | Uint8Array,
|
|
13
10
|
): Promise<{ width: number; height: number }> {
|
|
@@ -18,16 +15,12 @@ export async function getImageDimensions(
|
|
|
18
15
|
}
|
|
19
16
|
}
|
|
20
17
|
|
|
21
|
-
/**
|
|
22
|
-
* Applies a single transformation to an image
|
|
23
|
-
*/
|
|
24
18
|
export async function transformImage(
|
|
25
19
|
buffer: Buffer | Uint8Array,
|
|
26
20
|
transformation: ImageTransformationConfig,
|
|
27
21
|
): Promise<Buffer> {
|
|
28
22
|
let image = sharp(buffer)
|
|
29
23
|
|
|
30
|
-
// Apply resizing
|
|
31
24
|
if (transformation.width || transformation.height) {
|
|
32
25
|
image = image.resize({
|
|
33
26
|
width: transformation.width,
|
|
@@ -36,7 +29,6 @@ export async function transformImage(
|
|
|
36
29
|
})
|
|
37
30
|
}
|
|
38
31
|
|
|
39
|
-
// Apply format conversion
|
|
40
32
|
if (transformation.format) {
|
|
41
33
|
const options = {
|
|
42
34
|
quality: transformation.quality || 80,
|
|
@@ -61,10 +53,6 @@ export async function transformImage(
|
|
|
61
53
|
return await image.toBuffer()
|
|
62
54
|
}
|
|
63
55
|
|
|
64
|
-
/**
|
|
65
|
-
* Processes all transformations for an image
|
|
66
|
-
* Uploads the original and all transformed versions
|
|
67
|
-
*/
|
|
68
56
|
export async function processImageTransformations(
|
|
69
57
|
buffer: Buffer | Uint8Array,
|
|
70
58
|
originalFilename: string,
|
|
@@ -75,17 +63,13 @@ export async function processImageTransformations(
|
|
|
75
63
|
const results: Record<string, ImageTransformationResult> = {}
|
|
76
64
|
|
|
77
65
|
for (const [name, config] of Object.entries(transformations)) {
|
|
78
|
-
// Transform the image
|
|
79
66
|
const transformedBuffer = await transformImage(buffer, config)
|
|
80
67
|
|
|
81
|
-
// Get dimensions of transformed image
|
|
82
68
|
const { width, height } = await getImageDimensions(transformedBuffer)
|
|
83
69
|
|
|
84
|
-
// Generate filename for transformation
|
|
85
70
|
const ext = config.format ? `.${config.format}` : ''
|
|
86
71
|
const transformedFilename = `${originalFilename}-${name}${ext}`
|
|
87
72
|
|
|
88
|
-
// Upload transformed image
|
|
89
73
|
const uploadResult = await storageProvider.upload(transformedBuffer, transformedFilename, {
|
|
90
74
|
contentType:
|
|
91
75
|
config.format === 'jpeg'
|
|
@@ -90,7 +90,6 @@ const FILE_PART_PRISMA_TYPE: Record<FileColumnPart, 'String' | 'Int'> = {
|
|
|
90
90
|
*/
|
|
91
91
|
export type ImageColumnMap = Record<ImageColumnPart, string>
|
|
92
92
|
|
|
93
|
-
/** Map of file part → physical column name. */
|
|
94
93
|
export type FileColumnMap = Record<FileColumnPart, string>
|
|
95
94
|
|
|
96
95
|
/** A single physical column to emit for a multi-column field. */
|
|
@@ -149,9 +148,7 @@ export function resolveImageColumnMap(
|
|
|
149
148
|
return { ...keystoneImageColumnMap(fieldName), ...overrides }
|
|
150
149
|
}
|
|
151
150
|
|
|
152
|
-
/**
|
|
153
|
-
* Resolve the effective file column map.
|
|
154
|
-
*/
|
|
151
|
+
/** Resolve the effective file column map; see {@link resolveImageColumnMap}. */
|
|
155
152
|
export function resolveFileColumnMap(
|
|
156
153
|
fieldName: string,
|
|
157
154
|
overrides?: Partial<FileColumnMap>,
|
package/src/utils/upload.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import mime from 'mime-types'
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* File validation options
|
|
5
|
-
*/
|
|
6
3
|
export interface FileValidationOptions {
|
|
7
4
|
/** Maximum file size in bytes */
|
|
8
5
|
maxFileSize?: number
|
|
@@ -12,17 +9,11 @@ export interface FileValidationOptions {
|
|
|
12
9
|
acceptedExtensions?: string[]
|
|
13
10
|
}
|
|
14
11
|
|
|
15
|
-
/**
|
|
16
|
-
* File validation result
|
|
17
|
-
*/
|
|
18
12
|
export interface FileValidationResult {
|
|
19
13
|
valid: boolean
|
|
20
14
|
error?: string
|
|
21
15
|
}
|
|
22
16
|
|
|
23
|
-
/**
|
|
24
|
-
* Validates a file against the provided options
|
|
25
|
-
*/
|
|
26
17
|
export function validateFile(
|
|
27
18
|
file: { size: number; name: string; type: string },
|
|
28
19
|
options?: FileValidationOptions,
|
|
@@ -31,7 +22,6 @@ export function validateFile(
|
|
|
31
22
|
return { valid: true }
|
|
32
23
|
}
|
|
33
24
|
|
|
34
|
-
// Check file size
|
|
35
25
|
if (options.maxFileSize && file.size > options.maxFileSize) {
|
|
36
26
|
return {
|
|
37
27
|
valid: false,
|
|
@@ -39,7 +29,6 @@ export function validateFile(
|
|
|
39
29
|
}
|
|
40
30
|
}
|
|
41
31
|
|
|
42
|
-
// Check MIME type
|
|
43
32
|
if (options.acceptedMimeTypes && options.acceptedMimeTypes.length > 0) {
|
|
44
33
|
const fileMimeType = file.type || mime.lookup(file.name) || ''
|
|
45
34
|
if (!options.acceptedMimeTypes.includes(fileMimeType)) {
|
|
@@ -50,7 +39,6 @@ export function validateFile(
|
|
|
50
39
|
}
|
|
51
40
|
}
|
|
52
41
|
|
|
53
|
-
// Check file extension
|
|
54
42
|
if (options.acceptedExtensions && options.acceptedExtensions.length > 0) {
|
|
55
43
|
const ext = file.name.substring(file.name.lastIndexOf('.')).toLowerCase()
|
|
56
44
|
if (!options.acceptedExtensions.includes(ext)) {
|
|
@@ -64,9 +52,6 @@ export function validateFile(
|
|
|
64
52
|
return { valid: true }
|
|
65
53
|
}
|
|
66
54
|
|
|
67
|
-
/**
|
|
68
|
-
* Formats file size in human-readable format
|
|
69
|
-
*/
|
|
70
55
|
export function formatFileSize(bytes: number): string {
|
|
71
56
|
if (bytes === 0) return '0 Bytes'
|
|
72
57
|
|
|
@@ -77,16 +62,10 @@ export function formatFileSize(bytes: number): string {
|
|
|
77
62
|
return `${Math.round((bytes / Math.pow(k, i)) * 100) / 100} ${sizes[i]}`
|
|
78
63
|
}
|
|
79
64
|
|
|
80
|
-
/**
|
|
81
|
-
* Gets MIME type from filename
|
|
82
|
-
*/
|
|
83
65
|
export function getMimeType(filename: string): string {
|
|
84
66
|
return mime.lookup(filename) || 'application/octet-stream'
|
|
85
67
|
}
|
|
86
68
|
|
|
87
|
-
/**
|
|
88
|
-
* Extracts file metadata from a File or Blob
|
|
89
|
-
*/
|
|
90
69
|
export interface FileInfo {
|
|
91
70
|
name: string
|
|
92
71
|
size: number
|
|
@@ -94,18 +73,12 @@ export interface FileInfo {
|
|
|
94
73
|
lastModified?: number
|
|
95
74
|
}
|
|
96
75
|
|
|
97
|
-
/**
|
|
98
|
-
* Converts a File/Blob to Buffer for Node.js processing
|
|
99
|
-
*/
|
|
100
76
|
export async function fileToBuffer(file: Blob | File): Promise<Buffer> {
|
|
101
77
|
const arrayBuffer = await file.arrayBuffer()
|
|
102
78
|
return Buffer.from(arrayBuffer)
|
|
103
79
|
}
|
|
104
80
|
|
|
105
|
-
/**
|
|
106
|
-
* Parses FormData and extracts file information
|
|
107
|
-
* This is a utility for developers to use in their upload routes
|
|
108
|
-
*/
|
|
81
|
+
/** Utility for developers to use in their own upload routes. */
|
|
109
82
|
export async function parseFileFromFormData(
|
|
110
83
|
formData: FormData,
|
|
111
84
|
fieldName: string = 'file',
|