@Test public void testTemporaryFileUpload() throws Exception { final Path file = new Path("/t", EnumSet.of(Path.Type.file)); final AtomicBoolean found = new AtomicBoolean(); final AtomicInteger moved = new AtomicInteger(); final Find find = new Find() { @Override public boolean find(final Path f) { if(f.equals(file)) { found.set(true); return true; } return false; } }; final AttributesFinder attributes = new AttributesFinder() { @Override public PathAttributes find(final Path file) { return new PathAttributes(); } }; final NullSession session = new NullSession(new Host(new TestProtocol())) { @Override @SuppressWarnings("unchecked") public <T> T _getFeature(final Class<T> type) { if(type.equals(Move.class)) { return (T) new Move() { @Override public Path move(final Path source, final Path renamed, final TransferStatus status, final Delete.Callback callback, final ConnectionCallback connectionCallback) { if(moved.incrementAndGet() == 1) { assertEquals(file, source); } else if(moved.get() == 2) { assertEquals(file, renamed); } else { fail(); } return renamed; } @Override public boolean isRecursive(final Path source, final Path target) { return true; } }; } if(type.equals(Write.class)) { return (T) new Write<Void>() { @Override public StatusOutputStream write(final Path file, final TransferStatus status, final ConnectionCallback callback) { fail(); return null; } @Override public Append append(final Path file, final Long length, final Cache<Path> cache) { fail(); return new Append(1L); } @Override public boolean temporary() { return true; } @Override public boolean random() { return false; } }; } return null; } }; final UploadFilterOptions options = new UploadFilterOptions().withTemporary(true); final RenameExistingFilter f = new RenameExistingFilter(new DisabledUploadSymlinkResolver(), session, options); f.withFinder(find).withAttributes(attributes); assertTrue(options.temporary); final TransferStatus status = f.prepare(file, new NullLocal("t"), new TransferStatus().exists(true), new DisabledProgressListener()); assertNotNull(status.getRename()); assertNotNull(status.getRename().remote); assertNotEquals(file, status.getDisplayname().local); assertNull(status.getRename().local); f.apply(file, new NullLocal("t"), status, new DisabledProgressListener()); status.setComplete(); f.complete(file, new NullLocal("t"), new TransferOptions(), status, new DisabledProgressListener()); assertTrue(found.get()); assertEquals(2, moved.get()); }
@Override public void apply(final Path file, final Local local, final TransferStatus status, final ProgressListener listener) throws BackgroundException { if(status.isExists()) { Path rename; do { final String proposal = MessageFormat.format(PreferencesFactory.get().getProperty("queue.upload.file.rename.format"), FilenameUtils.getBaseName(file.getName()), UserDateFormatterFactory.get().getMediumFormat(System.currentTimeMillis(), false).replace(Path.DELIMITER, '-').replace(':', '-'), StringUtils.isNotBlank(file.getExtension()) ? String.format(".%s", file.getExtension()) : StringUtils.EMPTY); rename = new Path(file.getParent(), proposal, file.getType()); } while(find.withCache(cache).find(rename)); if(log.isInfoEnabled()) { log.info(String.format("Rename existing file %s to %s", file, rename)); } move.move(file, rename, new TransferStatus().exists(false), new Delete.DisabledCallback(), new DisabledConnectionCallback()); if(log.isDebugEnabled()) { log.debug(String.format("Clear exist flag for file %s", file)); } status.setExists(false); } super.apply(file, local, status, listener); }
RenameExistingFilter extends AbstractUploadFilter { @Override public void apply(final Path file, final Local local, final TransferStatus status, final ProgressListener listener) throws BackgroundException { if(status.isExists()) { Path rename; do { final String proposal = MessageFormat.format(PreferencesFactory.get().getProperty("queue.upload.file.rename.format"), FilenameUtils.getBaseName(file.getName()), UserDateFormatterFactory.get().getMediumFormat(System.currentTimeMillis(), false).replace(Path.DELIMITER, '-').replace(':', '-'), StringUtils.isNotBlank(file.getExtension()) ? String.format(".%s", file.getExtension()) : StringUtils.EMPTY); rename = new Path(file.getParent(), proposal, file.getType()); } while(find.withCache(cache).find(rename)); if(log.isInfoEnabled()) { log.info(String.format("Rename existing file %s to %s", file, rename)); } move.move(file, rename, new TransferStatus().exists(false), new Delete.DisabledCallback(), new DisabledConnectionCallback()); if(log.isDebugEnabled()) { log.debug(String.format("Clear exist flag for file %s", file)); } status.setExists(false); } super.apply(file, local, status, listener); } }
RenameExistingFilter extends AbstractUploadFilter { @Override public void apply(final Path file, final Local local, final TransferStatus status, final ProgressListener listener) throws BackgroundException { if(status.isExists()) { Path rename; do { final String proposal = MessageFormat.format(PreferencesFactory.get().getProperty("queue.upload.file.rename.format"), FilenameUtils.getBaseName(file.getName()), UserDateFormatterFactory.get().getMediumFormat(System.currentTimeMillis(), false).replace(Path.DELIMITER, '-').replace(':', '-'), StringUtils.isNotBlank(file.getExtension()) ? String.format(".%s", file.getExtension()) : StringUtils.EMPTY); rename = new Path(file.getParent(), proposal, file.getType()); } while(find.withCache(cache).find(rename)); if(log.isInfoEnabled()) { log.info(String.format("Rename existing file %s to %s", file, rename)); } move.move(file, rename, new TransferStatus().exists(false), new Delete.DisabledCallback(), new DisabledConnectionCallback()); if(log.isDebugEnabled()) { log.debug(String.format("Clear exist flag for file %s", file)); } status.setExists(false); } super.apply(file, local, status, listener); } RenameExistingFilter(final SymlinkResolver<Local> symlinkResolver, final Session<?> session);  RenameExistingFilter(final SymlinkResolver<Local> symlinkResolver, final Session<?> session,
                                final UploadFilterOptions options); }
