@Override public Object beginMultiple(Map<String, String> parameters) throws Throwable { Params params = Params.parse(parameters); ServerDetails serverDetails = params.serverDetails(); Connection conn = Ssh.get().getConnection(serverDetails); Session session = conn.connect(params.userDetails(), serverDetails.hostKey() != null); MultiTransferContext mctx = new MultiTransferContext(conn, session); _mctxs.add(mctx); return mctx; }
@Override public void consume( java.lang.Object multiCtx, java.lang.String path, java.util.Map<java.lang.String, java.lang.String> parameters, XmlDoc.Element userMeta, XmlDoc.Element meta, LongInputStream in, java.lang.String appMimeType, java.lang.String streamMimeType, long length) throws Throwable { Connection conn = multiCtx == null ? null : ((MultiTransferContext) multiCtx).connection; Session session = multiCtx == null ? null : ((MultiTransferContext) multiCtx).session; Params params = Params.parse(parameters); if (multiCtx == null) { conn = Ssh.get().getConnection(params.serverDetails()); session = conn.connect(params.userDetails(), true); } String assetId = meta != null ? meta.value("@id") : null; String ext = meta != null ? meta.value("content/type/@ext") : null; try { String baseDir = params.directory != null ? params.directory : session.getHome(); StringBuilder sb = new StringBuilder(baseDir); if (!baseDir.endsWith("/")) { sb.append("/"); } if (path != null) { path = path.replace("\\\\", "/").replace("\\", "/"); while (path.startsWith("/")) { path = path.substring(1); } sb.append(path); } Set<String> existingDirs = multiCtx == null ? Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()) : ((MultiTransferContext) multiCtx).existingDirectories; if (params.decompress && streamMimeType != null && ArchiveRegistry.isAnArchive(streamMimeType)) { // decompress archive if (assetId != null) { sb.append("/"); sb.append("asset_"); sb.append(assetId); } if (RemoteArchiveExtractor.canExtract(session, streamMimeType)) { sb.append("/"); sb.append(PathUtil.getRandomFileName(8)); sb.append(".tmp"); String remotePath = sb.toString(); transfer(session, in, length, remotePath, params.fileMode, existingDirs); RemoteArchiveExtractor.extract(session, remotePath); } else { extractAndTransfer( session, sb.toString(), ArchiveRegistry.createInput(in, new NamedMimeType(streamMimeType)), params.fileMode, existingDirs); } } else { // single file if (assetId != null) { sb.append("/"); sb.append("asset_"); sb.append(assetId); } if (ext != null) { sb.append("."); sb.append(ext); } transfer(session, in, length, sb.toString(), params.fileMode, existingDirs); } } finally { if (multiCtx == null) { if (session != null) { session.close(); } if (conn != null) { conn.disconnect(); } } } }