mirror of
https://github.com/AsamK/signal-cli
synced 2025-09-02 04:20:38 +00:00
Refactor retrieving of attachments to use StreamDetails
This commit is contained in:
parent
589f5378df
commit
c66e78b0ad
6 changed files with 26 additions and 30 deletions
|
@ -2,8 +2,10 @@ package org.asamk.signal.manager;
|
|||
|
||||
import org.asamk.signal.manager.util.IOUtils;
|
||||
import org.asamk.signal.manager.util.MimeUtils;
|
||||
import org.asamk.signal.manager.util.Utils;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
|
||||
import org.whispersystems.signalservice.api.util.StreamDetails;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
|
@ -39,10 +41,12 @@ public class AttachmentStore {
|
|||
Optional.ofNullable(pointer.getContentType()));
|
||||
}
|
||||
|
||||
public File getAttachmentFile(AttachmentPointer pointer) {
|
||||
return getAttachmentFile(SignalServiceAttachmentRemoteId.from(pointer.id()),
|
||||
Optional.ofNullable(pointer.fileName()),
|
||||
Optional.ofNullable(pointer.contentType()));
|
||||
public StreamDetails retrieveAttachment(final String id) throws IOException {
|
||||
final var attachmentFile = new File(attachmentsPath, id);
|
||||
if (!attachmentFile.exists()) {
|
||||
return null;
|
||||
}
|
||||
return Utils.createStreamDetailsFromFile(attachmentFile);
|
||||
}
|
||||
|
||||
private void storeAttachment(final File attachmentFile, final AttachmentStorer storer) throws IOException {
|
||||
|
|
|
@ -38,6 +38,7 @@ import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
|
|||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
import java.util.Collection;
|
||||
|
@ -272,7 +273,7 @@ public interface Manager extends Closeable {
|
|||
|
||||
void addClosedListener(Runnable listener);
|
||||
|
||||
File getAttachmentFile(AttachmentPointer pointer);
|
||||
InputStream retrieveAttachment(final String id) throws IOException;
|
||||
|
||||
@Override
|
||||
void close() throws IOException;
|
||||
|
|
|
@ -82,6 +82,7 @@ import org.whispersystems.signalservice.internal.util.Util;
|
|||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
|
@ -1147,8 +1148,9 @@ class ManagerImpl implements Manager {
|
|||
}
|
||||
}
|
||||
|
||||
public File getAttachmentFile(AttachmentPointer pointer) {
|
||||
return context.getAttachmentHelper().getAttachmentFile(pointer);
|
||||
@Override
|
||||
public InputStream retrieveAttachment(final String id) throws IOException {
|
||||
return context.getAttachmentHelper().retrieveAttachment(id).getStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package org.asamk.signal.manager.helper;
|
||||
|
||||
import org.asamk.signal.manager.AttachmentPointer;
|
||||
import org.asamk.signal.manager.AttachmentStore;
|
||||
import org.asamk.signal.manager.SignalDependencies;
|
||||
import org.asamk.signal.manager.api.AttachmentInvalidException;
|
||||
|
@ -14,6 +13,7 @@ import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
|||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
|
||||
import org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException;
|
||||
import org.whispersystems.signalservice.api.util.StreamDetails;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
@ -39,8 +39,8 @@ public class AttachmentHelper {
|
|||
return attachmentStore.getAttachmentFile(pointer);
|
||||
}
|
||||
|
||||
public File getAttachmentFile(AttachmentPointer pointer) {
|
||||
return attachmentStore.getAttachmentFile(pointer);
|
||||
public StreamDetails retrieveAttachment(final String id) throws IOException {
|
||||
return attachmentStore.retrieveAttachment(id);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue