From 7e30bf4197d93faba1188d983ce170d8bce77201 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Sat, 13 Nov 2021 17:40:09 +0000 Subject: [PATCH] Fixed website preview functionality which isn't compatible with asynchronous building. The simplest solution was to build synchronously when previewing. --- .../qortal/api/resource/WebsiteResource.java | 38 +++++++++++-------- .../qortal/arbitrary/ArbitraryDataReader.java | 7 +++- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/qortal/api/resource/WebsiteResource.java b/src/main/java/org/qortal/api/resource/WebsiteResource.java index af294f7c..e6131de9 100644 --- a/src/main/java/org/qortal/api/resource/WebsiteResource.java +++ b/src/main/java/org/qortal/api/resource/WebsiteResource.java @@ -117,38 +117,38 @@ public class WebsiteResource { @GET @Path("/signature/{signature}") public HttpServletResponse getIndexBySignature(@PathParam("signature") String signature) { - return this.get(signature, ResourceIdType.SIGNATURE, "/", null, "/site/signature", true); + return this.get(signature, ResourceIdType.SIGNATURE, "/", null, "/site/signature", true, true); } @GET @Path("/signature/{signature}/{path:.*}") public HttpServletResponse getPathBySignature(@PathParam("signature") String signature, @PathParam("path") String inPath) { - return this.get(signature, ResourceIdType.SIGNATURE, inPath,null, "/site/signature", true); + return this.get(signature, ResourceIdType.SIGNATURE, inPath,null, "/site/signature", true, true); } @GET @Path("/hash/{hash}") public HttpServletResponse getIndexByHash(@PathParam("hash") String hash58, @QueryParam("secret") String secret58) { - return this.get(hash58, ResourceIdType.FILE_HASH, "/", secret58, "/site/hash", true); + return this.get(hash58, ResourceIdType.FILE_HASH, "/", secret58, "/site/hash", true, false); + } + + @GET + @Path("/hash/{hash}/{path:.*}") + public HttpServletResponse getPathByHash(@PathParam("hash") String hash58, @PathParam("path") String inPath, + @QueryParam("secret") String secret58) { + return this.get(hash58, ResourceIdType.FILE_HASH, inPath, secret58, "/site/hash", true, false); } @GET @Path("{name}/{path:.*}") public HttpServletResponse getPathByName(@PathParam("name") String name, @PathParam("path") String inPath) { - return this.get(name, ResourceIdType.NAME, inPath, null, "/site", true); + return this.get(name, ResourceIdType.NAME, inPath, null, "/site", true, true); } @GET @Path("{name}") public HttpServletResponse getIndexByName(@PathParam("name") String name) { - return this.get(name, ResourceIdType.NAME, "/", null, "/site", true); - } - - @GET - @Path("/hash/{hash}/{path:.*}") - public HttpServletResponse getPathByHash(@PathParam("hash") String hash58, @PathParam("path") String inPath, - @QueryParam("secret") String secret58) { - return this.get(hash58, ResourceIdType.FILE_HASH, inPath, secret58, "/site/hash", true); + return this.get(name, ResourceIdType.NAME, "/", null, "/site", true, true); } @GET @@ -166,13 +166,13 @@ public class WebsiteResource { private HttpServletResponse getDomainMap(String inPath) { Map domainMap = Settings.getInstance().getSimpleDomainMap(); if (domainMap != null && domainMap.containsKey(request.getServerName())) { - return this.get(domainMap.get(request.getServerName()), ResourceIdType.NAME, inPath, null, "", false); + return this.get(domainMap.get(request.getServerName()), ResourceIdType.NAME, inPath, null, "", false, true); } return this.getResponse(404, "Error 404: File Not Found"); } private HttpServletResponse get(String resourceId, ResourceIdType resourceIdType, String inPath, String secret58, - String prefix, boolean usePrefix) { + String prefix, boolean usePrefix, boolean async) { if (!inPath.startsWith(File.separator)) { inPath = File.separator + inPath; } @@ -182,8 +182,14 @@ public class WebsiteResource { arbitraryDataReader.setSecret58(secret58); // Optional, used for loading encrypted file hashes only try { if (!arbitraryDataReader.isCachedDataAvailable()) { - arbitraryDataReader.loadAsynchronously(); - return this.getLoadingResponse(); + // If async is requested, show a loading screen whilst build is in progress + if (async) { + arbitraryDataReader.loadAsynchronously(); + return this.getLoadingResponse(); + } + + // Otherwise, hang the request until the build completes + arbitraryDataReader.loadSynchronously(false); } } catch (Exception e) { diff --git a/src/main/java/org/qortal/arbitrary/ArbitraryDataReader.java b/src/main/java/org/qortal/arbitrary/ArbitraryDataReader.java index db4ca108..90309f20 100644 --- a/src/main/java/org/qortal/arbitrary/ArbitraryDataReader.java +++ b/src/main/java/org/qortal/arbitrary/ArbitraryDataReader.java @@ -384,11 +384,14 @@ public class ArbitraryDataReader { } try { + // Default to ZIP compression - this is needed for previews + Compression compression = transactionData != null ? transactionData.getCompression() : Compression.ZIP; + // Handle each type of compression - if (transactionData.getCompression() == Compression.ZIP) { + if (compression == Compression.ZIP) { ZipUtils.unzip(this.filePath.toString(), this.uncompressedPath.getParent().toString()); } - else if (transactionData.getCompression() == Compression.NONE) { + else if (compression == Compression.NONE) { Files.createDirectories(this.uncompressedPath); Path finalPath = Paths.get(this.uncompressedPath.toString(), "data"); this.filePath.toFile().renameTo(finalPath.toFile());