3
0
mirror of https://github.com/Qortal/qortal.git synced 2025-02-12 18:25:49 +00:00

Fixed site preview functionality.

This commit is contained in:
CalDescent 2021-07-17 15:23:24 +01:00
parent f5b29bad33
commit dc83e32173

View File

@ -63,6 +63,11 @@ public class WebsiteResource {
private static final Logger LOGGER = LogManager.getLogger(WebsiteResource.class); private static final Logger LOGGER = LogManager.getLogger(WebsiteResource.class);
public enum ResourceIdType {
SIGNATURE,
FILE_HASH
};
@Context HttpServletRequest request; @Context HttpServletRequest request;
@Context HttpServletResponse response; @Context HttpServletResponse response;
@Context ServletContext context; @Context ServletContext context;
@ -199,7 +204,7 @@ public class WebsiteResource {
if (dataFile != null) { if (dataFile != null) {
String digest58 = dataFile.digest58(); String digest58 = dataFile.digest58();
if (digest58 != null) { if (digest58 != null) {
return "http://localhost:12393/site/" + digest58; return "http://localhost:12393/site/hash/" + digest58 + "?secret=" + Base58.encode(dataFile.getSecret());
} }
} }
return "Unable to generate preview URL"; return "Unable to generate preview URL";
@ -277,38 +282,51 @@ public class WebsiteResource {
} }
@GET @GET
@Path("{resource}") @Path("{signature}")
public HttpServletResponse getResourceIndex(@PathParam("resource") String resourceId) { public HttpServletResponse getIndexBySignature(@PathParam("signature") String signature) {
return this.get(resourceId, "/", true); return this.get(signature, ResourceIdType.SIGNATURE, "/", null,true);
} }
@GET @GET
@Path("{resource}/{path:.*}") @Path("{signature}/{path:.*}")
public HttpServletResponse getResourcePath(@PathParam("resource") String resourceId, @PathParam("path") String inPath) { public HttpServletResponse getPathBySignature(@PathParam("signature") String signature, @PathParam("path") String inPath) {
return this.get(resourceId, inPath, true); return this.get(signature, ResourceIdType.SIGNATURE, inPath,null,true);
}
@GET
@Path("/hash/{hash}")
public HttpServletResponse getIndexByHash(@PathParam("hash") String hash58, @QueryParam("secret") String secret58) {
return this.get(hash58, ResourceIdType.FILE_HASH, "/", secret58,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,true);
} }
@GET @GET
@Path("/domainmap") @Path("/domainmap")
public HttpServletResponse getDomainMapIndex() { public HttpServletResponse getIndexByDomainMap() {
Map<String, String> domainMap = Settings.getInstance().getSimpleDomainMap(); return this.getDomainMap("/");
if (domainMap != null && domainMap.containsKey(request.getServerName())) {
return this.get(domainMap.get(request.getServerName()), "/", false);
}
return this.get404Response();
} }
@GET @GET
@Path("/domainmap/{path:.*}") @Path("/domainmap/{path:.*}")
public HttpServletResponse getDomainMapPath(@PathParam("path") String inPath) { public HttpServletResponse getPathByDomainMap(@PathParam("path") String inPath) {
return this.getDomainMap(inPath);
}
private HttpServletResponse getDomainMap(String inPath) {
Map<String, String> domainMap = Settings.getInstance().getSimpleDomainMap(); Map<String, String> domainMap = Settings.getInstance().getSimpleDomainMap();
if (domainMap != null && domainMap.containsKey(request.getServerName())) { if (domainMap != null && domainMap.containsKey(request.getServerName())) {
return this.get(domainMap.get(request.getServerName()), inPath, false); return this.get(domainMap.get(request.getServerName()), ResourceIdType.SIGNATURE, inPath, null, false);
} }
return this.get404Response(); return this.get404Response();
} }
private HttpServletResponse get(String resourceId, String inPath, boolean usePrefix) { private HttpServletResponse get(String resourceId, ResourceIdType resourceIdType, String inPath, String secret58, boolean usePrefix) {
if (!inPath.startsWith(File.separator)) { if (!inPath.startsWith(File.separator)) {
inPath = File.separator + inPath; inPath = File.separator + inPath;
} }
@ -322,21 +340,25 @@ public class WebsiteResource {
// Load the full transaction data so we can access the file hashes // Load the full transaction data so we can access the file hashes
try (final Repository repository = RepositoryManager.getRepository()) { try (final Repository repository = RepositoryManager.getRepository()) {
DataFile dataFile = null;
byte[] digest = null;
byte[] secret = null;
if (resourceIdType == ResourceIdType.SIGNATURE) {
ArbitraryTransactionData transactionData = (ArbitraryTransactionData) repository.getTransactionRepository().fromSignature(Base58.decode(resourceId)); ArbitraryTransactionData transactionData = (ArbitraryTransactionData) repository.getTransactionRepository().fromSignature(Base58.decode(resourceId));
if (!(transactionData instanceof ArbitraryTransactionData)) { if (!(transactionData instanceof ArbitraryTransactionData)) {
return this.get404Response(); return this.get404Response();
} }
// Load hashes // Load hashes
byte[] digest = transactionData.getData(); digest = transactionData.getData();
byte[] chunkHashes = transactionData.getChunkHashes(); byte[] chunkHashes = transactionData.getChunkHashes();
// Load secret // Load secret
byte[] secret = transactionData.getSecret(); secret = transactionData.getSecret();
// Load data file(s) // Load data file(s)
DataFile dataFile = DataFile.fromHash(digest); dataFile = DataFile.fromHash(digest);
if (!dataFile.exists()) { if (!dataFile.exists()) {
if (!dataFile.allChunksExist(chunkHashes)) { if (!dataFile.allChunksExist(chunkHashes)) {
// TODO: fetch them? // TODO: fetch them?
@ -347,6 +369,14 @@ public class WebsiteResource {
dataFile.join(); dataFile.join();
} }
}
else if (resourceIdType == ResourceIdType.FILE_HASH) {
dataFile = DataFile.fromHash58(resourceId);
digest = Base58.decode(resourceId);
secret = secret58 != null ? Base58.decode(secret58) : null;
}
// If the complete file still doesn't exist then something went wrong // If the complete file still doesn't exist then something went wrong
if (!dataFile.exists()) { if (!dataFile.exists()) {
return this.get404Response(); return this.get404Response();