Return the hashes of newly uploaded attachments to the uploader.

This commit is contained in:
Mike Hearn 2016-03-08 15:41:44 +01:00
parent 57ec407755
commit ffcc0507c4
3 changed files with 15 additions and 3 deletions

View File

@ -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()

View File

@ -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.

View File

@ -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<SecureHash>()
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) }
}
}