Sanitize app names and shorten screenshot names

pull/349/head
Ashvini Wegodapola 9 months ago
parent c8cf4b86ca
commit 695624c455

@ -857,7 +857,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
ApplicationArtifact applicationArtifact, int tenantId) throws ResourceManagementException { ApplicationArtifact applicationArtifact, int tenantId) throws ResourceManagementException {
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
applicationReleaseDTO.setIconName(applicationArtifact.getIconName()); applicationReleaseDTO.setIconName(ApplicationManagementUtil.sanitizeName
(applicationArtifact.getIconName(), Constants.ICON_NAME));
applicationReleaseDTO.setBannerName(applicationArtifact.getBannerName()); applicationReleaseDTO.setBannerName(applicationArtifact.getBannerName());
Map<String, InputStream> screenshots = applicationArtifact.getScreenshots(); Map<String, InputStream> screenshots = applicationArtifact.getScreenshots();
@ -866,11 +867,14 @@ public class ApplicationManagerImpl implements ApplicationManager {
int counter = 1; int counter = 1;
for (String scName : screenshotNames) { for (String scName : screenshotNames) {
if (counter == 1) { if (counter == 1) {
applicationReleaseDTO.setScreenshotName1(scName); applicationReleaseDTO.setScreenshotName1(ApplicationManagementUtil.sanitizeName
(scName, Constants.SCREENSHOT_NAME + counter));
} else if (counter == 2) { } else if (counter == 2) {
applicationReleaseDTO.setScreenshotName2(scName); applicationReleaseDTO.setScreenshotName2(ApplicationManagementUtil.sanitizeName
(scName, Constants.SCREENSHOT_NAME + counter));
} else if (counter == 3) { } else if (counter == 3) {
applicationReleaseDTO.setScreenshotName3(scName); applicationReleaseDTO.setScreenshotName3(ApplicationManagementUtil.sanitizeName
(scName, Constants.SCREENSHOT_NAME + counter));
} }
counter++; counter++;
} }
@ -898,7 +902,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
applicationStorageManager applicationStorageManager
.deleteAppReleaseArtifact(applicationReleaseDTO.getAppHashValue(), Constants.ICON_ARTIFACT, .deleteAppReleaseArtifact(applicationReleaseDTO.getAppHashValue(), Constants.ICON_ARTIFACT,
applicationReleaseDTO.getIconName(), tenantId); applicationReleaseDTO.getIconName(), tenantId);
applicationReleaseDTO.setIconName(applicationArtifact.getIconName()); applicationReleaseDTO.setIconName(ApplicationManagementUtil.sanitizeName
(applicationArtifact.getIconName(), Constants.ICON_NAME));
} }
if (!StringUtils.isEmpty(applicationArtifact.getBannerName())){ if (!StringUtils.isEmpty(applicationArtifact.getBannerName())){
applicationStorageManager applicationStorageManager
@ -921,17 +926,20 @@ public class ApplicationManagerImpl implements ApplicationManager {
applicationStorageManager applicationStorageManager
.deleteAppReleaseArtifact(applicationReleaseDTO.getAppHashValue(), folderPath, .deleteAppReleaseArtifact(applicationReleaseDTO.getAppHashValue(), folderPath,
applicationReleaseDTO.getScreenshotName1(), tenantId); applicationReleaseDTO.getScreenshotName1(), tenantId);
applicationReleaseDTO.setScreenshotName1(scName); applicationReleaseDTO.setScreenshotName1(ApplicationManagementUtil.sanitizeName
(scName, Constants.SCREENSHOT_NAME + counter));
} else if (counter == 2) { } else if (counter == 2) {
applicationStorageManager applicationStorageManager
.deleteAppReleaseArtifact(applicationReleaseDTO.getAppHashValue(), folderPath, .deleteAppReleaseArtifact(applicationReleaseDTO.getAppHashValue(), folderPath,
applicationReleaseDTO.getScreenshotName2(), tenantId); applicationReleaseDTO.getScreenshotName2(), tenantId);
applicationReleaseDTO.setScreenshotName2(scName); applicationReleaseDTO.setScreenshotName2(ApplicationManagementUtil.sanitizeName
(scName, Constants.SCREENSHOT_NAME + counter));
} else if (counter == 3) { } else if (counter == 3) {
applicationStorageManager applicationStorageManager
.deleteAppReleaseArtifact(applicationReleaseDTO.getAppHashValue(), folderPath, .deleteAppReleaseArtifact(applicationReleaseDTO.getAppHashValue(), folderPath,
applicationReleaseDTO.getScreenshotName3(), tenantId); applicationReleaseDTO.getScreenshotName3(), tenantId);
applicationReleaseDTO.setScreenshotName3(scName); applicationReleaseDTO.setScreenshotName3(ApplicationManagementUtil.sanitizeName
(scName, Constants.SCREENSHOT_NAME + counter));
} }
counter++; counter++;
} }
@ -2428,7 +2436,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg); log.error(msg);
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
applicationDTO.setName(applicationUpdateWrapper.getName()); applicationDTO.setName(ApplicationManagementUtil.sanitizeName(applicationUpdateWrapper.getName(),
Constants.ApplicationProperties.NAME));
} }
if (!StringUtils.isEmpty(applicationUpdateWrapper.getSubMethod()) && !applicationDTO.getSubType() if (!StringUtils.isEmpty(applicationUpdateWrapper.getSubMethod()) && !applicationDTO.getSubType()
.equals(applicationUpdateWrapper.getSubMethod())) { .equals(applicationUpdateWrapper.getSubMethod())) {

@ -309,7 +309,8 @@ public class APIUtil {
if (param instanceof ApplicationWrapper){ if (param instanceof ApplicationWrapper){
ApplicationWrapper applicationWrapper = (ApplicationWrapper) param; ApplicationWrapper applicationWrapper = (ApplicationWrapper) param;
DeviceType deviceType = getDeviceTypeData(applicationWrapper.getDeviceType()); DeviceType deviceType = getDeviceTypeData(applicationWrapper.getDeviceType());
applicationDTO.setName(applicationWrapper.getName()); applicationDTO.setName(ApplicationManagementUtil.sanitizeName(applicationWrapper.getName(),
Constants.ApplicationProperties.NAME));
applicationDTO.setDescription(applicationWrapper.getDescription()); applicationDTO.setDescription(applicationWrapper.getDescription());
applicationDTO.setAppCategories(applicationWrapper.getCategories()); applicationDTO.setAppCategories(applicationWrapper.getCategories());
applicationDTO.setType(ApplicationType.ENTERPRISE.toString()); applicationDTO.setType(ApplicationType.ENTERPRISE.toString());
@ -323,7 +324,8 @@ public class APIUtil {
applicationDTO.setApplicationReleaseDTOs(applicationReleaseEntities); applicationDTO.setApplicationReleaseDTOs(applicationReleaseEntities);
} else if (param instanceof WebAppWrapper){ } else if (param instanceof WebAppWrapper){
WebAppWrapper webAppWrapper = (WebAppWrapper) param; WebAppWrapper webAppWrapper = (WebAppWrapper) param;
applicationDTO.setName(webAppWrapper.getName()); applicationDTO.setName(ApplicationManagementUtil.sanitizeName(webAppWrapper.getName(),
Constants.ApplicationProperties.NAME));
applicationDTO.setDescription(webAppWrapper.getDescription()); applicationDTO.setDescription(webAppWrapper.getDescription());
applicationDTO.setAppCategories(webAppWrapper.getCategories()); applicationDTO.setAppCategories(webAppWrapper.getCategories());
applicationDTO.setSubType(webAppWrapper.getSubMethod()); applicationDTO.setSubType(webAppWrapper.getSubMethod());
@ -331,13 +333,14 @@ public class APIUtil {
applicationDTO.setType(webAppWrapper.getType()); applicationDTO.setType(webAppWrapper.getType());
applicationDTO.setTags(webAppWrapper.getTags()); applicationDTO.setTags(webAppWrapper.getTags());
applicationDTO.setUnrestrictedRoles(webAppWrapper.getUnrestrictedRoles()); applicationDTO.setUnrestrictedRoles(webAppWrapper.getUnrestrictedRoles());
applicationReleaseEntities = webAppWrapper.getWebAppReleaseWrappers() applicationReleaseEntities = webAppWrapper.getWebAppReleaseWrappers()
.stream().map(APIUtil::releaseWrapperToReleaseDTO).collect(Collectors.toList()); .stream().map(APIUtil::releaseWrapperToReleaseDTO).collect(Collectors.toList());
applicationDTO.setApplicationReleaseDTOs(applicationReleaseEntities); applicationDTO.setApplicationReleaseDTOs(applicationReleaseEntities);
} else if (param instanceof PublicAppWrapper) { } else if (param instanceof PublicAppWrapper) {
PublicAppWrapper publicAppWrapper = (PublicAppWrapper) param; PublicAppWrapper publicAppWrapper = (PublicAppWrapper) param;
DeviceType deviceType = getDeviceTypeData(publicAppWrapper.getDeviceType()); DeviceType deviceType = getDeviceTypeData(publicAppWrapper.getDeviceType());
applicationDTO.setName(publicAppWrapper.getName()); applicationDTO.setName(ApplicationManagementUtil.sanitizeName(publicAppWrapper.getName(),
Constants.ApplicationProperties.NAME));
applicationDTO.setDescription(publicAppWrapper.getDescription()); applicationDTO.setDescription(publicAppWrapper.getDescription());
applicationDTO.setAppCategories(publicAppWrapper.getCategories()); applicationDTO.setAppCategories(publicAppWrapper.getCategories());
applicationDTO.setType(ApplicationType.PUBLIC.toString()); applicationDTO.setType(ApplicationType.PUBLIC.toString());
@ -352,7 +355,8 @@ public class APIUtil {
} else if (param instanceof CustomAppWrapper){ } else if (param instanceof CustomAppWrapper){
CustomAppWrapper customAppWrapper = (CustomAppWrapper) param; CustomAppWrapper customAppWrapper = (CustomAppWrapper) param;
DeviceType deviceType = getDeviceTypeData(customAppWrapper.getDeviceType()); DeviceType deviceType = getDeviceTypeData(customAppWrapper.getDeviceType());
applicationDTO.setName(customAppWrapper.getName()); applicationDTO.setName(ApplicationManagementUtil.sanitizeName(customAppWrapper.getName(),
Constants.ApplicationProperties.NAME));
applicationDTO.setDescription(customAppWrapper.getDescription()); applicationDTO.setDescription(customAppWrapper.getDescription());
applicationDTO.setAppCategories(customAppWrapper.getCategories()); applicationDTO.setAppCategories(customAppWrapper.getCategories());
applicationDTO.setType(ApplicationType.CUSTOM.toString()); applicationDTO.setType(ApplicationType.CUSTOM.toString());

@ -535,4 +535,28 @@ public class ApplicationManagementUtil {
packageNamesOfApps.add(adamId); packageNamesOfApps.add(adamId);
return applicationManager.getApplications(packageNamesOfApps); return applicationManager.getApplications(packageNamesOfApps);
} }
/**
* Sanitize app names and shorten icon/screenshot file names
*
* @param originalName Original name of the file which is being uploaded
* @param type Type - Name/Artifact(Icon, Screenshot, etc.)
* @return Sanitized and shortened file name
*/
public static String sanitizeName(String originalName, String type) {
String sanitizedName = originalName.replaceAll("[^a-zA-Z0-9.\\s-]", "");
if (type.equals(Constants.ApplicationProperties.NAME) && sanitizedName.length() > 350) {
sanitizedName = sanitizedName.substring(0, 350);
return sanitizedName;
} else if (Constants.ICON_NAME.equals(type) || Constants.SCREENSHOT_NAME.equals(type)) {
// Shortening icon/screenshot names
String fileExtension = "";
int dotIndex = originalName.lastIndexOf('.');
if (dotIndex >= 0) {
fileExtension = originalName.substring(dotIndex);
}
return type + fileExtension;
} else
return sanitizedName;
}
} }

Loading…
Cancel
Save