/** * Copy a file into the given directory OR host * * @param file File to be copied * @param parent Destination Folder * @param host Destination host * @return true if copy success, false otherwise * @throws IOException * @throws DotHibernateException */ private File copyFile(File file, Folder parent, Host host) throws DotDataException, IOException { File newFile = new File(); newFile.copy(file); // gets filename before extension String fileName = com.dotmarketing.util.UtilMethods.getFileName(file.getFileName()); // gets file extension String fileExtension = com.dotmarketing.util.UtilMethods.getFileExtension(file.getFileName()); Boolean fileNameExists; if (parent != null) { fileNameExists = fileNameExists(parent, file.getFileName()); } else { fileNameExists = fileNameExists(APILocator.getFolderAPI().findSystemFolder(), file.getFileName()); } // Setting file name if (fileNameExists) { // adds "copy" word to the filename newFile.setFileName(fileName + "_copy." + fileExtension); newFile.setFriendlyName(file.getFriendlyName() + " (COPY) "); } else { newFile.setFileName(fileName + "." + fileExtension); } Identifier identifier; if (parent != null) { identifier = APILocator.getIdentifierAPI().createNew(newFile, parent); } else { identifier = APILocator.getIdentifierAPI().createNew(newFile, host); } newFile.setIdentifier(identifier.getInode()); // persists the webasset HibernateUtil.saveOrUpdate(newFile); saveFileData(file, newFile, null); Logger.debug(FileFactory.class, "identifier=" + identifier.getURI()); WorkingCache.removeAssetFromCache(newFile); WorkingCache.addToWorkingAssetToCache(newFile); PermissionAPI permissionAPI = APILocator.getPermissionAPI(); try { APILocator.getVersionableAPI().setWorking(newFile); if (file.isLive()) APILocator.getVersionableAPI().setLive(newFile); } catch (DotStateException e) { Logger.error(this, e.getMessage()); } catch (DotSecurityException e) { Logger.error(this, e.getMessage()); } // Copy permissions permissionAPI.copyPermissions(file, newFile); return newFile; }
@SuppressWarnings({"unchecked", "deprecation"}) public boolean renameFile(File file, String newName) throws DotStateException, DotDataException, DotSecurityException { // getting old file properties String oldFileName = file.getFileName(); String ext = UtilMethods.getFileExtension(oldFileName); Folder folder = APILocator.getFolderAPI() .findParentFolder(file, APILocator.getUserAPI().getSystemUser(), false); Identifier ident = APILocator.getIdentifierAPI().find(file); String newFileName = newName; if (UtilMethods.isSet(ext)) { newFileName = newFileName + "." + ext; } if (fileNameExists(folder, newFileName) || file.isLocked()) return false; List<Versionable> versions = APILocator.getVersionableAPI().findAllVersions(ident); boolean islive = false; for (Versionable version : versions) { File f = (File) version; // sets filename for this new file f.setFileName(newFileName); HibernateUtil.saveOrUpdate(f); if (f.isLive()) islive = true; } LiveCache.removeAssetFromCache(file); WorkingCache.removeAssetFromCache(file); CacheLocator.getIdentifierCache().removeFromCacheByVersionable(file); ident.setURI(APILocator.getIdentifierAPI().find(folder).getPath() + newFileName); // HibernateUtil.saveOrUpdate(ident); APILocator.getIdentifierAPI().save(ident); if (islive) { LiveCache.removeAssetFromCache(file); LiveCache.addToLiveAssetToCache(file); } WorkingCache.removeAssetFromCache(file); WorkingCache.addToWorkingAssetToCache(file); CacheLocator.getIdentifierCache().removeFromCacheByVersionable(file); // RefreshMenus.deleteMenus(); RefreshMenus.deleteMenu(file); CacheLocator.getNavToolCache().removeNav(folder.getHostId(), folder.getInode()); return true; }
public void deleteFromCache(File file) throws DotDataException, DotStateException, DotSecurityException { fileCache.remove(file); WorkingCache.removeAssetFromCache(file); if (file.isLive()) { LiveCache.removeAssetFromCache(file); } CacheLocator.getIdentifierCache().removeFromCacheByVersionable(file); }
@SuppressWarnings("unchecked") public File saveFile( File newFile, java.io.File dataFile, Folder parentFolder, Identifier identifier) throws DotDataException { boolean localTransation = false; try { localTransation = DbConnectionFactory.getConnection().getAutoCommit(); if (localTransation) { HibernateUtil.startTransaction(); } // old working file File oldFile = null; // if new identifier if (identifier == null || !InodeUtils.isSet(identifier.getInode())) { identifier = APILocator.getIdentifierAPI().createNew(newFile, parentFolder); newFile.setIdentifier(identifier.getInode()); HibernateUtil.save(newFile); APILocator.getVersionableAPI().setWorking(newFile); saveFileData(newFile, null, dataFile); } else { APILocator.getVersionableAPI().removeLive(identifier.getId()); } if (UtilMethods.isSet(dataFile)) { HibernateUtil.save(newFile); saveFileData(newFile, null, dataFile); } if (oldFile != null && InodeUtils.isSet(oldFile.getInode())) { APILocator.getFileAPI().invalidateCache(oldFile); fileCache.remove(oldFile); WorkingCache.removeAssetFromCache(oldFile); } LiveCache.removeAssetFromCache(newFile); if (newFile.isLive()) { LiveCache.addToLiveAssetToCache(newFile); } WorkingCache.addToWorkingAssetToCache(newFile); if (localTransation) { HibernateUtil.commitTransaction(); } } catch (Exception e) { if (localTransation) { HibernateUtil.rollbackTransaction(); } throw new DotDataException(e.getMessage(), e); } return newFile; }
public File get(String inode) throws DotStateException, DotDataException, DotSecurityException { File file = fileCache.get(inode); if ((file == null) || !InodeUtils.isSet(file.getInode())) { file = (File) HibernateUtil.load(File.class, inode); fileCache.add(file); WorkingCache.removeAssetFromCache(file); WorkingCache.addToWorkingAssetToCache(file); LiveCache.removeAssetFromCache(file); if (file.isLive()) { LiveCache.addToLiveAssetToCache(file); } } return file; }