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:
ced-b 2022-11-24 11:29:45 -05:00 committed by GitHub
parent 3e60303b90
commit 35def4445d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 26 deletions

View file

@ -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);

View file

@ -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
) {}

View file

@ -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);
}
}