ActionMessages errors = new ActionMessages();
String fileTypes = SystemConfigConstants.CONTENT_TYPE ;
String avatarDir = getSysProp().getValue("AVATAR.FILEPATH");
MyAlumniUserContainer continer = getUserContainer(request);
int maxFileSize = 0;
int maxHeight = 0;
int maxWidth = 0;
String overwrite = "false";
if (isCancelled(request)){
return mapping.findForward(BaseConstants.FWD_CANCEL);
}
MemberVO token = getCurrentLoggedInUser(request);
MemberForm memberForm = (MemberForm)form;
if (!memberSecurityCheck(request, token)){
return mapping.findForward(BaseConstants.FWD_LOGIN);
}
// Set Max Size
try{
maxFileSize = Integer.parseInt(getAppProp().getValue("avatar.image.size").trim());
}
catch(Exception e){
maxFileSize = 120000; // 120000 Bytes = 120 KB
}
// Set Max Height
try{
maxHeight = Integer.parseInt(getAppProp().getValue("avatar.image.height").trim());
}
catch(Exception e){
maxHeight = 200; // 200 px
}
// Set Max Width
try{
maxWidth = Integer.parseInt(getAppProp().getValue("avatar.image.width").trim());
}
catch(Exception e){
maxWidth = 200; // 200 px
}
FormFile importFile = memberForm.getAvatarUpload();
overwrite = StringUtil.safeString(memberForm.getAvatarUploadOverwrite());
String importFileName = getCurrentLoggedInUser(request).getMemberUserName() + "." + getFileExtensionForImageReader(importFile.getFileName());
int size = importFile.getFileSize();
//-------------------- VALIDATE THE IMAGE -----------------------------------------
// check width and heigh of image
logger.debug(importFileName + " ext = " + getFileExtensionForImageReader(importFileName));
Iterator readers = ImageIO.getImageReadersBySuffix(getFileExtensionForImageReader(importFileName));
ImageReader reader = (ImageReader) readers.next();
try {
ImageInputStream iis = ImageIO.createImageInputStream(importFile.getInputStream());
reader.setInput(iis, true);
int width = reader.getWidth(0);
int height = reader.getHeight(0);
logger.debug(importFile.getFileName() + ": width=" + width + ", height=" + height);
if (width > maxWidth || height > maxHeight){
errors.add(BaseConstants.WARN_KEY,new ActionMessage("error.dimensions", width, height, maxWidth, maxHeight ));
saveMessages(request, errors);
return mapping.getInputForward();
}
} catch (IOException e) {
System.err.println(e.getMessage() + ": can't open");
errors.add(BaseConstants.FATAL_KEY,new ActionMessage("error.notreadable"));
saveMessages(request, errors);
return mapping.getInputForward();
}
// check file name
if (importFileName.indexOf(" ") > -1) {
errors.add(BaseConstants.WARN_KEY,new ActionMessage("error.filename", importFileName));
saveMessages(request, errors);
return mapping.getInputForward();
}
//boolean validImageName = false;
/* StringTokenizer st0 = new StringTokenizer(importFileName, ".");
if (st0.hasMoreTokens()) {
if (token.getMemberUserName().equals(st0.nextToken())) {
//validImageName = true;
}
else{
errors.add(BaseConstants.WARN_KEY, new ActionMessage("error.fileusername", token.getMemberUserName(),importFileName ));
saveMessages(request, errors);
return mapping.getInputForward();
}
}*/
File f = new File(avatarDir + importFileName);
if ( f.exists() && (overwrite.equalsIgnoreCase("false") || overwrite.equalsIgnoreCase(""))){
continer.setOverWriteAvatar(true);
errors.add(BaseConstants.WARN_KEY, new ActionMessage("error.filename.exist"));
saveMessages(request, errors);
return mapping.getInputForward();
}
if ( size > maxFileSize){
errors.add(BaseConstants.WARN_KEY, new ActionMessage("error.filetoobig", String.valueOf(size), String.valueOf(maxFileSize)));
saveMessages(request, errors);
return mapping.getInputForward();
}
boolean validImageExtension = false;
StringTokenizer st = new StringTokenizer(fileTypes, ",");
logger.debug("Current Type = " + importFile.getContentType());
while (st.hasMoreTokens()) {
if ( importFile.getContentType().equalsIgnoreCase(st.nextToken())){
validImageExtension = true;
}
}
// check file extension
if (!validImageExtension){
errors.add(BaseConstants.WARN_KEY, new ActionMessage("error.imageext", String.valueOf(fileTypes)));
saveMessages(request, errors);
return mapping.getInputForward();
}
// apend the file extension
//avatar = avatar + "." + importFile.getContentType();
//-------------------------------------------------------------
if(!uploadFromLocalDrive(importFile, importFileName ,avatarDir)){
errors.add(BaseConstants.FATAL_KEY, new ActionMessage("errors.technical.difficulty"));
saveMessages(request, errors);
return mapping.getInputForward();
}
memService.updateMemberAvatar(importFileName, token.getMemberUserName(), getLastModifiedBy(request));
continer.setOverWriteAvatar(false);
continer.setAvatar(importFileName);
MemberVO memberVO = memService.getMemberProfileByUserName(token.getMemberUserName());
BeanUtils.copyProperties(memberForm, memberVO);
return mapping.findForward(BaseConstants.FWD_SUCCESS);
}