@@ -864,7 +864,8 @@ var submitForDevelopChallenge = function (api, connection, dbConnectionMap, next
864
864
ret = { } ,
865
865
userId = connection . caller . userId ,
866
866
challengeId = Number ( connection . params . challengeId ) ,
867
- fileName ,
867
+ fileName = connection . params . fileName ,
868
+ fileData = connection . params . fileData ,
868
869
type = connection . params . type ,
869
870
error ,
870
871
resourceId ,
@@ -879,8 +880,7 @@ var submitForDevelopChallenge = function (api, connection, dbConnectionMap, next
879
880
thurgoodApiKey = process . env . THURGOOD_API_KEY || api . config . thurgoodApiKey ,
880
881
thurgoodJobId = null ,
881
882
multipleSubmissionPossible ,
882
- savedFilePath = null ,
883
- submissionFile = connection . params . submissionFile ;
883
+ savedFilePath = null ;
884
884
885
885
async . waterfall ( [
886
886
function ( cb ) {
@@ -893,7 +893,9 @@ var submitForDevelopChallenge = function (api, connection, dbConnectionMap, next
893
893
894
894
//Simple validations of the incoming parameters
895
895
error = helper . checkPositiveInteger ( challengeId , 'challengeId' ) ||
896
- helper . checkMaxNumber ( challengeId , MAX_INT , 'challengeId' ) ;
896
+ helper . checkMaxNumber ( challengeId , MAX_INT , 'challengeId' ) ||
897
+ helper . checkStringPopulated ( fileName , 'fileName' ) ||
898
+ helper . checkStringPopulated ( fileData , 'fileData' ) ;
897
899
898
900
if ( error ) {
899
901
cb ( error ) ;
@@ -911,15 +913,7 @@ var submitForDevelopChallenge = function (api, connection, dbConnectionMap, next
911
913
}
912
914
}
913
915
914
- //Simple validations of the incoming parameters
915
- if ( submissionFile . constructor . name !== 'File' ) {
916
- cb ( new IllegalArgumentError ( "submissionFile must be a File" ) ) ;
917
- return ;
918
- }
919
-
920
-
921
916
//Validation for the size of the fileName parameter. It should be 256 chars as this is max length of parameter field in submission table.
922
- fileName = submissionFile . name ;
923
917
if ( fileName . length > 256 ) {
924
918
cb ( new BadRequestError ( "The file name is too long. It must be 256 characters or less." ) ) ;
925
919
return ;
@@ -988,37 +982,53 @@ var submitForDevelopChallenge = function (api, connection, dbConnectionMap, next
988
982
uploadId = generatedIds . uploadId ;
989
983
submissionId = generatedIds . submissionId ;
990
984
991
- var submissionPath ;
985
+ var submissionPath ,
986
+ filePathToSave ,
987
+ decodedFileData ;
992
988
993
989
//The file output dir should be overwritable by environment variable
994
990
submissionPath = api . config . submissionDir ;
995
991
996
992
//The path to save is the folder with the name as <base submission path>
997
993
//The name of the file is the <generated upload id>_<original file name>
998
- savedFilePath = submissionPath + "/" + uploadId + "_" + fileName ;
999
-
1000
- //Check the max length of the submission file (if there is a limit)
1001
- if ( api . config . submissionMaxSizeBytes > 0 ) {
1002
- fs . stat ( submissionFile . path , function ( err , stats ) {
1003
- if ( err ) {
1004
- cb ( err ) ;
1005
- return ;
1006
- }
994
+ filePathToSave = submissionPath + "/" + uploadId + "_" + connection . params . fileName ;
1007
995
1008
- if ( stats . size > api . config . submissionMaxSizeBytes ) {
1009
- cb ( new RequestTooLargeError (
1010
- "The submission file size is greater than the max allowed size: " + ( api . config . submissionMaxSizeBytes / 1024 ) + " KB."
1011
- ) ) ;
1012
- return ;
1013
- }
996
+ //Decode the base64 encoded file data
997
+ decodedFileData = new Buffer ( connection . params . fileData , 'base64' ) ;
998
+
999
+ //Write the submission to file
1000
+ fs . writeFile ( filePathToSave , decodedFileData , function ( err ) {
1001
+ if ( err ) {
1002
+ cb ( err ) ;
1003
+ return ;
1004
+ }
1005
+
1006
+ //Check the max length of the submission file (if there is a limit)
1007
+ if ( api . config . submissionMaxSizeBytes > 0 ) {
1008
+ fs . stat ( filePathToSave , function ( err , stats ) {
1009
+ if ( err ) {
1010
+ cb ( err ) ;
1011
+ return ;
1012
+ }
1013
+ console . log ( '-------------------------------------------' ) ;
1014
+ console . log ( stats . size + '\t' + api . config . submissionMaxSizeBytes ) ;
1015
+ console . log ( '-------------------------------------------' ) ;
1016
+
1017
+ if ( stats . size > api . config . submissionMaxSizeBytes ) {
1018
+ cb ( new RequestTooLargeError (
1019
+ "The submission file size is greater than the max allowed size: " + ( api . config . submissionMaxSizeBytes / 1024 ) + " KB."
1020
+ ) ) ;
1021
+ return ;
1022
+ }
1023
+ savedFilePath = filePathToSave ;
1024
+ cb ( ) ;
1025
+ } ) ;
1026
+ } else {
1027
+ savedFilePath = filePathToSave ;
1014
1028
cb ( ) ;
1015
- } ) ;
1016
- } else {
1017
- cb ( ) ;
1018
- }
1029
+ }
1030
+ } ) ;
1019
1031
} , function ( cb ) {
1020
- fs . createReadStream ( submissionFile . path ) . pipe ( fs . createWriteStream ( savedFilePath ) ) ;
1021
-
1022
1032
//Now insert into upload table
1023
1033
_ . extend ( sqlParams , {
1024
1034
uploadId : uploadId ,
@@ -1029,7 +1039,7 @@ var submitForDevelopChallenge = function (api, connection, dbConnectionMap, next
1029
1039
fileName : uploadId + "_" + fileName
1030
1040
} ) ;
1031
1041
api . dataAccess . executeQuery ( "insert_upload" , sqlParams , dbConnectionMap , cb ) ;
1032
- } , function ( notUsed , cb ) {
1042
+ } , function ( notUsed , cb ) {
1033
1043
//Now check if the contest is a CloudSpokes one and if it needs to submit the thurgood job
1034
1044
if ( ! _ . isUndefined ( thurgoodPlatform ) && ! _ . isUndefined ( thurgoodLanguage ) && type === 'final' ) {
1035
1045
//Make request to the thurgood job api url
@@ -1609,7 +1619,7 @@ exports.submitForDevelopChallenge = {
1609
1619
name : "submitForDevelopChallenge" ,
1610
1620
description : "submitForDevelopChallenge" ,
1611
1621
inputs : {
1612
- required : [ "challengeId" , "submissionFile " ] ,
1622
+ required : [ "challengeId" , "fileName" , "fileData "] ,
1613
1623
optional : [ "type" ]
1614
1624
} ,
1615
1625
blockedConnectionTypes : [ ] ,
0 commit comments