Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix success response code on jpeg images #372

116 changes: 110 additions & 6 deletions app/controller/NavigationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,28 @@ SDL.NavigationController = Em.Object.create(
searchAddress: request.params.address
}
);
FFW.Navigation.sendNavigationResult(
SDL.SDLModel.data.resultCode.SUCCESS,
request.id,
request.method
);

var callback = function(failed) {
var WARNINGS = SDL.SDLModel.data.resultCode.WARNINGS;
var SUCCESS = SDL.SDLModel.data.resultCode.SUCCESS;

FFW.Navigation.sendNavigationResult(
failed ? WARNINGS : SUCCESS,
request.id,
request.method,
failed ? "Requested image(s) not found" : null
);
}

if (!SDL.SDLModel.validateImagesInRequest(request.id, callback, [request.params.locationImage])) {
FFW.Navigation.sendNavigationResult(
SDL.SDLModel.data.resultCode.WARNINGS,
request.id,
request.method
);
}
},

/**
* Navigation view List Button action handler
* Opens selected WayPoint structure
Expand Down Expand Up @@ -407,6 +423,94 @@ SDL.NavigationController = Em.Object.create(
},
2000
); // Allow time for the initial map display
}
},

/**
* @desc Verifies if image is an PNG image,
* accordingly to file extension.
* @param imagePath - path to image
* @return {Boolean} true if image is PNG and false otherwise
*/
isPng: function(imagePath) {
const img_extension = '.png';
var search_offset = imagePath.lastIndexOf('.');
return imagePath.includes(img_extension, search_offset);
Comment on lines +435 to +437
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const img_extension = '.png';
var search_offset = imagePath.lastIndexOf('.');
return imagePath.includes(img_extension, search_offset);
return imagePath.endsWith('.png');

},

