Skip to content

Commit

Permalink
Enable prepareAttachment for cpp as well as js
Browse files Browse the repository at this point in the history
  • Loading branch information
singpolyma committed Oct 12, 2024
1 parent 27f6c56 commit 3837fb9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
19 changes: 19 additions & 0 deletions snikket/AttachmentSource.cpp.hx
Original file line number Diff line number Diff line change
@@ -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));
}
}
8 changes: 8 additions & 0 deletions snikket/AttachmentSource.js.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package snikket;

@:forward
abstract AttachmentSource(js.html.File) {
public inline function tinkSource() {
return tink.io.Source.ofJsFile(this.name, this);
}
}
13 changes: 7 additions & 6 deletions snikket/Client.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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));
Expand All @@ -682,7 +684,6 @@ class Client extends EventEmitter {
});
sendQuery(httpUploadSlot);
}
#end

/**
@returns array of open chats, sorted by last activity
Expand Down

0 comments on commit 3837fb9

Please sign in to comment.