}
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);