RenameExistingFilter extends AbstractUploadFilter { @Override public void apply(final Path file, final Local local, final TransferStatus status, final ProgressListener listener) throws BackgroundException { if(status.isExists()) { Path rename; do { final String proposal = MessageFormat.format(PreferencesFactory.get().getProperty("queue.upload.file.rename.format"), FilenameUtils.getBaseName(file.getName()), UserDateFormatterFactory.get().getMediumFormat(System.currentTimeMillis(), false).replace(Path.DELIMITER, '-').replace(':', '-'), StringUtils.isNotBlank(file.getExtension()) ? String.format(".%s", file.getExtension()) : StringUtils.EMPTY); rename = new Path(file.getParent(), proposal, file.getType()); } while(find.withCache(cache).find(rename)); if(log.isInfoEnabled()) { log.info(String.format("Rename existing file %s to %s", file, rename)); } move.move(file, rename, new TransferStatus().exists(false), new Delete.DisabledCallback(), new DisabledConnectionCallback()); if(log.isDebugEnabled()) { log.debug(String.format("Clear exist flag for file %s", file)); } status.setExists(false); } super.apply(file, local, status, listener); } RenameExistingFilter(final SymlinkResolver<Local> symlinkResolver, final Session<?> session);  RenameExistingFilter(final SymlinkResolver<Local> symlinkResolver, final Session<?> session,
                                final UploadFilterOptions options); @Override void apply(final Path file, final Local local, final TransferStatus status,
                      final ProgressListener listener); }
RenameExistingFilter extends AbstractUploadFilter { @Override public void apply(final Path file, final Local local, final TransferStatus status, final ProgressListener listener) throws BackgroundException { if(status.isExists()) { Path rename; do { final String proposal = MessageFormat.format(PreferencesFactory.get().getProperty("queue.upload.file.rename.format"), FilenameUtils.getBaseName(file.getName()), UserDateFormatterFactory.get().getMediumFormat(System.currentTimeMillis(), false).replace(Path.DELIMITER, '-').replace(':', '-'), StringUtils.isNotBlank(file.getExtension()) ? String.format(".%s", file.getExtension()) : StringUtils.EMPTY); rename = new Path(file.getParent(), proposal, file.getType()); } while(find.withCache(cache).find(rename)); if(log.isInfoEnabled()) { log.info(String.format("Rename existing file %s to %s", file, rename)); } move.move(file, rename, new TransferStatus().exists(false), new Delete.DisabledCallback(), new DisabledConnectionCallback()); if(log.isDebugEnabled()) { log.debug(String.format("Clear exist flag for file %s", file)); } status.setExists(false); } super.apply(file, local, status, listener); } RenameExistingFilter(final SymlinkResolver<Local> symlinkResolver, final Session<?> session);  RenameExistingFilter(final SymlinkResolver<Local> symlinkResolver, final Session<?> session,
                                final UploadFilterOptions options); @Override void apply(final Path file, final Local local, final TransferStatus status,
                      final ProgressListener listener);  }