} catch (DotSecurityException e) {
Logger.error(DotWebdavHelper.class, e.getMessage(), e);
throw new IOException(e.getMessage());
}
Folder folder = new Folder();
try {
folder = folderAPI.findFolderByPath(folderName, host,user,false);
} catch (Exception e2) {
Logger.error(this, e2.getMessage(), e2);
}
if (host != null && InodeUtils.isSet(host.getInode()) && InodeUtils.isSet(folder.getInode())) {
IFileAsset destinationFile = null;
java.io.File workingFile = null;
Folder parent = null;
Contentlet fileAssetCont = null;
Identifier identifier = APILocator.getIdentifierAPI().find(host, path);
if(identifier!=null && InodeUtils.isSet(identifier.getId()) && identifier.getAssetType().equals("contentlet")){
fileAssetCont = APILocator.getContentletAPI().findContentletByIdentifier(identifier.getId(), false, APILocator.getLanguageAPI().getDefaultLanguage().getId(), user, false);
workingFile = fileAssetCont.getBinary(FileAssetAPI.BINARY_FIELD);
destinationFile = APILocator.getFileAssetAPI().fromContentlet(fileAssetCont);
parent = APILocator.getFolderAPI().findFolderByPath(identifier.getParentPath(), host, user, false);
if(fileAssetCont.isArchived()) {
APILocator.getContentletAPI().unarchive(fileAssetCont, user, false);
}
}else if(identifier!=null && InodeUtils.isSet(identifier.getId())){
destinationFile = fileAPI.getFileByURI(path, host, false, user, false);
// inode{1}/inode{2}/inode.file_extension
workingFile = fileAPI.getAssetIOFile((File)destinationFile);
if(destinationFile.isArchived()) {
WebAssetFactory.unArchiveAsset((File)destinationFile);
}
}
//http://jira.dotmarketing.net/browse/DOTCMS-1873
//To clear velocity cache
if(workingFile!=null){
DotResourceCache vc = CacheLocator.getVeloctyResourceCache();
vc.remove(ResourceManager.RESOURCE_TEMPLATE + workingFile.getPath());
}
InputStream is = content;
byte[] currentData = IOUtils.toByteArray(is);
if(destinationFile==null){
Contentlet fileAsset = new Contentlet();
Structure faStructure = StructureCache.getStructureByInode(folder.getDefaultFileType());
Field fieldVar = faStructure.getFieldVar(FileAssetAPI.BINARY_FIELD);
fileAsset.setStructureInode(folder.getDefaultFileType());
fileAsset.setFolder(folder.getInode());
if (currentData != null) {
java.io.File tempUserFolder = new java.io.File(APILocator.getFileAPI().getRealAssetPathTmpBinary() + java.io.File.separator + user.getUserId() +
java.io.File.separator + fieldVar.getFieldContentlet());
if (!tempUserFolder.exists())
tempUserFolder.mkdirs();
java.io.File fileData = new java.io.File(tempUserFolder.getAbsolutePath() + java.io.File.separator + fileName);
if(fileData.exists())
fileData.delete();
// Saving the new working data
FileChannel writeCurrentChannel = new FileOutputStream(fileData).getChannel();
writeCurrentChannel.truncate(0);
ByteBuffer buffer = ByteBuffer.allocate(currentData.length);
buffer.put(currentData);
buffer.position(0);
writeCurrentChannel.write(buffer);
writeCurrentChannel.force(false);
writeCurrentChannel.close();
fileAsset.setStringProperty(FileAssetAPI.TITLE_FIELD, fileName);
fileAsset.setStringProperty(FileAssetAPI.FILE_NAME_FIELD, fileName);
fileAsset.setBinary(FileAssetAPI.BINARY_FIELD, fileData);
fileAsset.setHost(host.getIdentifier());
fileAsset=APILocator.getContentletAPI().checkin(fileAsset, user, false);
//Validate if the user have the right permission before
if(isAutoPub && !perAPI.doesUserHavePermission(fileAsset, PermissionAPI.PERMISSION_PUBLISH, user) ){
APILocator.getContentletAPI().archive(fileAsset, APILocator.getUserAPI().getSystemUser(), false);
APILocator.getContentletAPI().delete(fileAsset, APILocator.getUserAPI().getSystemUser(), false);
throw new DotSecurityException("User does not have permission to publish contentlets");
}else if(!isAutoPub && !perAPI.doesUserHavePermission(fileAsset, PermissionAPI.PERMISSION_EDIT, user)){
APILocator.getContentletAPI().archive(fileAsset, APILocator.getUserAPI().getSystemUser(), false);
APILocator.getContentletAPI().delete(fileAsset, APILocator.getUserAPI().getSystemUser(), false);
throw new DotSecurityException("User does not have permission to edit contentlets");
}
if(isAutoPub && perAPI.doesUserHavePermission(fileAsset, PermissionAPI.PERMISSION_PUBLISH, user)) {
APILocator.getContentletAPI().publish(fileAsset, user, false);
Date currentDate = new Date();
fileResourceCache.add(resourceUri + "|" + user.getUserId(), currentDate.getTime());
}
}
}else{
if(destinationFile instanceof File){
// Save the file size
File file = fileAPI.getFileByURI(path, host, false, user, false);
file.setSize(currentData.length);
file.setModDate(modifiedDate);
file.setModUser(user.getUserId());
try {
HibernateUtil.saveOrUpdate(file);
} catch (DotHibernateException e1) {
Logger.error(this,e1.getMessage(), e1);
}
}
if (currentData != null) {
// Saving the new working data
java.io.File fileData;
if(destinationFile instanceof File) {
fileData=workingFile;
}
else {
Structure faStructure = StructureCache.getStructureByInode(folder.getDefaultFileType());
Field fieldVar = faStructure.getFieldVar(FileAssetAPI.BINARY_FIELD);
java.io.File tempUserFolder = new java.io.File(APILocator.getFileAPI().getRealAssetPathTmpBinary() + java.io.File.separator + user.getUserId() +
java.io.File.separator + fieldVar.getFieldContentlet());
if (!tempUserFolder.exists())
tempUserFolder.mkdirs();
fileData = new java.io.File(tempUserFolder.getAbsolutePath() + java.io.File.separator + fileName);
if(fileData.exists())
fileData.delete();
}
// Saving the new working data
FileChannel writeCurrentChannel = new FileOutputStream(fileData).getChannel();
writeCurrentChannel.truncate(0);
ByteBuffer buffer = ByteBuffer.allocate(currentData.length);
buffer.put(currentData);
buffer.position(0);
writeCurrentChannel.write(buffer);
writeCurrentChannel.force(false);
writeCurrentChannel.close();
Logger.debug(this, "WEBDAV fileName:" + fileName + ":" + fileData.getAbsolutePath());
if(destinationFile instanceof File){
// checks if it's an image
if (UtilMethods.isImage(fileName) && workingFile != null) {
try {
// gets image height
BufferedImage img = javax.imageio.ImageIO.read(workingFile);
if(img != null){
int height = img.getHeight();
((File)destinationFile).setHeight(height);
// gets image width
int width = img.getWidth();
((File)destinationFile).setWidth(width);
}
} catch (Exception ioe) {
Logger.error(this.getClass(), ioe.getMessage(), ioe);
}
}
}else{
fileAssetCont.setInode(null);
fileAssetCont.setFolder(parent.getInode());
fileAssetCont.setBinary(FileAssetAPI.BINARY_FIELD, fileData);
fileAssetCont = APILocator.getContentletAPI().checkin(fileAssetCont, user, false);
if(isAutoPub && perAPI.doesUserHavePermission(fileAssetCont, PermissionAPI.PERMISSION_PUBLISH, user))
APILocator.getContentletAPI().publish(fileAssetCont, user, false);
}