@@ -7,7 +7,10 @@ const jwt = require('jsonwebtoken');
7
7
const fs = require ( 'fs' ) ;
8
8
9
9
/**
10
- * Data and images handling for Screenshot test
10
+ * Data and images handling for Screenshot test.
11
+ *
12
+ * All users can post data to temporary folder. These Functions will check the data with JsonWebToken and
13
+ * move the valid data out of temporary folder.
11
14
*
12
15
* For valid data posted to database /$temp/screenshot/reports/$prNumber/$secureToken, move it to
13
16
* /screenshot/reports/$prNumber.
@@ -87,6 +90,7 @@ exports.copyImage = firebaseFunctions.database.ref(copyImagePath).onWrite(event
87
90
const binaryData = new Buffer ( event . data . val ( ) , 'base64' ) . toString ( 'binary' ) ;
88
91
fs . writeFile ( tempPath , binaryData , 'binary' ) ;
89
92
return bucket . upload ( tempPath , { destination : filePath } ) . then ( ( ) => {
93
+ // Clear the data in temporary folder after processed.
90
94
return event . data . ref . parent . set ( null ) ;
91
95
} ) ;
92
96
} ) . catch ( ( error ) => {
@@ -124,6 +128,10 @@ exports.copyGoldens = firebaseFunctions.storage.bucket(firebaseFunctions.config(
124
128
} ) ;
125
129
} ) ;
126
130
131
+ /**
132
+ * Handle data written to temporary folder. Validate the JWT and move the data out of
133
+ * temporary folder if the token is valid.
134
+ */
127
135
function handleDataChange ( event , path ) {
128
136
// Only edit data when it is first created. Exit when the data is deleted.
129
137
if ( event . data . previous . exists ( ) || ! event . data . exists ( ) ) {
@@ -137,6 +145,7 @@ function handleDataChange(event, path) {
137
145
return validateSecureToken ( secureToken , prNumber ) . then ( ( payload ) => {
138
146
return firebaseAdmin . database ( ) . ref ( ) . child ( 'screenshot/reports' )
139
147
. child ( prNumber ) . child ( path ) . set ( original ) . then ( ( ) => {
148
+ // Clear the data in temporary folder after processed.
140
149
return event . data . ref . parent . set ( null ) ;
141
150
} ) ;
142
151
} ) . catch ( ( error ) => {
0 commit comments