forked from Qortal-Forker/qortal
		
	Added POST /site/preview API
This can be used to preview a site before signing a transaction and announcing it to the network. The response will need reworking to return JSON (along with most of the other new APIs)
This commit is contained in:
		@@ -69,7 +69,7 @@ public class WebsiteResource {
 | 
				
			|||||||
                    )
 | 
					                    )
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    public String hostWebsite(String directoryPath) {
 | 
					    public String uploadWebsite(String directoryPath) {
 | 
				
			||||||
        Security.checkApiCallAllowed(request);
 | 
					        Security.checkApiCallAllowed(request);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // It's too dangerous to allow user-supplied filenames in weaker security contexts
 | 
					        // It's too dangerous to allow user-supplied filenames in weaker security contexts
 | 
				
			||||||
@@ -77,6 +77,56 @@ public class WebsiteResource {
 | 
				
			|||||||
            throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.NON_PRODUCTION);
 | 
					            throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.NON_PRODUCTION);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        String base58Digest = this.hostWebsite(directoryPath);
 | 
				
			||||||
 | 
					        if (base58Digest != null) {
 | 
				
			||||||
 | 
					            // TODO: build transaction
 | 
				
			||||||
 | 
					            return "true";
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return "false";
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @POST
 | 
				
			||||||
 | 
					    @Path("/preview")
 | 
				
			||||||
 | 
					    @Operation(
 | 
				
			||||||
 | 
					            summary = "Generate preview URL based on a user-supplied path to a static website",
 | 
				
			||||||
 | 
					            requestBody = @RequestBody(
 | 
				
			||||||
 | 
					                    required = true,
 | 
				
			||||||
 | 
					                    content = @Content(
 | 
				
			||||||
 | 
					                            mediaType = MediaType.TEXT_PLAIN,
 | 
				
			||||||
 | 
					                            schema = @Schema(
 | 
				
			||||||
 | 
					                                    type = "string", example = "/Users/user/Documents/MyStaticWebsite"
 | 
				
			||||||
 | 
					                            )
 | 
				
			||||||
 | 
					                    )
 | 
				
			||||||
 | 
					            ),
 | 
				
			||||||
 | 
					            responses = {
 | 
				
			||||||
 | 
					                    @ApiResponse(
 | 
				
			||||||
 | 
					                            description = "raw, unsigned, UPLOAD_DATA transaction encoded in Base58",
 | 
				
			||||||
 | 
					                            content = @Content(
 | 
				
			||||||
 | 
					                                    mediaType = MediaType.TEXT_PLAIN,
 | 
				
			||||||
 | 
					                                    schema = @Schema(
 | 
				
			||||||
 | 
					                                            type = "string"
 | 
				
			||||||
 | 
					                                    )
 | 
				
			||||||
 | 
					                            )
 | 
				
			||||||
 | 
					                    )
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    public String previewWebsite(String directoryPath) {
 | 
				
			||||||
 | 
					        Security.checkApiCallAllowed(request);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // It's too dangerous to allow user-supplied filenames in weaker security contexts
 | 
				
			||||||
 | 
					        if (Settings.getInstance().isApiRestricted()) {
 | 
				
			||||||
 | 
					            throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.NON_PRODUCTION);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        String base58Digest = this.hostWebsite(directoryPath);
 | 
				
			||||||
 | 
					        if (base58Digest != null) {
 | 
				
			||||||
 | 
					            return "http://localhost:12393/site/" + base58Digest;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return "Unable to generate preview URL";
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private String hostWebsite(String directoryPath) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Check if a file or directory has been supplied
 | 
					        // Check if a file or directory has been supplied
 | 
				
			||||||
        File file = new File(directoryPath);
 | 
					        File file = new File(directoryPath);
 | 
				
			||||||
        if (!file.isDirectory()) {
 | 
					        if (!file.isDirectory()) {
 | 
				
			||||||
@@ -113,10 +163,10 @@ public class WebsiteResource {
 | 
				
			|||||||
            int chunkCount = dataFile.split(DataFile.CHUNK_SIZE);
 | 
					            int chunkCount = dataFile.split(DataFile.CHUNK_SIZE);
 | 
				
			||||||
            if (chunkCount > 0) {
 | 
					            if (chunkCount > 0) {
 | 
				
			||||||
                LOGGER.info(String.format("Successfully split into %d chunk%s", chunkCount, (chunkCount == 1 ? "" : "s")));
 | 
					                LOGGER.info(String.format("Successfully split into %d chunk%s", chunkCount, (chunkCount == 1 ? "" : "s")));
 | 
				
			||||||
                return "true";
 | 
					                return dataFile.base58Digest();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return "false";
 | 
					            return null;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        finally {
 | 
					        finally {
 | 
				
			||||||
            // Clean up by deleting the zipped file
 | 
					            // Clean up by deleting the zipped file
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user