From 71c247fe5631998af625a7a4755df95a6c1b36ed Mon Sep 17 00:00:00 2001 From: CalDescent Date: Fri, 25 Jun 2021 19:28:01 +0100 Subject: [PATCH] Added POST /data/file/{hash}/build API, used to join multiple chunks together. --- .../org/qortal/api/resource/DataResource.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/main/java/org/qortal/api/resource/DataResource.java b/src/main/java/org/qortal/api/resource/DataResource.java index fce2f0dc..c7894989 100644 --- a/src/main/java/org/qortal/api/resource/DataResource.java +++ b/src/main/java/org/qortal/api/resource/DataResource.java @@ -225,4 +225,59 @@ public class DataResource { throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA); } } + + @POST + @Path("/file/{hash}/build") + @Operation( + summary = "Join multiple chunks into a single file, using supplied comma separated base58 encoded SHA256 digest strings", + requestBody = @RequestBody( + required = true, + content = @Content( + mediaType = MediaType.TEXT_PLAIN, + schema = @Schema( + type = "string", example = "FZdHKgF5CbN2tKihvop5Ts9vmWmA9ZyyPY6bC1zivjy4,FZdHKgF5CbN2tKihvop5Ts9vmWmA9ZyyPY6bC1zivjy4" + ) + ) + ), + responses = { + @ApiResponse( + description = "true if joined, false if not", + content = @Content( + mediaType = MediaType.TEXT_PLAIN, + schema = @Schema( + type = "string" + ) + ) + ) + } + ) + @ApiErrors({ApiError.REPOSITORY_ISSUE, ApiError.INVALID_DATA, ApiError.INVALID_CRITERIA, ApiError.FILE_NOT_FOUND, ApiError.NO_REPLY}) + public Response joinFiles(String files, @PathParam("combinedHash") String combinedHash) { + + if (combinedHash == null) { + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA); + } + + DataFile dataFile = DataFile.fromBase58Digest(combinedHash); + if (dataFile.exists()) { + LOGGER.info("We already have the combined file {}, but we'll join the chunks anyway.", combinedHash); + } + + String base58DigestList[] = files.split(","); + for (String base58Digest : base58DigestList) { + if (base58Digest != null) { + DataFileChunk chunk = DataFileChunk.fromBase58Digest(base58Digest); + dataFile.addChunk(chunk); + } + } + boolean success = dataFile.join(); + if (success) { + if (combinedHash.equals(dataFile.base58Digest())) { + LOGGER.info("Valid hash {} after joining {} files", dataFile.base58Digest(), dataFile.chunkCount()); + return Response.ok("true").build(); + } + } + + return Response.ok("false").build(); + } }