diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/Connection.kt b/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/Connection.kt index 02571c7..c646f55 100644 --- a/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/Connection.kt +++ b/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/Connection.kt @@ -1,6 +1,8 @@ package org.hso.texturesyncclient.controller.net import com.google.gson.* +import javafx.scene.image.Image +import org.hso.texturesyncclient.model.Sha256 import org.hso.texturesyncclient.model.Texture import java.io.* import java.lang.RuntimeException @@ -164,6 +166,54 @@ class Connection(val address: InetAddress, val port: Int = 10796) : Closeable { } + @Throws(IOException::class, ConnectionException::class) + @Synchronized + fun getTextureFile(hash : Sha256) : ByteArray { + val io = getStreams() + + val obj = JsonObject() + obj.add("get_texture_file", { + val inner = JsonObject() + inner.addProperty("texture_hash", hash.toString()) + inner + }()) + + JsonPackage(obj).write(io.second) + + when (val pkg = Package.read(io.first)) { + is JsonPackage -> throw ConnectionUnexpecedPacketException() + is BinaryPackage -> return pkg.content + is ErrorPackage -> throw ConnectionErrorException(pkg) + else -> throw RuntimeException("Unreachable") + } + + } + + @Throws(IOException::class, ConnectionException::class) + @Synchronized + fun getTexturePreview(hash : Sha256) : Image { + val io = getStreams() + + val obj = JsonObject() + obj.add("get_texture_file", { + val inner = JsonObject() + inner.addProperty("texture_hash", hash.toString()) + inner.addProperty("desired_format", "jpeg") + inner + }()) + + JsonPackage(obj).write(io.second) + + when (val pkg = Package.read(io.first)) { + is JsonPackage -> throw ConnectionUnexpecedPacketException() + is BinaryPackage -> { + return Image(ByteArrayInputStream(pkg.content)) + } + is ErrorPackage -> throw ConnectionErrorException(pkg) + else -> throw RuntimeException("Unreachable") + } + } + @Throws(IOException::class) @Synchronized override fun close() {