Skip to content

Commit

Permalink
Merge pull request #114 from xmtp/np/add-filename-to-decrypted-attach…
Browse files Browse the repository at this point in the history
…ment

fix: add file name to decrypted local attachment
  • Loading branch information
nplasterer authored Sep 14, 2023
2 parents e3b7cf2 + ab24d86 commit ba299bd
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ repositories {
dependencies {
implementation project(':expo-modules-core')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
implementation "org.xmtp:android:0.5.6"
implementation "org.xmtp:android:0.6.0"
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.facebook.react:react-native:0.71.3'
implementation "com.daveanthonythomas.moshipack:moshipack:1.0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ class XMTPModule : Module() {
DecryptedLocalAttachment(
fileUri = file.toURI().toString(),
mimeType = attachment.mimeType,
filename = attachment.filename
).toJson()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import com.google.gson.JsonParser
class DecryptedLocalAttachment(
val fileUri: String,
val mimeType: String,
val filename: String,
) {
companion object {
fun fromJsonObject(obj: JsonObject) = DecryptedLocalAttachment(
obj.get("fileUri").asString,
obj.get("mimeType").asString,
obj.get("filename")?.asString ?: "",
)

fun fromJson(json: String): DecryptedLocalAttachment {
Expand All @@ -26,6 +28,7 @@ class DecryptedLocalAttachment(
fun toJsonMap(): Map<String, Any> = mapOf(
"fileUri" to fileUri,
"mimeType" to mimeType,
"filename" to filename,
)

fun toJson(): String = GsonBuilder().create().toJson(toJsonMap())
Expand Down
3 changes: 3 additions & 0 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@ test("remote attachments should work", async () => {
if (attached.mimeType !== "text/plain") {
throw new Error("Expected mimeType to match");
}
if (attached.filename !== filename) {
throw new Error(`Expected ${attached.filename} to equal ${filename}`);
}
const text = await fs.readFile(new URL(attached.fileUri).pathname, "utf8");
if (text !== "hello world") {
throw new Error("Expected text to match");
Expand Down
7 changes: 5 additions & 2 deletions ios/Wrappers/DecodedMessageWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,23 @@ struct EncryptedLocalAttachment {
struct DecryptedLocalAttachment {
var fileUri: String
var mimeType: String
var filename: String

static func fromJson(_ json: String) throws -> DecryptedLocalAttachment {
let data = json.data(using: .utf8)!
let obj = (try? JSONSerialization.jsonObject(with: data) as? [String: Any]) ?? [:]
return DecryptedLocalAttachment(
fileUri: obj["fileUri"] as? String ?? "",
mimeType: obj["mimeType"] as? String ?? ""
mimeType: obj["mimeType"] as? String ?? "",
filename: obj["filename"] as? String ?? ""
)
}

func toJson() throws -> String {
let obj: [String: Any] = [
"fileUri": fileUri,
"mimeType": mimeType
"mimeType": mimeType,
"filename": filename
]
return try obj.toJson()
}
Expand Down
3 changes: 2 additions & 1 deletion ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ public class XMTPModule: Module {
try attachment.data.write(to: file)
return try DecryptedLocalAttachment(
fileUri: file.absoluteString,
mimeType: attachment.mimeType
mimeType: attachment.mimeType,
filename: attachment.filename
).toJson()
}

Expand Down
1 change: 1 addition & 0 deletions src/XMTP.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type StaticAttachmentContent = {
export type DecryptedLocalAttachment = {
fileUri: string;
mimeType?: string;
filename?: string;
};

export type RemoteAttachmentMetadata = {
Expand Down

0 comments on commit ba299bd

Please sign in to comment.