git » sdk » commit 3837fb9

Enable prepareAttachment for cpp as well as js

author Stephen Paul Weber
2024-10-12 03:11:30 UTC
committer Stephen Paul Weber
2024-10-12 03:11:30 UTC
parent 27f6c56741c30d7ff854e65fc75835aebcd815a0

Enable prepareAttachment for cpp as well as js

snikket/AttachmentSource.cpp.hx +19 -0
snikket/AttachmentSource.js.hx +8 -0
snikket/Client.hx +7 -6

diff --git a/snikket/AttachmentSource.cpp.hx b/snikket/AttachmentSource.cpp.hx
new file mode 100644
index 0000000..d0da87a
--- /dev/null
+++ b/snikket/AttachmentSource.cpp.hx
@@ -0,0 +1,19 @@
+package snikket;
+
+class AttachmentSource {
+	public final path: String;
+	public final type: String;
+	public final name: String;
+	public final size: Int;
+
+	public function new(path: String, mime: String) {
+		this.name = haxe.io.Path.withoutDirectory(path);
+		this.path = sys.FileSystem.fullPath(path);
+		this.size = sys.FileSystem.stat(this.path).size;
+		this.type = mime;
+	}
+
+	public inline function tinkSource() {
+		return tink.io.Source.ofInput(this.name, sys.io.File.read(path));
+	}
+}
diff --git a/snikket/AttachmentSource.js.hx b/snikket/AttachmentSource.js.hx
new file mode 100644
index 0000000..3842799
--- /dev/null
+++ b/snikket/AttachmentSource.js.hx
@@ -0,0 +1,8 @@
+package snikket;
+
+@:forward
+abstract AttachmentSource(js.html.File) {
+	public inline function tinkSource() {
+		return tink.io.Source.ofJsFile(this.name, this);
+	}
+}
diff --git a/snikket/Client.hx b/snikket/Client.hx
index 4102aa9..605cf9c 100644
--- a/snikket/Client.hx
+++ b/snikket/Client.hx
@@ -643,11 +643,13 @@ class Client extends EventEmitter {
 		return EventHandled;
 	}
 
-	#if js
-	public function prepareAttachment(source: js.html.File, callback: (Null<ChatAttachment>)->Void) { // TODO: abstract with filename, mime, and ability to convert to tink.io.Source
+	/**
+		Turn a file into a ChatAttachment for attaching to a ChatMessage
+	**/
+	public function prepareAttachment(source: AttachmentSource, callback: (Null<ChatAttachment>)->Void) {
 		persistence.findServicesWithFeature(accountId(), "urn:xmpp:http:upload:0", (services) -> {
 			final sha256 = new sha.SHA256();
-			tink.io.Source.ofJsFile(source.name, source).chunked().forEach((chunk) -> {
+			source.tinkSource().chunked().forEach((chunk) -> {
 				sha256.update(chunk);
 				return tink.streams.Stream.Handled.Resume;
 			}).handle((o) -> switch o {
@@ -660,7 +662,7 @@ class Client extends EventEmitter {
 		});
 	}
 
-	private function prepareAttachmentFor(source: js.html.File, services: Array<{ serviceId: String }>, hashes: Array<Hash>, callback: (Null<ChatAttachment>)->Void) {
+	private function prepareAttachmentFor(source: AttachmentSource, services: Array<{ serviceId: String }>, hashes: Array<Hash>, callback: (Null<ChatAttachment>)->Void) {
 		if (services.length < 1) {
 			callback(null);
 			return;
@@ -671,7 +673,7 @@ class Client extends EventEmitter {
 			if (slot == null) {
 				prepareAttachmentFor(source, services.slice(1), hashes, callback);
 			} else {
-				tink.http.Client.fetch(slot.put, { method: PUT, headers: slot.putHeaders, body: tink.io.Source.RealSourceTools.idealize(tink.io.Source.ofJsFile(source.name, source), (e) -> throw e) }).all()
+				tink.http.Client.fetch(slot.put, { method: PUT, headers: slot.putHeaders, body: tink.io.Source.RealSourceTools.idealize(source.tinkSource(), (e) -> throw e) }).all()
 					.handle((o) -> switch o {
 						case Success(res) if (res.header.statusCode == 201):
 							callback(new ChatAttachment(source.name, source.type, source.size, [slot.get], hashes));
@@ -682,7 +684,6 @@ class Client extends EventEmitter {
 		});
 		sendQuery(httpUploadSlot);
 	}
-	#end
 
 	/**
 		@returns array of open chats, sorted by last activity