Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 4f25f08

Browse files
author
fireice.topcoder
committed
revert back to base64 approach for submit dev challenge submission API
1 parent 3af6d99 commit 4f25f08

File tree

3 files changed

+181
-92
lines changed

3 files changed

+181
-92
lines changed

actions/challenges.js

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,8 @@ var submitForDevelopChallenge = function (api, connection, dbConnectionMap, next
864864
ret = {},
865865
userId = connection.caller.userId,
866866
challengeId = Number(connection.params.challengeId),
867-
fileName,
867+
fileName = connection.params.fileName,
868+
fileData = connection.params.fileData,
868869
type = connection.params.type,
869870
error,
870871
resourceId,
@@ -879,8 +880,7 @@ var submitForDevelopChallenge = function (api, connection, dbConnectionMap, next
879880
thurgoodApiKey = process.env.THURGOOD_API_KEY || api.config.thurgoodApiKey,
880881
thurgoodJobId = null,
881882
multipleSubmissionPossible,
882-
savedFilePath = null,
883-
submissionFile = connection.params.submissionFile;
883+
savedFilePath = null;
884884

885885
async.waterfall([
886886
function (cb) {
@@ -893,7 +893,9 @@ var submitForDevelopChallenge = function (api, connection, dbConnectionMap, next
893893

894894
//Simple validations of the incoming parameters
895895
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');
897899

898900
if (error) {
899901
cb(error);
@@ -911,15 +913,7 @@ var submitForDevelopChallenge = function (api, connection, dbConnectionMap, next
911913
}
912914
}
913915

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-
921916
//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;
923917
if (fileName.length > 256) {
924918
cb(new BadRequestError("The file name is too long. It must be 256 characters or less."));
925919
return;
@@ -988,37 +982,53 @@ var submitForDevelopChallenge = function (api, connection, dbConnectionMap, next
988982
uploadId = generatedIds.uploadId;
989983
submissionId = generatedIds.submissionId;
990984

991-
var submissionPath;
985+
var submissionPath,
986+
filePathToSave,
987+
decodedFileData;
992988

993989
//The file output dir should be overwritable by environment variable
994990
submissionPath = api.config.submissionDir;
995991

996992
//The path to save is the folder with the name as <base submission path>
997993
//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;
1007995

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;
10141028
cb();
1015-
});
1016-
} else {
1017-
cb();
1018-
}
1029+
}
1030+
});
10191031
}, function (cb) {
1020-
fs.createReadStream(submissionFile.path).pipe(fs.createWriteStream(savedFilePath));
1021-
10221032
//Now insert into upload table
10231033
_.extend(sqlParams, {
10241034
uploadId: uploadId,
@@ -1029,7 +1039,7 @@ var submitForDevelopChallenge = function (api, connection, dbConnectionMap, next
10291039
fileName: uploadId + "_" + fileName
10301040
});
10311041
api.dataAccess.executeQuery("insert_upload", sqlParams, dbConnectionMap, cb);
1032-
}, function (notUsed, cb) {
1042+
}, function(notUsed, cb) {
10331043
//Now check if the contest is a CloudSpokes one and if it needs to submit the thurgood job
10341044
if (!_.isUndefined(thurgoodPlatform) && !_.isUndefined(thurgoodLanguage) && type === 'final') {
10351045
//Make request to the thurgood job api url
@@ -1609,7 +1619,7 @@ exports.submitForDevelopChallenge = {
16091619
name: "submitForDevelopChallenge",
16101620
description: "submitForDevelopChallenge",
16111621
inputs: {
1612-
required: ["challengeId", "submissionFile"],
1622+
required: ["challengeId", "fileName", "fileData"],
16131623
optional: ["type"]
16141624
},
16151625
blockedConnectionTypes: [],

apiary.apib

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2698,7 +2698,8 @@ Register a new user.
26982698
+ Request (application/json)
26992699

27002700
+ Parameters
2701-
+ submissionFile (required, string, `abcd.zip`) ... The submission zip
2701+
+ fileName (required, string, `abcd.zip`) ... The name of the file that is being uploaded
2702+
+ fileData (required, string, `SGVsbG8gV29ybGQ=`) ... The Base64 encoded content of the file being uploaded. The example value is 'Hello World' base64 encoded
27022703
+ type (optional, string, `checkpoint`) ... The type of submission. Can be 'final' or 'checkpoint'. If absent, 'final' is assumed.
27032704

27042705
+ Response 200 (application/json)

0 commit comments

Comments
 (0)