@@ -10,69 +10,70 @@ type Args = {
10
10
filesRequiredOnCreate : boolean
11
11
}
12
12
13
- export const getBeforeValidateHook =
14
- ( { context, filesRequiredOnCreate } : Args ) : CollectionBeforeValidateHook < JsonObject & TypeWithID > => {
15
- return async ( { data, operation, originalDoc, req } ) => {
16
- const file = req . file
17
-
18
- if ( operation === 'create' && filesRequiredOnCreate && ! data ?. bunnyVideoId && ! file ) {
19
- throw new MissingFile ( req . t )
20
- }
21
-
22
- if ( data && ! data . bunnyVideoId ) {
23
- data . bunnyVideoId = null
24
- }
25
-
26
- const processVideoData = async ( videoId : string , targetData : typeof data ) => {
27
- if ( ! context . streamConfig || ! targetData ) {
28
- return
29
- }
30
-
31
- const videoData = await getStreamVideo ( {
32
- streamConfig : context . streamConfig ,
33
- videoId,
34
- } )
35
-
36
- if ( isVideoProcessed ( videoData . status ) ) {
37
- const safeFilename = await getSafeFileName ( {
38
- collectionSlug : context . collection . slug ,
39
- desiredFilename : videoData . title || `video-${ videoData . guid } ` ,
40
- req,
41
- staticPath : '' ,
42
- } )
43
-
44
- targetData . filename = safeFilename
45
- targetData . width = null
46
- targetData . height = null
47
- targetData . focalX = null
48
- targetData . focalY = null
49
- targetData . bunnyVideoId = videoData . guid
50
-
51
- if ( ! targetData . mimeType ) {
52
- targetData . mimeType = 'video/mp4'
53
- }
54
-
55
- if ( ! targetData . filesize ) {
56
- targetData . filesize = videoData . storageSize
57
- }
58
- }
59
- }
60
-
61
- if ( operation === 'update' && originalDoc && data ) {
62
- if ( ! file && data . bunnyVideoId && data . bunnyVideoId !== originalDoc . bunnyVideoId ) {
63
- if ( ! req . context ) {
64
- req . context = { }
65
- }
66
- req . context . oldDoc = originalDoc
67
-
68
- await processVideoData ( data . bunnyVideoId , data )
69
- }
70
- }
71
-
72
- if ( operation === 'create' && ! file && data ?. bunnyVideoId ) {
73
- await processVideoData ( data . bunnyVideoId , data )
74
- }
75
-
76
- return data
77
- }
78
- }
13
+ type Data = JsonObject & TypeWithID
14
+
15
+ export const getBeforeValidateHook = ( { context, filesRequiredOnCreate } : Args ) : CollectionBeforeValidateHook < Data > => {
16
+ return async ( { data, operation, originalDoc, req } ) => {
17
+ const file = req . file
18
+
19
+ if ( operation === 'create' && filesRequiredOnCreate && ! data ?. bunnyVideoId && ! file ) {
20
+ throw new MissingFile ( req . t )
21
+ }
22
+
23
+ if ( data && ! data . bunnyVideoId ) {
24
+ data . bunnyVideoId = null
25
+ }
26
+
27
+ const processVideoData = async ( videoId : string , targetData : typeof data ) => {
28
+ if ( ! context . streamConfig || ! targetData ) {
29
+ return
30
+ }
31
+
32
+ const videoData = await getStreamVideo ( {
33
+ streamConfig : context . streamConfig ,
34
+ videoId,
35
+ } )
36
+
37
+ if ( isVideoProcessed ( videoData . status ) ) {
38
+ const safeFilename = await getSafeFileName ( {
39
+ collectionSlug : context . collection . slug ,
40
+ desiredFilename : videoData . title || `video-${ videoData . guid } ` ,
41
+ req,
42
+ staticPath : '' ,
43
+ } )
44
+
45
+ targetData . filename = safeFilename
46
+ targetData . width = null
47
+ targetData . height = null
48
+ targetData . focalX = null
49
+ targetData . focalY = null
50
+ targetData . bunnyVideoId = videoData . guid
51
+
52
+ if ( ! targetData . mimeType ) {
53
+ targetData . mimeType = 'video/mp4'
54
+ }
55
+
56
+ if ( ! targetData . filesize ) {
57
+ targetData . filesize = videoData . storageSize
58
+ }
59
+ }
60
+ }
61
+
62
+ if ( operation === 'update' && originalDoc && data ) {
63
+ if ( ! file && data . bunnyVideoId && data . bunnyVideoId !== originalDoc . bunnyVideoId ) {
64
+ if ( ! req . context ) {
65
+ req . context = { }
66
+ }
67
+ req . context . oldDoc = originalDoc
68
+
69
+ await processVideoData ( data . bunnyVideoId , data )
70
+ }
71
+ }
72
+
73
+ if ( operation === 'create' && ! file && data ?. bunnyVideoId ) {
74
+ await processVideoData ( data . bunnyVideoId , data )
75
+ }
76
+
77
+ return data
78
+ }
79
+ }
0 commit comments