mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Fix handling of attachments in JSON RPC (#1109)
* Fix handling of attachments in JSON RPC It turns out that using a custom serializer on an input stream did not work well. For one the stream seems to be getting closed before the JSON gets written. But also the method for writing it was throwing an UnsupportedOperationException further down in Jackson. The above simplifies the matter by simply outputting the Base64 string first and then setting it on the model. * Add missing files to attachment fix Co-authored-by: cedb <cedb@keylimebox.org>
This commit is contained in:
parent
3e60303b90
commit
35def4445d
3 changed files with 4 additions and 26 deletions
|
@ -40,12 +40,12 @@ public class GetAttachmentCommand implements JsonRpcLocalCommand {
|
|||
final var id = ns.getString("id");
|
||||
|
||||
try (InputStream attachment = m.retrieveAttachment(id)) {
|
||||
final var bytes = attachment.readAllBytes();
|
||||
final var base64 = Base64.getEncoder().encodeToString(bytes);
|
||||
if (outputWriter instanceof PlainTextWriter writer) {
|
||||
final var bytes = attachment.readAllBytes();
|
||||
final var base64 = Base64.getEncoder().encodeToString(bytes);
|
||||
writer.println(base64);
|
||||
} else if (outputWriter instanceof JsonWriter writer) {
|
||||
writer.write(new JsonAttachmentData(attachment));
|
||||
writer.write(new JsonAttachmentData(base64));
|
||||
}
|
||||
} catch (FileNotFoundException ex) {
|
||||
throw new UserErrorException("Could not find attachment with ID: " + id, ex);
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
package org.asamk.signal.json;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public record JsonAttachmentData(
|
||||
@JsonSerialize(using = JsonStreamSerializer.class) InputStream data
|
||||
String data
|
||||
) {}
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
package org.asamk.signal.json;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class JsonStreamSerializer extends JsonSerializer<InputStream> {
|
||||
|
||||
@Override
|
||||
public void serialize(
|
||||
final InputStream value, final JsonGenerator jsonGenerator, final SerializerProvider serializers
|
||||
) throws IOException {
|
||||
jsonGenerator.writeBinary(value, -1);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue