import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
+import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
import org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException;
import java.io.File;
}
public List<SignalServiceAttachment> uploadAttachments(final List<String> attachments) throws AttachmentInvalidException, IOException {
- var attachmentStreams = AttachmentUtils.getSignalServiceAttachments(attachments);
+ var attachmentStreams = AttachmentUtils.createAttachmentStreams(attachments);
// Upload attachments here, so we only upload once even for multiple recipients
- var messageSender = dependencies.getMessageSender();
var attachmentPointers = new ArrayList<SignalServiceAttachment>(attachmentStreams.size());
- for (var attachment : attachmentStreams) {
- if (attachment.isStream()) {
- attachmentPointers.add(messageSender.uploadAttachment(attachment.asStream()));
- } else if (attachment.isPointer()) {
- attachmentPointers.add(attachment.asPointer());
- }
+ for (var attachmentStream : attachmentStreams) {
+ attachmentPointers.add(uploadAttachment(attachmentStream));
}
return attachmentPointers;
}
+ public SignalServiceAttachmentPointer uploadAttachment(String attachment) throws IOException, AttachmentInvalidException {
+ var attachmentStream = AttachmentUtils.createAttachmentStream(new File(attachment));
+ return uploadAttachment(attachmentStream);
+ }
+
+ public SignalServiceAttachmentPointer uploadAttachment(SignalServiceAttachmentStream attachment) throws IOException {
+ var messageSender = dependencies.getMessageSender();
+ return messageSender.uploadAttachment(attachment);
+ }
+
public void downloadAttachment(final SignalServiceAttachment attachment) {
if (!attachment.isPointer()) {
logger.warn("Invalid state, can't store an attachment stream.");
package org.asamk.signal.manager.util;
import org.asamk.signal.manager.api.AttachmentInvalidException;
-import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
import org.whispersystems.signalservice.api.util.StreamDetails;
import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
public class AttachmentUtils {
- public static List<SignalServiceAttachment> getSignalServiceAttachments(List<String> attachments) throws AttachmentInvalidException {
- List<SignalServiceAttachment> signalServiceAttachments = null;
- if (attachments != null) {
- signalServiceAttachments = new ArrayList<>(attachments.size());
- for (var attachment : attachments) {
- try {
- signalServiceAttachments.add(createAttachment(new File(attachment)));
- } catch (IOException e) {
- throw new AttachmentInvalidException(attachment, e);
- }
- }
+ public static List<SignalServiceAttachmentStream> createAttachmentStreams(List<String> attachments) throws AttachmentInvalidException {
+ if (attachments == null) {
+ return null;
+ }
+ final var signalServiceAttachments = new ArrayList<SignalServiceAttachmentStream>(attachments.size());
+ for (var attachment : attachments) {
+ signalServiceAttachments.add(createAttachmentStream(new File(attachment)));
}
return signalServiceAttachments;
}
- public static SignalServiceAttachmentStream createAttachment(File attachmentFile) throws IOException {
- final var streamDetails = Utils.createStreamDetailsFromFile(attachmentFile);
- return createAttachment(streamDetails, Optional.of(attachmentFile.getName()));
+ public static SignalServiceAttachmentStream createAttachmentStream(File attachmentFile) throws AttachmentInvalidException {
+ try {
+ final var streamDetails = Utils.createStreamDetailsFromFile(attachmentFile);
+ return createAttachmentStream(streamDetails, Optional.of(attachmentFile.getName()));
+ } catch (IOException e) {
+ throw new AttachmentInvalidException(attachmentFile.toString(), e);
+ }
}
- public static SignalServiceAttachmentStream createAttachment(
+ public static SignalServiceAttachmentStream createAttachmentStream(
StreamDetails streamDetails, Optional<String> name
) {
- // TODO mabybe add a parameter to set the voiceNote, borderless, preview, width, height and caption option
+ // TODO maybe add a parameter to set the voiceNote, borderless, preview, width, height and caption option
final var uploadTimestamp = System.currentTimeMillis();
Optional<byte[]> preview = Optional.empty();
Optional<String> caption = Optional.empty();