// gets the current container being edited from the request object
File currentFile = (File) req.getAttribute(WebKeys.FILE_EDIT);
// parent folder
String parent = ParamUtil.getString(req, "parent");
Folder folder = APILocator.getFolderAPI().find(parent, user, false);
//http://jira.dotmarketing.net/browse/DOTCMS-5899
if(InodeUtils.isSet(currentFile.getIdentifier())){
Identifier id = APILocator.getIdentifierAPI().find(currentFile);
if(id!=null && InodeUtils.isSet(id.getInode())){
String URI = id.getURI();
String uriPath = URI.substring(0,URI.lastIndexOf("/")+1);
String folderPath = APILocator.getIdentifierAPI().find(folder).getPath();
if(!uriPath.equals(folderPath)){
id.setURI(folderPath+currentFile.getFileName());
APILocator.getIdentifierAPI().save(id);
}
}
}
req.setAttribute(WebKeys.PARENT_FOLDER, folder); // Since the above query is expensive, save it into request object
if(currentFile!=null)
file.setIdentifier(currentFile.getIdentifier());
_checkPermissions(file, folder, user, httpReq);
// gets user id from request for modified user
String userId = user.getUserId();
boolean previousShowMenu = file.isShowOnMenu();
long uploadFileMaxSize = Long.parseLong(Config.getStringProperty("UPLOAD_FILE_MAX_SIZE"));
//Checking the max file size
java.io.File uploadedFile = uploadReq.getFile("uploadedFile");
if(currentFile!=null && UtilMethods.isSet(currentFile.getInode())){
java.io.File editedFile = new java.io.File(APILocator.getFileAPI().getAssetIOFile(currentFile).getPath()+"_text");
if(editedFile.exists()){
FileChannel ic = new FileInputStream(editedFile).getChannel();
FileChannel oc = new FileOutputStream(APILocator.getFileAPI().getAssetIOFile(currentFile)).getChannel();
ic.transferTo(0, ic.size(), oc);
ic.close();
oc.close();
}
}
//Do we have an uploaded file or not?
boolean haveUpload = (uploadedFile.exists() && uploadedFile.length()>0);
boolean tmpFile = true;
if(!haveUpload){
// look for a edited file in the users session, if it is there, use that
if(UtilMethods.isSet(fileForm.get_imageToolSaveFile())){
String x =fileForm.get_imageToolSaveFile().trim();
if( x != null){
x = PublicEncryptionFactory.decryptString(x);
uploadedFile = new java.io.File( x.trim());
haveUpload = (uploadedFile.exists() && uploadedFile.length()>0);
}
else{
x=null;
}
reqImpl.getHttpServletRequest().getSession().removeAttribute(WebKeys.IMAGE_TOOL_SAVE_FILES);
}
}
if(haveUpload) {
if ((uploadFileMaxSize > 0) && (uploadedFile.length() > uploadFileMaxSize)) {
// when there is an error saving should unlock working asset
WebAssetFactory.unLockAsset(currentFile);
throw new ActionException();
}
}
// CHECK THE FOLDER PATTERN
if (UtilMethods.isSet(file.getFileName()) && !APILocator.getFolderAPI().matchFilter(folder, file.getFileName())) {
SessionMessages.add(req, "error", "message.file_asset.error.filename.filters");
// when there is an error saving should unlock working asset
WebAssetFactory.unLockAsset(currentFile);
throw new ActionException("message.file_asset.error.filename.filters");
}
// Checking permissions
_checkPermissions(currentFile, folder, user, httpReq);
//Setting some flags
boolean editing = (InodeUtils.isSet(currentFile.getInode()));
if(!haveUpload){
uploadedFile = APILocator.getFileAPI().getAssetIOFile(currentFile);
tmpFile = false;
}
// if we don't have a file anywhere, die
if(!(uploadedFile.exists() && uploadedFile.length()>0)){
SessionMessages.add(req, "error", "message.file_asset.error.nofile");
// when there is an error saving should unlock working asset
WebAssetFactory.unLockAsset(currentFile);
throw new ActionException("message.file_asset.error.nofile");
}
String fileName = (editing) ? currentFile.getFileName() : fileForm.getFileName();
//getting mime type
String mimeType = APILocator.getFileAPI().getMimeType(fileName);
file.setMimeType(mimeType);
if (editing && haveUpload && !
APILocator.getFileAPI().getMimeType(fileName).equals(APILocator.getFileAPI().getMimeType(uploadedFile.getName()))) {
SessionMessages.add(req, "error", "message.file_asset.error.mimetype");
// when there is an error saving should unlock working asset
Logger.error(this.getClass(), "MimeType Mismatch:" + fileName + " : " + uploadedFile.getName());
WebAssetFactory.unLockAsset(currentFile);
HibernateUtil.rollbackTransaction();
throw new ActionException("message.file_asset.error.mimetype");
}
// checks if another identifier with the same name exists in the same
// folder
if ((!editing) && (APILocator.getFileAPI().fileNameExists(folder, fileName))) {
SessionMessages.add(req, "error", "message.file_asset.error.filename.exists");
throw new ActionException("message.file_asset.error.filename.exists");
}
//sets new file properties
file.setFileName(fileName);
file.setModUser(userId);
if (haveUpload) {
file.setSize((int)uploadedFile.length());
}
else {
file.setSize(currentFile.getSize());
file.setWidth(currentFile.getWidth());
file.setHeight(currentFile.getHeight());
}
//Saving the file, this creates the new version and save the new data
File workingFile = null;
workingFile = APILocator.getFileAPI().saveFile(file, uploadedFile, folder, user, false);
APILocator.getVersionableAPI().setWorking(file);
APILocator.getVersionableAPI().setLocked(file, false, user);
if(uploadedFile != null && uploadedFile.exists() &&tmpFile){
uploadedFile.delete();
}
SessionMessages.add(req, "message", "message.file_asset.save");
// for file in a popup
if (req.getParameter("popup") != null) {
req.setAttribute("fileInode", file.getInode() + "");
req.setAttribute("fileName", file.getFileName() + "");
}
req.setAttribute(WebKeys.FILE_FORM_EDIT, workingFile);
// copies the information back into the form bean
BeanUtils.copyProperties(form, req.getAttribute(WebKeys.FILE_FORM_EDIT));
//Refreshing the menues
if (previousShowMenu != file.isShowOnMenu()) {
//existing folder with different show on menu ... need to regenerate menu
RefreshMenus.deleteMenu(file);
CacheLocator.getNavToolCache().removeNav(folder.getHostId(), folder.getInode());
}
} catch (Exception e) {
Logger.error(this, "\n\n\nEXCEPTION IN FILE SAVING!!! " + e.getMessage(), e);
throw new ActionException(e.getMessage());