@@ -7,7 +7,10 @@ const jwt = require('jsonwebtoken');
77const fs = require ( 'fs' ) ;
88
99/**
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.
1114 *
1215 * For valid data posted to database /$temp/screenshot/reports/$prNumber/$secureToken, move it to
1316 * /screenshot/reports/$prNumber.
@@ -87,6 +90,7 @@ exports.copyImage = firebaseFunctions.database.ref(copyImagePath).onWrite(event
8790 const binaryData = new Buffer ( event . data . val ( ) , 'base64' ) . toString ( 'binary' ) ;
8891 fs . writeFile ( tempPath , binaryData , 'binary' ) ;
8992 return bucket . upload ( tempPath , { destination : filePath } ) . then ( ( ) => {
93+ // Clear the data in temporary folder after processed.
9094 return event . data . ref . parent . set ( null ) ;
9195 } ) ;
9296 } ) . catch ( ( error ) => {
@@ -124,6 +128,10 @@ exports.copyGoldens = firebaseFunctions.storage.bucket(firebaseFunctions.config(
124128 } ) ;
125129} ) ;
126130
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+ */
127135function handleDataChange ( event , path ) {
128136 // Only edit data when it is first created. Exit when the data is deleted.
129137 if ( event . data . previous . exists ( ) || ! event . data . exists ( ) ) {
@@ -137,6 +145,7 @@ function handleDataChange(event, path) {
137145 return validateSecureToken ( secureToken , prNumber ) . then ( ( payload ) => {
138146 return firebaseAdmin . database ( ) . ref ( ) . child ( 'screenshot/reports' )
139147 . child ( prNumber ) . child ( path ) . set ( original ) . then ( ( ) => {
148+ // Clear the data in temporary folder after processed.
140149 return event . data . ref . parent . set ( null ) ;
141150 } ) ;
142151 } ) . catch ( ( error ) => {
0 commit comments