List<Host> hostList = new ArrayList<Host>();
String hosts = loadPluginConfigProperty(pluginId, "hosts.name");
if(UtilMethods.isSet(hosts)){
for(String hostname : hosts.split(",")){
Host host = hostAPI.findByName(hostname, systemUser, false);
hostList.add(host);
}
}else{
Host host = hostAPI.findDefaultHost(systemUser, false);
hostList.add(host);
}
Enumeration resources = jar.entries();
while(resources.hasMoreElements()){
JarEntry entry = (JarEntry) resources.nextElement();
// find the files inside the dotcms folder in the jar to copy on backend with this reg expression ("dotcms\\/.*\\.([^\\.]+)$")
if(entry.getName().matches("dotcms\\/.*\\.([^\\.]+)$") ){
String filePathAndName=entry.getName().substring(7);
String filePath = "";
if(filePathAndName.lastIndexOf("/") != -1){
filePath = filePathAndName.substring(0, filePathAndName.lastIndexOf("/"));
}
String fileName = filePathAndName.substring(filePathAndName.lastIndexOf("/")+1);
String pluginFolderPath = "/plugins/"+pluginId;
Logger.debug(this,"files in dotcms:"+filePathAndName+"\n");
//Create temporary file with the inputstream to be used in the FileFactory
InputStream input = jar.getInputStream(entry);
File temporaryFile = new File("file.temp");
OutputStream output=new FileOutputStream(temporaryFile);
byte buf[]=new byte[1024];
int len;
while((len=input.read(buf))>0){
output.write(buf,0,len);
}
output.close();
input.close();
for(Host host : hostList){
Folder folder = APILocator.getFolderAPI().findFolderByPath(pluginFolderPath + "/" + filePath,host,APILocator.getUserAPI().getSystemUser(),false);
if( !InodeUtils.isSet(folder.getInode())){
folder = APILocator.getFolderAPI().createFolders(pluginFolderPath + "/" + filePath, host,APILocator.getUserAPI().getSystemUser(),false);
}
//GetPrevious version if exists
IFileAsset currentFile = null;
Identifier currentId = APILocator.getIdentifierAPI().find(host, pluginFolderPath+"/"+filePathAndName);
if(currentId!=null && InodeUtils.isSet(currentId.getId()) && currentId.getAssetType().equals("contentlet")){
Contentlet cont = APILocator.getContentletAPI().findContentletByIdentifier(currentId.getId(), true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), APILocator.getUserAPI().getSystemUser(),false);
if(cont!=null && InodeUtils.isSet(cont.getInode())){
currentFile = APILocator.getFileAssetAPI().fromContentlet(cont);
cont.setStringProperty(FileAssetAPI.TITLE_FIELD, UtilMethods.getFileName(fileName));
cont.setFolder(folder.getInode());
cont.setHost(host.getIdentifier());
cont.setBinary(FileAssetAPI.BINARY_FIELD, temporaryFile);
APILocator.getContentletAPI().checkin(cont, APILocator.getUserAPI().getSystemUser(),false);
APILocator.getVersionableAPI().setWorking(cont);
APILocator.getVersionableAPI().setLive(cont);
if (cont.isLive()){
LiveCache.removeAssetFromCache(cont);
LiveCache.addToLiveAssetToCache(cont);
}else{
LiveCache.removeAssetFromCache(cont);
LiveCache.addToLiveAssetToCache(cont);
}
WorkingCache.removeAssetFromCache(cont);
WorkingCache.addToWorkingAssetToCache(cont);
}
}else if(currentId!=null && InodeUtils.isSet(currentId.getId())){
currentFile = APILocator.getFileAPI().getFileByURI(pluginFolderPath+"/"+filePathAndName, host, true, APILocator.getUserAPI().getSystemUser(),false);
com.dotmarketing.portlets.files.model.File file = new com.dotmarketing.portlets.files.model.File();
file.setFileName(fileName);
file.setFriendlyName(UtilMethods.getFileName(fileName));
file.setTitle(UtilMethods.getFileName(fileName));
file.setMimeType(APILocator.getFileAPI().getMimeType(fileName));
file.setOwner(systemUser.getUserId());
file.setModUser(systemUser.getUserId());
file.setModDate(new Date());
file.setParent(folder.getIdentifier());
file.setSize((int)temporaryFile.length());
HibernateUtil.saveOrUpdate(file);
APILocator.getFileAPI().invalidateCache(file);
// get the file Identifier
Identifier ident = null;
if (InodeUtils.isSet(currentFile.getInode())){
ident = APILocator.getIdentifierAPI().find((com.dotmarketing.portlets.files.model.File)currentFile);
APILocator.getFileAPI().invalidateCache((com.dotmarketing.portlets.files.model.File)currentFile);
}else{
ident = new Identifier();
}
//Saving the file, this creates the new version and save the new data
com.dotmarketing.portlets.files.model.File workingFile = null;
workingFile = APILocator.getFileAPI().saveFile(file, temporaryFile, folder, systemUser, false);
APILocator.getVersionableAPI().setWorking(workingFile);
APILocator.getVersionableAPI().setLive(workingFile);
APILocator.getFileAPI().invalidateCache(workingFile);
ident = APILocator.getIdentifierAPI().find(workingFile);
//updating caches
if (workingFile.isLive()){
LiveCache.removeAssetFromCache(workingFile);
LiveCache.addToLiveAssetToCache(workingFile);
}else{
LiveCache.removeAssetFromCache(file);
LiveCache.addToLiveAssetToCache(file);
}
WorkingCache.removeAssetFromCache(workingFile);
WorkingCache.addToWorkingAssetToCache(workingFile);
//Publish the File
PublishFactory.publishAsset(workingFile, systemUser, false);
}else{
Contentlet cont = new Contentlet();
cont.setStructureInode(folder.getDefaultFileType());
cont.setStringProperty(FileAssetAPI.TITLE_FIELD, UtilMethods.getFileName(fileName));
cont.setFolder(folder.getInode());
cont.setHost(host.getIdentifier());
cont.setBinary(FileAssetAPI.BINARY_FIELD, temporaryFile);
APILocator.getContentletAPI().checkin(cont, APILocator.getUserAPI().getSystemUser(),false);
APILocator.getVersionableAPI().setWorking(cont);
APILocator.getVersionableAPI().setLive(cont);
if (cont.isLive()){