mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Refactor attachment upload helpers
This commit is contained in:
parent
b292de8ae6
commit
0f701df91f
5 changed files with 33 additions and 28 deletions
|
@ -590,7 +590,7 @@ class ManagerImpl implements Manager {
|
||||||
stickerPack.getPackKey(),
|
stickerPack.getPackKey(),
|
||||||
stickerId,
|
stickerId,
|
||||||
manifestSticker.emoji(),
|
manifestSticker.emoji(),
|
||||||
AttachmentUtils.createAttachment(streamDetails, Optional.empty())));
|
AttachmentUtils.createAttachmentStream(streamDetails, Optional.empty())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
||||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
|
||||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
|
||||||
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
|
||||||
import org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException;
|
import org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -39,21 +40,26 @@ public class AttachmentHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
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.getSignalServiceAttachments(attachments);
|
var attachmentStreams = AttachmentUtils.createAttachmentStreams(attachments);
|
||||||
|
|
||||||
// Upload attachments here, so we only upload once even for multiple recipients
|
// Upload attachments here, so we only upload once even for multiple recipients
|
||||||
var messageSender = dependencies.getMessageSender();
|
|
||||||
var attachmentPointers = new ArrayList<SignalServiceAttachment>(attachmentStreams.size());
|
var attachmentPointers = new ArrayList<SignalServiceAttachment>(attachmentStreams.size());
|
||||||
for (var attachment : attachmentStreams) {
|
for (var attachmentStream : attachmentStreams) {
|
||||||
if (attachment.isStream()) {
|
attachmentPointers.add(uploadAttachment(attachmentStream));
|
||||||
attachmentPointers.add(messageSender.uploadAttachment(attachment.asStream()));
|
|
||||||
} else if (attachment.isPointer()) {
|
|
||||||
attachmentPointers.add(attachment.asPointer());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return attachmentPointers;
|
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) {
|
public void downloadAttachment(final SignalServiceAttachment attachment) {
|
||||||
if (!attachment.isPointer()) {
|
if (!attachment.isPointer()) {
|
||||||
logger.warn("Invalid state, can't store an attachment stream.");
|
logger.warn("Invalid state, can't store an attachment stream.");
|
||||||
|
|
|
@ -98,7 +98,7 @@ public class GroupHelper {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Optional.of(AttachmentUtils.createAttachment(streamDetails, Optional.empty()));
|
return Optional.of(AttachmentUtils.createAttachmentStream(streamDetails, Optional.empty()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public GroupInfoV2 getOrMigrateGroup(
|
public GroupInfoV2 getOrMigrateGroup(
|
||||||
|
|
|
@ -302,7 +302,7 @@ public class SyncHelper {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Optional.of(AttachmentUtils.createAttachment(streamDetails, Optional.empty()));
|
return Optional.of(AttachmentUtils.createAttachmentStream(streamDetails, Optional.empty()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void downloadContactAvatar(SignalServiceAttachment avatar, RecipientAddress address) {
|
private void downloadContactAvatar(SignalServiceAttachment avatar, RecipientAddress address) {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package org.asamk.signal.manager.util;
|
package org.asamk.signal.manager.util;
|
||||||
|
|
||||||
import org.asamk.signal.manager.api.AttachmentInvalidException;
|
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.messages.SignalServiceAttachmentStream;
|
||||||
import org.whispersystems.signalservice.api.util.StreamDetails;
|
import org.whispersystems.signalservice.api.util.StreamDetails;
|
||||||
import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
|
import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
|
||||||
|
@ -14,30 +13,30 @@ import java.util.Optional;
|
||||||
|
|
||||||
public class AttachmentUtils {
|
public class AttachmentUtils {
|
||||||
|
|
||||||
public static List<SignalServiceAttachment> getSignalServiceAttachments(List<String> attachments) throws AttachmentInvalidException {
|
public static List<SignalServiceAttachmentStream> createAttachmentStreams(List<String> attachments) throws AttachmentInvalidException {
|
||||||
List<SignalServiceAttachment> signalServiceAttachments = null;
|
if (attachments == null) {
|
||||||
if (attachments != null) {
|
return null;
|
||||||
signalServiceAttachments = new ArrayList<>(attachments.size());
|
}
|
||||||
for (var attachment : attachments) {
|
final var signalServiceAttachments = new ArrayList<SignalServiceAttachmentStream>(attachments.size());
|
||||||
try {
|
for (var attachment : attachments) {
|
||||||
signalServiceAttachments.add(createAttachment(new File(attachment)));
|
signalServiceAttachments.add(createAttachmentStream(new File(attachment)));
|
||||||
} catch (IOException e) {
|
|
||||||
throw new AttachmentInvalidException(attachment, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return signalServiceAttachments;
|
return signalServiceAttachments;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SignalServiceAttachmentStream createAttachment(File attachmentFile) throws IOException {
|
public static SignalServiceAttachmentStream createAttachmentStream(File attachmentFile) throws AttachmentInvalidException {
|
||||||
final var streamDetails = Utils.createStreamDetailsFromFile(attachmentFile);
|
try {
|
||||||
return createAttachment(streamDetails, Optional.of(attachmentFile.getName()));
|
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
|
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();
|
final var uploadTimestamp = System.currentTimeMillis();
|
||||||
Optional<byte[]> preview = Optional.empty();
|
Optional<byte[]> preview = Optional.empty();
|
||||||
Optional<String> caption = Optional.empty();
|
Optional<String> caption = Optional.empty();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue