@Override public void move(final RemoteFile sourceFile, final RemoteFile targetFile) throws StorageException { final RemoteFile pathAwareSourceFile = createPathAwareRemoteFile(sourceFile); final RemoteFile pathAwareTargetFile = createPathAwareRemoteFile(targetFile); if (!createFolder(pathAwareTargetFile)) { throw new StorageException("Unable to create path for " + pathAwareTargetFile); } underlyingTransferManager.move(pathAwareSourceFile, pathAwareTargetFile); removeFolder(pathAwareSourceFile); }
@Override public <T extends RemoteFile> Map<String, T> list(final Class<T> remoteFileClass) throws StorageException { Map<String, T> filesInFolder = Maps.newHashMap(); String remoteFilePath = getRemoteFilePath(remoteFileClass); list(remoteFilePath, filesInFolder, remoteFileClass); return filesInFolder; }
private RemoteFile createPathAwareRemoteFile(RemoteFile remoteFile) throws StorageException { PathAwareRemoteFileAttributes pathAwareRemoteFileAttributes = new PathAwareRemoteFileAttributes(); remoteFile.setAttributes(pathAwareRemoteFileAttributes); if (isFolderizable(remoteFile.getClass())) { // If remote file is folderizable, i.e. an 'affected file', // get the sub-path for it String subPathId = getSubPathId(remoteFile); String subPath = getSubPath(subPathId); pathAwareRemoteFileAttributes.setPath(subPath); } return remoteFile; }
@Override public boolean delete(final RemoteFile remoteFile) throws StorageException { RemoteFile pathAwareRemoteFile = createPathAwareRemoteFile(remoteFile); boolean fileDeleted = underlyingTransferManager.delete(pathAwareRemoteFile); boolean folderDeleted = removeFolder(pathAwareRemoteFile); return fileDeleted && folderDeleted; }
@Override public void upload(final File localFile, final RemoteFile remoteFile) throws StorageException { final RemoteFile pathAwareRemoteFile = createPathAwareRemoteFile(remoteFile); if (!createFolder(pathAwareRemoteFile)) { throw new StorageException("Unable to create path for " + pathAwareRemoteFile); } underlyingTransferManager.upload(localFile, pathAwareRemoteFile); }
private boolean removeFolder(RemoteFile remoteFile) throws StorageException { PathAwareRemoteFileAttributes pathAwareRemoteFileAttributes = remoteFile.getAttributes(PathAwareRemoteFileAttributes.class); boolean notAPathAwareRemoteFile = pathAwareRemoteFileAttributes == null || !pathAwareRemoteFileAttributes.hasPath(); if (notAPathAwareRemoteFile) { return true; } else { String remoteFilePath = pathToString(Paths.get(underlyingTransferManager.getRemoteFilePath(remoteFile.getClass()), pathAwareRemoteFileAttributes.getPath())); logger.log(Level.INFO, "Remote file is path aware, cleaning empty folders at " + remoteFilePath); boolean success = removeFolder(remoteFilePath); return success; } }
@Override public void download(final RemoteFile remoteFile, final File localFile) throws StorageException { underlyingTransferManager.download(createPathAwareRemoteFile(remoteFile), localFile); }
private <T extends RemoteFile> void list(String remoteFilePath, Map<String, T> remoteFiles, Class<T> remoteFileClass) throws StorageException { logger.log(Level.INFO, "Listing folder for files matching " + remoteFileClass.getSimpleName() + ": " + remoteFilePath); Map<String, FileType> folderList = pathAwareFeatureExtension.listFolder(remoteFilePath); for (Map.Entry<String, FileType> folderListEntry : folderList.entrySet()) { String fileName = folderListEntry.getKey(); FileType fileType = folderListEntry.getValue(); if (fileType == FileType.FILE) { try { remoteFiles.put(fileName, RemoteFile.createRemoteFile(fileName, remoteFileClass)); logger.log(Level.INFO, "- File: " + fileName); } catch (StorageException e) { // We don't care and ignore non-matching files! } } else if (fileType == FileType.FOLDER) { logger.log(Level.INFO, "- Folder: " + fileName); String newRemoteFilePath = remoteFilePath + folderSeparator + fileName; list(newRemoteFilePath, remoteFiles, remoteFileClass); } } }
private boolean createFolder(RemoteFile remoteFile) throws StorageException { PathAwareRemoteFileAttributes pathAwareRemoteFileAttributes = remoteFile.getAttributes(PathAwareRemoteFileAttributes.class); boolean notAPathAwareRemoteFile = pathAwareRemoteFileAttributes == null || !pathAwareRemoteFileAttributes.hasPath(); if (notAPathAwareRemoteFile) { return true; } else { String remoteFilePath = pathToString(Paths.get(underlyingTransferManager.getRemoteFilePath(remoteFile.getClass()), pathAwareRemoteFileAttributes.getPath())); logger.log(Level.INFO, "Remote file is path aware, creating folder " + remoteFilePath); boolean success = pathAwareFeatureExtension.createPath(remoteFilePath); return success; } }
public PathAwareFeatureTransferManager(TransferManager originalTransferManager, TransferManager underlyingTransferManager, Config config, PathAware pathAwareAnnotation) { this.underlyingTransferManager = underlyingTransferManager; this.subfolderDepth = pathAwareAnnotation.subfolderDepth(); this.bytesPerFolder = pathAwareAnnotation.bytesPerFolder(); this.folderSeparator = pathAwareAnnotation.folderSeparator(); this.affectedFiles = ImmutableList.copyOf(pathAwareAnnotation.affected()); this.pathAwareFeatureExtension = getPathAwareFeatureExtension(originalTransferManager, pathAwareAnnotation); }