mirror of
https://github.com/AsamK/signal-cli
synced 2025-09-02 04:20:38 +00:00
Add command to get an attachment
This commit is contained in:
parent
49aaff2bbe
commit
589f5378df
9 changed files with 97 additions and 0 deletions
|
@ -0,0 +1,3 @@
|
||||||
|
package org.asamk.signal.manager;
|
||||||
|
|
||||||
|
public record AttachmentPointer(String id, String fileName, String contentType) {}
|
|
@ -39,6 +39,12 @@ public class AttachmentStore {
|
||||||
Optional.ofNullable(pointer.getContentType()));
|
Optional.ofNullable(pointer.getContentType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public File getAttachmentFile(AttachmentPointer pointer) {
|
||||||
|
return getAttachmentFile(SignalServiceAttachmentRemoteId.from(pointer.id()),
|
||||||
|
Optional.ofNullable(pointer.fileName()),
|
||||||
|
Optional.ofNullable(pointer.contentType()));
|
||||||
|
}
|
||||||
|
|
||||||
private void storeAttachment(final File attachmentFile, final AttachmentStorer storer) throws IOException {
|
private void storeAttachment(final File attachmentFile, final AttachmentStorer storer) throws IOException {
|
||||||
createAttachmentsDir();
|
createAttachmentsDir();
|
||||||
try (OutputStream output = new FileOutputStream(attachmentFile)) {
|
try (OutputStream output = new FileOutputStream(attachmentFile)) {
|
||||||
|
|
|
@ -272,6 +272,8 @@ public interface Manager extends Closeable {
|
||||||
|
|
||||||
void addClosedListener(Runnable listener);
|
void addClosedListener(Runnable listener);
|
||||||
|
|
||||||
|
File getAttachmentFile(AttachmentPointer pointer);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void close() throws IOException;
|
void close() throws IOException;
|
||||||
|
|
||||||
|
|
|
@ -1147,6 +1147,10 @@ class ManagerImpl implements Manager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public File getAttachmentFile(AttachmentPointer pointer) {
|
||||||
|
return context.getAttachmentHelper().getAttachmentFile(pointer);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
Thread thread;
|
Thread thread;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package org.asamk.signal.manager.helper;
|
package org.asamk.signal.manager.helper;
|
||||||
|
|
||||||
|
import org.asamk.signal.manager.AttachmentPointer;
|
||||||
import org.asamk.signal.manager.AttachmentStore;
|
import org.asamk.signal.manager.AttachmentStore;
|
||||||
import org.asamk.signal.manager.SignalDependencies;
|
import org.asamk.signal.manager.SignalDependencies;
|
||||||
import org.asamk.signal.manager.api.AttachmentInvalidException;
|
import org.asamk.signal.manager.api.AttachmentInvalidException;
|
||||||
|
@ -38,6 +39,11 @@ public class AttachmentHelper {
|
||||||
return attachmentStore.getAttachmentFile(pointer);
|
return attachmentStore.getAttachmentFile(pointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public File getAttachmentFile(AttachmentPointer pointer) {
|
||||||
|
return attachmentStore.getAttachmentFile(pointer);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public List<SignalServiceAttachment> uploadAttachments(final List<String> attachments) throws AttachmentInvalidException, IOException {
|
public List<SignalServiceAttachment> uploadAttachments(final List<String> attachments) throws AttachmentInvalidException, IOException {
|
||||||
var attachmentStreams = AttachmentUtils.createAttachmentStreams(attachments);
|
var attachmentStreams = AttachmentUtils.createAttachmentStreams(attachments);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
package org.asamk.signal.commands;
|
||||||
|
|
||||||
|
import net.sourceforge.argparse4j.inf.Namespace;
|
||||||
|
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.util.Base64;
|
||||||
|
|
||||||
|
public class AttachmentCommand implements JsonRpcLocalCommand {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "attachment";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void attachToSubparser(final Subparser subparser) {
|
||||||
|
subparser.addArgument("--file-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
|
||||||
|
public void handleCommand(
|
||||||
|
final Namespace ns,
|
||||||
|
final Manager m,
|
||||||
|
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 file = m.getAttachmentFile(new AttachmentPointer(id, fileName, contentType));
|
||||||
|
|
||||||
|
try {
|
||||||
|
final var bytes = Files.readAllBytes(file.toPath());
|
||||||
|
final var base64 = Base64.getEncoder().encodeToString(bytes);
|
||||||
|
|
||||||
|
if (outputWriter instanceof PlainTextWriter writer) {
|
||||||
|
writer.println(base64);
|
||||||
|
}
|
||||||
|
else if (outputWriter instanceof JsonWriter writer) {
|
||||||
|
writer.write(new JsonAttachmentData(base64));
|
||||||
|
}
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new UnexpectedErrorException("An error occurred reading attachment file: " + file, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ public class Commands {
|
||||||
|
|
||||||
static {
|
static {
|
||||||
addCommand(new AddDeviceCommand());
|
addCommand(new AddDeviceCommand());
|
||||||
|
addCommand(new AttachmentCommand());
|
||||||
addCommand(new BlockCommand());
|
addCommand(new BlockCommand());
|
||||||
addCommand(new DaemonCommand());
|
addCommand(new DaemonCommand());
|
||||||
addCommand(new DeleteLocalAccountDataCommand());
|
addCommand(new DeleteLocalAccountDataCommand());
|
||||||
|
|
|
@ -2,6 +2,7 @@ package org.asamk.signal.dbus;
|
||||||
|
|
||||||
import org.asamk.Signal;
|
import org.asamk.Signal;
|
||||||
import org.asamk.signal.DbusConfig;
|
import org.asamk.signal.DbusConfig;
|
||||||
|
import org.asamk.signal.manager.AttachmentPointer;
|
||||||
import org.asamk.signal.manager.Manager;
|
import org.asamk.signal.manager.Manager;
|
||||||
import org.asamk.signal.manager.api.AttachmentInvalidException;
|
import org.asamk.signal.manager.api.AttachmentInvalidException;
|
||||||
import org.asamk.signal.manager.api.Configuration;
|
import org.asamk.signal.manager.api.Configuration;
|
||||||
|
@ -907,6 +908,12 @@ public class DbusManagerImpl implements Manager {
|
||||||
}).toList();
|
}).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getAttachmentFile(final AttachmentPointer pointer) {
|
||||||
|
//TODO may need an implementation
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private <T> T getValue(
|
private <T> T getValue(
|
||||||
final Map<String, Variant<?>> stringVariantMap, final String field
|
final Map<String, Variant<?>> stringVariantMap, final String field
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
package org.asamk.signal.json;
|
||||||
|
|
||||||
|
public record JsonAttachmentData(String dataBase64) {}
|
Loading…
Add table
Add a link
Reference in a new issue