From a3e2bc8d23adf04c6851ac916619b0943e3ed9ee Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Tue, 30 Aug 2022 16:44:45 +0100 Subject: [PATCH] fix browsing failures when requests return no files or folders --- .../muwire/core/search/BrowseManager.groovy | 31 +++++--- .../muwire/core/search/BrowseSession.groovy | 71 ++++++++++--------- .../com/muwire/gui/ResultTreeModel.groovy | 3 +- 3 files changed, 58 insertions(+), 47 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/search/BrowseManager.groovy b/core/src/main/groovy/com/muwire/core/search/BrowseManager.groovy index 535e91c6..7d6ffed2 100644 --- a/core/src/main/groovy/com/muwire/core/search/BrowseManager.groovy +++ b/core/src/main/groovy/com/muwire/core/search/BrowseManager.groovy @@ -123,10 +123,12 @@ class BrowseManager { JsonOutput jsonOutput = new JsonOutput() def baos = new ByteArrayOutputStream() def dos = new DataOutputStream(new GZIPOutputStream(baos)) - writeFiles(topLevelItems.files.values(), dos, jsonOutput) - writeDirs(topLevelItems.dirs, dos, jsonOutput) - dos.close() - os.write(baos.toByteArray()) + if (count > 0) { + writeFiles(topLevelItems.files.values(), dos, jsonOutput) + writeDirs(topLevelItems.dirs, dos, jsonOutput) + dos.close() + os.write(baos.toByteArray()) + } os.flush() InputStream is = endpoint.getInputStream() @@ -168,19 +170,21 @@ class BrowseManager { def cb = new PathCallback() tempTree.traverse(requestedPath, cb) filesToWrite = cb.files - dirsToWrite = Collections.emptySet() + dirsToWrite = cb.dirs } filesToWrite.each {it.hit(browser, System.currentTimeMillis(), "Browse Host")} os.write("Files:${filesToWrite.size()}\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("Dirs:${dirsToWrite.size()}\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("\r\n".getBytes(StandardCharsets.US_ASCII)) - baos = new ByteArrayOutputStream() - dos = new DataOutputStream(new GZIPOutputStream(baos)) - writeFiles(filesToWrite, dos, jsonOutput) - writeDirs(dirsToWrite, dos, jsonOutput) - dos.close() - os.write(baos.toByteArray()) + if (filesToWrite.size() + dirsToWrite.size() > 0) { + baos = new ByteArrayOutputStream() + dos = new DataOutputStream(new GZIPOutputStream(baos)) + writeFiles(filesToWrite, dos, jsonOutput) + writeDirs(dirsToWrite, dos, jsonOutput) + dos.close() + os.write(baos.toByteArray()) + } os.flush() } } @@ -239,9 +243,14 @@ class BrowseManager { private static class PathCallback implements PathTreeCallback { final Set files = new HashSet<>() + final Set dirs = new HashSet<>() @Override void onDirectoryEnter(Path path, BrowsedFolder value) { + if (!value.sent) { + value.sent = true + dirs.add(value.path) + } } @Override diff --git a/core/src/main/groovy/com/muwire/core/search/BrowseSession.groovy b/core/src/main/groovy/com/muwire/core/search/BrowseSession.groovy index 4f476694..8b5d9bc2 100644 --- a/core/src/main/groovy/com/muwire/core/search/BrowseSession.groovy +++ b/core/src/main/groovy/com/muwire/core/search/BrowseSession.groovy @@ -136,48 +136,49 @@ class BrowseSession implements Runnable { if (!headers.containsKey("Dirs")) throw new Exception("Dirs header missing") int dirs = Integer.parseInt(headers['Dirs']) - eventBus.publish(new BrowseStatusEvent(host: event.host, status: BrowseStatus.FETCHING, - totalResults: results, currentItems: (files + dirs), uuid: uuid)) - log.info("starting to fetch ${files} files and ${dirs} dirs with uuid $uuid") + if (files + dirs > 0) { + eventBus.publish(new BrowseStatusEvent(host: event.host, status: BrowseStatus.FETCHING, + totalResults: results, currentItems: (files + dirs), uuid: uuid)) + log.info("starting to fetch ${files} files and ${dirs} dirs with uuid $uuid") - JsonSlurper slurper = new JsonSlurper() - DataInputStream dis = new DataInputStream(new GZIPInputStream(is)) - UIResultEvent[] batch = new UIResultEvent[Math.min(BATCH_SIZE, files)] - int j = 0 - for (int i = 0; i < files; i++) { - if (closed) - return - log.fine("parsing result $i at batch position $j") + JsonSlurper slurper = new JsonSlurper() + DataInputStream dis = new DataInputStream(new GZIPInputStream(is)) + UIResultEvent[] batch = new UIResultEvent[Math.min(BATCH_SIZE, files)] + int j = 0 + for (int i = 0; i < files; i++) { + if (closed) + return + log.fine("parsing result $i at batch position $j") - def json = readJson(slurper, dis) - UIResultEvent result = ResultsParser.parse(event.host, uuid, json) - result.chat = chat - result.profileHeader = profileHeader - batch[j++] = result + def json = readJson(slurper, dis) + UIResultEvent result = ResultsParser.parse(event.host, uuid, json) + result.chat = chat + result.profileHeader = profileHeader + batch[j++] = result - // publish piecemally - if (j == batch.length) { - eventBus.publish(new UIResultBatchEvent(results: batch, uuid: uuid)) - j = 0 - batch = new UIResultEvent[Math.min(files - i - 1, BATCH_SIZE)] - log.fine("publishing batch, next batch size ${batch.length}") + // publish piecemally + if (j == batch.length) { + eventBus.publish(new UIResultBatchEvent(results: batch, uuid: uuid)) + j = 0 + batch = new UIResultEvent[Math.min(files - i - 1, BATCH_SIZE)] + log.fine("publishing batch, next batch size ${batch.length}") + } } - } - for (int i = 0; i < dirs; i++) { - if (closed) - return + for (int i = 0; i < dirs; i++) { + if (closed) + return - def json = readJson(slurper, dis) - if (!json.directory || json.path == null) - throw new Exception("Invalid dir json") - List path = json.path.collect { DataUtil.readi18nString(Base64.decode(it)) } - def event = new UIBrowseDirEvent(uuid: uuid, - path: path.toArray(new String[0])) - eventBus.publish(event) + def json = readJson(slurper, dis) + if (!json.directory || json.path == null) + throw new Exception("Invalid dir json") + List path = json.path.collect { DataUtil.readi18nString(Base64.decode(it)) } + def event = new UIBrowseDirEvent(uuid: uuid, + path: path.toArray(new String[0])) + eventBus.publish(event) + } + eventBus.publish(new BrowseStatusEvent(host: event.host, status: BrowseStatus.FINISHED, uuid: uuid)) } - eventBus.publish(new BrowseStatusEvent(host: event.host, status : BrowseStatus.FINISHED, uuid : uuid)) - while(true) { Request nextPath = fetchQueue.poll(PING_INTERVAL, TimeUnit.MILLISECONDS) if (nextPath == null) { diff --git a/gui/src/main/groovy/com/muwire/gui/ResultTreeModel.groovy b/gui/src/main/groovy/com/muwire/gui/ResultTreeModel.groovy index 4f46c414..d48a4a41 100644 --- a/gui/src/main/groovy/com/muwire/gui/ResultTreeModel.groovy +++ b/gui/src/main/groovy/com/muwire/gui/ResultTreeModel.groovy @@ -37,7 +37,8 @@ class ResultTreeModel extends DefaultTreeModel { node = elementNode } - node.addDescendant(new PlaceholderNode()) + if (node.getChildCount() == 0) + node.addDescendant(new PlaceholderNode()) } void addToTree(UIResultEvent event) {