From ffcc0507c4c35020eedc646973526471b9fc27d4 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Tue, 8 Mar 2016 15:41:44 +0100 Subject: [PATCH] Return the hashes of newly uploaded attachments to the uploader. --- src/main/kotlin/core/node/Node.kt | 2 +- .../kotlin/core/node/services/NodeAttachmentStorage.kt | 8 ++++++-- .../kotlin/core/node/servlets/AttachmentUploadServlet.kt | 8 ++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/core/node/Node.kt b/src/main/kotlin/core/node/Node.kt index f43f0ee993..c99898779f 100644 --- a/src/main/kotlin/core/node/Node.kt +++ b/src/main/kotlin/core/node/Node.kt @@ -60,7 +60,7 @@ class Node(dir: Path, val p2pAddr: HostAndPort, configuration: NodeConfiguration val server = Server(port) val handler = ServletContextHandler() handler.setAttribute("storage", storage) - handler.addServlet(AttachmentUploadServlet::class.java, "/attachments") + handler.addServlet(AttachmentUploadServlet::class.java, "/attachments/upload") handler.addServlet(AttachmentDownloadServlet::class.java, "/attachments/*") server.handler = handler server.start() diff --git a/src/main/kotlin/core/node/services/NodeAttachmentStorage.kt b/src/main/kotlin/core/node/services/NodeAttachmentStorage.kt index 7b90c7a817..ccb4ad5f8e 100644 --- a/src/main/kotlin/core/node/services/NodeAttachmentStorage.kt +++ b/src/main/kotlin/core/node/services/NodeAttachmentStorage.kt @@ -13,7 +13,6 @@ import com.google.common.hash.Hashing import com.google.common.hash.HashingInputStream import com.google.common.io.CountingInputStream import core.Attachment -import core.node.services.AttachmentStorage import core.crypto.SecureHash import core.extractZipFile import core.utilities.loggerFor @@ -106,16 +105,21 @@ class NodeAttachmentStorage(val storePath: Path) : AttachmentStorage { try { // Move into place atomically or fail if that isn't possible. We don't want a half moved attachment to // be exposed to parallel threads. This gives us thread safety. + if (!Files.exists(finalPath)) + log.info("Stored new attachment $id") + else + log.info("Replacing attachment $id - only bother doing this if you're trying to repair file corruption") Files.move(tmp, finalPath, StandardCopyOption.ATOMIC_MOVE) } finally { Files.deleteIfExists(tmp) } - log.info("Stored new attachment $id") if (automaticallyExtractAttachments) { val extractTo = storePath.resolve("${id}.jar") try { Files.createDirectory(extractTo) extractZipFile(finalPath, extractTo) + } catch(e: java.nio.file.FileAlreadyExistsException) { + log.trace("Did not extract attachment jar to directory because it already exists") } catch(e: Exception) { log.error("Failed to extract attachment jar $id, ", e) // TODO: Delete the extractTo directory here. diff --git a/src/main/kotlin/core/node/servlets/AttachmentUploadServlet.kt b/src/main/kotlin/core/node/servlets/AttachmentUploadServlet.kt index cee0f39695..1b3c7341d2 100644 --- a/src/main/kotlin/core/node/servlets/AttachmentUploadServlet.kt +++ b/src/main/kotlin/core/node/servlets/AttachmentUploadServlet.kt @@ -8,9 +8,11 @@ package core.node.servlets +import core.crypto.SecureHash import core.node.services.StorageService import core.utilities.loggerFor import org.apache.commons.fileupload.servlet.ServletFileUpload +import java.util.* import javax.servlet.http.HttpServlet import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse @@ -30,6 +32,7 @@ class AttachmentUploadServlet : HttpServlet() { val upload = ServletFileUpload() val iterator = upload.getItemIterator(req) + val ids = ArrayList() while (iterator.hasNext()) { val item = iterator.next() if (!item.name.endsWith(".jar")) { @@ -45,7 +48,12 @@ class AttachmentUploadServlet : HttpServlet() { item.openStream().use { val id = storage.attachments.importAttachment(it) log.info("${item.name} successfully inserted into the attachment store with id $id") + ids += id } } + + // Send back the hashes as a convenience for the user. + val writer = resp.writer + ids.forEach { writer.println(it) } } }