1
1
const { Text } = require ( '@keystonejs/fields' )
2
2
const cuid = require ( 'cuid' )
3
- const isNil = require ( 'lodash/isNil' )
4
3
4
+ const { CLOUD_STORAGE_TEXT_MIMETYPE , CLOUD_STORAGE_TEXT_ENCODING } = require ( '@open-condo/keystone/fields/CloudStorageText/constants' )
5
5
const { bufferToStream, readFileFromStream, getObjectStream } = require ( '@open-condo/keystone/file' )
6
6
7
+
7
8
const CommonInterface = superclass => class extends superclass {
8
9
9
10
constructor ( ) {
@@ -16,7 +17,7 @@ const CommonInterface = superclass => class extends superclass {
16
17
17
18
setupHooks ( { addPreSaveHook, addPostReadHook } ) {
18
19
addPreSaveHook ( async ( item ) => {
19
- if ( this . _isFieldDefined ( item ) ) {
20
+ if ( item [ this . path ] ) {
20
21
item [ this . path ] = await this . _saveFileToAdapter ( item [ this . path ] )
21
22
}
22
23
return item
@@ -30,30 +31,24 @@ const CommonInterface = superclass => class extends superclass {
30
31
} )
31
32
}
32
33
33
- _isFieldDefined ( item ) {
34
- return item && ! isNil ( item [ this . path ] )
35
- }
36
-
37
34
async _saveFileToAdapter ( content ) {
38
35
const stream = bufferToStream ( content )
39
- const originalFilename = this . _generateFilename ( )
40
- const mimetype = 'text/plain'
41
- const encoding = 'utf8'
36
+ const originalFilename = cuid ( )
42
37
43
38
const { id, filename, _meta } = await this . fileAdapter . save ( {
44
39
stream,
45
40
filename : originalFilename ,
46
- mimetype,
47
- encoding,
41
+ mimetype : CLOUD_STORAGE_TEXT_MIMETYPE ,
42
+ encoding : CLOUD_STORAGE_TEXT_ENCODING ,
48
43
id : cuid ( ) ,
49
44
} )
50
45
51
46
return {
52
47
id,
53
48
filename,
54
49
originalFilename,
55
- mimetype,
56
- encoding,
50
+ mimetype : CLOUD_STORAGE_TEXT_MIMETYPE ,
51
+ encoding : CLOUD_STORAGE_TEXT_ENCODING ,
57
52
_meta,
58
53
}
59
54
}
@@ -64,12 +59,10 @@ const CommonInterface = superclass => class extends superclass {
64
59
return fileContent . toString ( )
65
60
}
66
61
67
- _generateFilename ( ) {
68
- return `${ new Date ( ) . toISOString ( ) } `
69
- }
70
-
71
62
addToTableSchema ( table ) {
72
- table . jsonb ( this . path )
63
+ const column = table . jsonb ( this . path )
64
+ if ( this . isNotNullable ) column . notNullable ( )
65
+ if ( this . defaultTo ) column . defaultTo ( this . defaultTo )
73
66
}
74
67
}
75
68
0 commit comments