/**
* @desc Collects images paths from request
* and calls validation function.
* @param request - request data
*/
validateIcons: function(request) {
var params = request.params;
imageList = [];
var nonPngCounter = 0;

if(params.turnList) {
var countList=params.turnList.length;
for(var i = 0; i < countList; i++) {
if(params.turnList[i].turnIcon) {
var iconPath = params.turnList[i].turnIcon.value;
if(!this.isPng(iconPath)) {
delete params.turnList[i].turnIcon;
nonPngCounter++;
continue;
}
imageList.push(iconPath);
}
}
}
if(params.softButtons) {
var countButtons=params.softButtons.length;
for(var i=0;i<countButtons;i++) {
if(params.softButtons[i].image) {
var iconPath = params.softButtons[i].image.value;
if(!this.isPng(iconPath)) {
delete params.softButtons[i].image;
nonPngCounter++;
continue;
}
imageList.push(iconPath);
}
}
}
if(params.turnIcon) {
if(!this.isPng(params.turnIcon.value)) {
delete params.turnIcon;
nonPngCounter++;
} else {
imageList.push(params.turnIcon.value);
}
}
if(params.nextTurnIcon) {
if(!this.isPng(params.nextTurnIcon.value)) {
delete params.nextTurnIcon;
nonPngCounter++;
} else {
imageList.push(params.nextTurnIcon.value);
}
}

if(nonPngCounter > 0) {
FFW.Navigation.sendNavigationResult(
SDL.SDLModel.data.resultCode.WARNINGS,
request.id,
request.method,
);
return;
}

var callback = function(failed) {
var WARNINGS = SDL.SDLModel.data.resultCode.WARNINGS;
var SUCCESS = SDL.SDLModel.data.resultCode.SUCCESS;
FFW.Navigation.sendNavigationResult(
failed ? WARNINGS : SUCCESS,
request.id,
request.method,
failed ? "Requested image(s) not found" : null);
}
SDL.SDLModel.validateImages(request.id, callback, imageList);
},
}
);
4 changes: 2 additions & 2 deletions app/controller/sdl/Abstract/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,11 +730,11 @@ SDL.SDLController = Em.Object.extend(
* Method to sent notification ABORTED for PerformInteractionChoise
*/
interactionChoiseCloseResponse: function(appID, result, choiceID,
manualTextEntry) {
manualTextEntry, additionalInfo) {
FFW.UI.interactionResponse(
SDL.SDLController.getApplicationModel(
appID
).activeRequests.uiPerformInteraction, result, choiceID, manualTextEntry
).activeRequests.uiPerformInteraction, result, choiceID, manualTextEntry, additionalInfo
);
SDL.SDLModel.data.set('interactionData.vrHelpTitle', null);
SDL.SDLModel.data.set('interactionData.vrHelp', null);
Expand Down
46 changes: 33 additions & 13 deletions app/model/sdl/Abstract/AppModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,17 +521,26 @@ SDL.ABSAppModel = Em.Object.extend(
}
if(request.params.cmdIcon){
var image = request.params.cmdIcon.value;
var search_offset = image.lastIndexOf('.');
str='.png';
var isPng=image.includes(str, search_offset);
if(!isPng){
FFW.UI.sendUIResult(
SDL.SDLModel.data.resultCode.WARNINGS, request.id,
request.method
);
if(!SDL.NavigationController.isPng(image)) {
FFW.UI.sendUIResult(
SDL.SDLModel.data.resultCode.WARNINGS, request.id,
request.method);
return;
}

var callback = function(failed) {
var WARNINGS = SDL.SDLModel.data.resultCode.WARNINGS;
var SUCCESS = SDL.SDLModel.data.resultCode.SUCCESS;

FFW.UI.sendUIResult(
failed ? WARNINGS : SUCCESS,
request.id,
request.method,
failed ? "Requested image(s) not found" : null);
}
SDL.SDLModel.validateImages(request.id, callback, [image]);
return;
}
}
if (request.id >= 0) {
FFW.UI.sendUIResult(
SDL.SDLModel.data.resultCode.SUCCESS, request.id,
Expand Down Expand Up @@ -602,10 +611,21 @@ SDL.ABSAppModel = Em.Object.extend(
SDL.SDLController.buttonsSort(parentID, this.appID);
SDL.OptionsView.commands.refreshItems();
}
FFW.UI.sendUIResult(
SDL.SDLModel.data.resultCode.SUCCESS, request.id,
request.method
);
var callback = function(failed) {
var WARNINGS = SDL.SDLModel.data.resultCode.WARNINGS;
var SUCCESS = SDL.SDLModel.data.resultCode.SUCCESS;

FFW.UI.sendUIResult(
failed ? WARNINGS : SUCCESS,
request.id,
request.method,
failed ? "Requested image(s) not found" : null);
}
var imageList = [];
if(request.params.menuIcon) {
imageList.push(request.params.menuIcon.value);
}
SDL.SDLModel.validateImages(request.id, callback, imageList);
} else {
FFW.UI.sendError(
SDL.SDLModel.data.resultCode.REJECTED, request.id,
Expand Down
172 changes: 172 additions & 0 deletions app/model/sdl/Abstract/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,22 @@ SDL.SDLModel = Em.Object.extend({
request.params, messageRequestId
);
}

var callback = function(failed) {
if(failed) {
FFW.UI.sendUIResult(SDL.SDLModel.data.resultCode.WARNINGS,
request.id,
request.method,
"Requested image(s) not found");
}
};

if (!SDL.SDLModel.validateImagesInRequest(request.id, callback, [request.params.softButtons])) {
FFW.UI.sendUIResult(SDL.SDLModel.data.resultCode.WARNINGS,
request.id,
request.method);
}

return true;
} else {
FFW.UI.sendError(SDL.SDLModel.data.resultCode.REJECTED, request.id,
Expand Down Expand Up @@ -1011,6 +1027,139 @@ SDL.SDLModel = Em.Object.extend({
}
},

/**
* List of images(paths to images) to check.
*/
imageCheckList: {},

/**
* @description Checks images in request provided inside 'objectsWithImages' param.
* @param requestID {Integer} Id of request
* @param callback {Function} User defined callback
* @param objectsWithImages {Array} Array of data structures, specific to request,
* that contain images.
* @return {Boolean} Returns true, if all images have valid extension.
* Returns false, if at least 1 image has invalid extension.
*/
validateImagesInRequest: function(requestID, callback, objectsWithImages) {
if (!Array.isArray(objectsWithImages) ||
(Array.isArray(objectsWithImages) && objectsWithImages.length == 0)) {
return false;
}

var imageList = [];
var allImagesValid = true;

var checkExtension = function(img) {
if(!img) {
return;
}
if(img.isTemplate && !SDL.NavigationController.isPng(img.value)) {
allImagesValid = false;
return;
}
imageList.push(img.value);
}

objectsWithImages.forEach(objWithImage => {
if(Array.isArray(objWithImage)) {
objWithImage.forEach(img => {
checkExtension(img.image);
});
} else {
checkExtension(objWithImage);
}
});

if(!allImagesValid) {
return false;
}

this.validateImages(requestID, callback, imageList);
return true;
},

/**
* @function validateImages
* @description Checks if image exists by path provided in request data
* @param requestID - request id, to which icons belong
* @param callback - user callback after check
* @param imageList - list of paths to check
*/
validateImages: function(requestID, callback, imageList) {
if(imageList == null || imageList.length == 0) {
callback(false);
return;
}

this.imageCheckList[requestID] = [];
const filteredImageList = imageList.filter(function(item, pos) {
return imageList.indexOf(item) == pos;
});

filteredImageList.forEach(image => {
this.imageCheckList[requestID].push({
'path': image,
'checkResult': null
});
});

for(var i = 0; i < this.imageCheckList[requestID].length; i++) {
var image = new Image();
image.onload = function() {
for(var i = 0; i < SDL.SDLModel.imageCheckList[requestID].length; i++) {
var formattedImgPath = this.src.substring(this.src.indexOf('://') + '://'.length);
var path = SDL.SDLModel.imageCheckList[requestID][i].path;
if(path === formattedImgPath) {
SDL.SDLModel.imageCheckList[requestID][i].checkResult = true;
break;
}
}
SDL.SDLModel.finalizeImageValidation(requestID, callback);
};
image.onerror = function() {
for(var i = 0; i < SDL.SDLModel.imageCheckList[requestID].length; i++) {
var formattedImgPath = this.src.substring(this.src.indexOf('://') + '://'.length);
var path = SDL.SDLModel.imageCheckList[requestID][i].path;
if(path === formattedImgPath) {
SDL.SDLModel.imageCheckList[requestID][i].checkResult = false;
break;
}
}
SDL.SDLModel.finalizeImageValidation(requestID, callback);
};
image.src = this.imageCheckList[requestID][i].path;
}
},

/**
* @function finalizeImageValidation
* @description Collects result of images validation.
* If validation is finished - calls user callback function.
* @param callback - user callback.
*/
finalizeImageValidation: function(requestID, callback) {
var failed = false;
var BreakException = {};
try {
SDL.SDLModel.imageCheckList[requestID].forEach(image => {
if (image.checkResult === null) {
throw BreakException;
}
if (!image.checkResult) {
failed = true;
}
});
} catch (exception) {
if (exception == BreakException) {
return;
}
}

delete SDL.SDLModel.imageCheckList.requestID;
callback(failed);
},

/**
* Method to call function from DeviceListView to show list of connected
* devices
Expand Down Expand Up @@ -1168,6 +1317,29 @@ SDL.SDLModel = Em.Object.extend({
message.id, 'UI.PerformInteraction');
return true;
}

var callback = function(failed) {
var WARNINGS = SDL.SDLModel.data.resultCode.WARNINGS;
var SUCCESS = SDL.SDLModel.data.resultCode.SUCCESS;

SDL.InteractionChoicesView.imageCheckInfo.resultCode = failed ? WARNINGS : SUCCESS;
SDL.InteractionChoicesView.imageCheckInfo.info = failed ? "Requested image(s) not found" : null;
}

var imagesToCheck = [];
imagesToCheck.push(message.params.vrHelp);
message.params.choiceSet.forEach(choice => {
if(choice.image) {
imagesToCheck.push(choice.image);
}
if(choice.secondaryImage) {
imagesToCheck.push(choice.secondaryImage);
}
});

if (!SDL.SDLModel.validateImagesInRequest(message.id, callback, imagesToCheck)) {
SDL.InteractionChoicesView.imageCheckInfo.resultCode = SDL.SDLModel.data.resultCode.WARNINGS;
}

SDL.SDLController.getApplicationModel(message.params.appID)
.activeRequests.uiPerformInteraction = message.id;
Expand Down
Loading