Refactor retrieving of attachments to use StreamDetails

This commit is contained in:
cedb 2022-11-01 16:13:55 -04:00
parent 589f5378df
commit c66e78b0ad
6 changed files with 26 additions and 30 deletions

View file

@ -6,14 +6,13 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.json.JsonAttachmentData;
import org.asamk.signal.manager.AttachmentPointer;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.io.InputStream;
import java.util.Base64;
public class AttachmentCommand implements JsonRpcLocalCommand {
@ -25,14 +24,8 @@ public class AttachmentCommand implements JsonRpcLocalCommand {
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.addArgument("--file-id")
subparser.addArgument("--id")
.help("The ID of the attachment file.");
subparser.addArgument("--file-name")
.nargs("?")
.help("The name of the file.");
subparser.addArgument("--content-type")
.nargs("?")
.help("The content type of the file.");
}
@Override
@ -42,14 +35,11 @@ public class AttachmentCommand implements JsonRpcLocalCommand {
final OutputWriter outputWriter
) throws CommandException {
final var id = ns.getString("file-id");
final var fileName = ns.getString("file-name");
final var contentType = ns.getString("content-type");
final var id = ns.getString("id");
final var file = m.getAttachmentFile(new AttachmentPointer(id, fileName, contentType));
try(InputStream attachment = m.retrieveAttachment(id)) {
try {
final var bytes = Files.readAllBytes(file.toPath());
final var bytes = attachment.readAllBytes();
final var base64 = Base64.getEncoder().encodeToString(bytes);
if (outputWriter instanceof PlainTextWriter writer) {
@ -59,7 +49,7 @@ public class AttachmentCommand implements JsonRpcLocalCommand {
writer.write(new JsonAttachmentData(base64));
}
} catch (IOException ex) {
throw new UnexpectedErrorException("An error occurred reading attachment file: " + file, ex);
throw new UnexpectedErrorException("An error occurred reading attachment: " + id, ex);
}
}
}

View file

@ -2,7 +2,6 @@ package org.asamk.signal.dbus;
import org.asamk.Signal;
import org.asamk.signal.DbusConfig;
import org.asamk.signal.manager.AttachmentPointer;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.Configuration;
@ -47,6 +46,7 @@ import org.freedesktop.dbus.types.Variant;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
@ -909,9 +909,8 @@ public class DbusManagerImpl implements Manager {
}
@Override
public File getAttachmentFile(final AttachmentPointer pointer) {
//TODO may need an implementation
return null;
public InputStream retrieveAttachment(final String id) throws IOException {
throw new UnsupportedOperationException();
}
@SuppressWarnings("unchecked")