diff --git a/pom.xml b/pom.xml index 1930614..b4491c5 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather telegram-bot - 0.0.14 + 0.0.15 pom diff --git a/telegram-core/pom.xml b/telegram-core/pom.xml index 7af7772..50d49e3 100644 --- a/telegram-core/pom.xml +++ b/telegram-core/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather telegram-bot - 0.0.14 + 0.0.15 telegram-core diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/files/ByteContainer.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/files/ByteContainer.java new file mode 100644 index 0000000..49f6cad --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/files/ByteContainer.java @@ -0,0 +1,31 @@ +package dev.struchkov.godfather.telegram.domain.files; + +public class ByteContainer { + + public static final ByteContainer EMPTY = new ByteContainer(null, null); + + private final String fileName; + private final byte[] bytes; + + public ByteContainer(String fileName, byte[] bytes) { + this.fileName = fileName; + this.bytes = bytes; + } + + public static ByteContainer empty() { + return EMPTY; + } + + public String getFileName() { + return fileName; + } + + public byte[] getBytes() { + return bytes; + } + + public boolean isNotEmpty() { + return bytes != null && bytes.length > 0; + } + +} diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/FileContainer.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/files/FileContainer.java similarity index 94% rename from telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/FileContainer.java rename to telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/files/FileContainer.java index ebc91bc..957ef41 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/FileContainer.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/files/FileContainer.java @@ -1,4 +1,4 @@ -package dev.struchkov.godfather.telegram.domain; +package dev.struchkov.godfather.telegram.domain.files; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,6 +21,10 @@ public class FileContainer { this.file = file; } + public static FileContainer empty() { + return EMPTY; + } + public String getFileName() { return fileName; } @@ -29,10 +33,6 @@ public class FileContainer { return file; } - public static FileContainer empty() { - return EMPTY; - } - public boolean isNotEmpty() { return file != null; } diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/AttachmentServiceImpl.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/AttachmentServiceImpl.java index 9552f48..50428fe 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/AttachmentServiceImpl.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/AttachmentServiceImpl.java @@ -1,9 +1,11 @@ package dev.struchkov.godfather.telegram.service; import dev.struchkov.godfather.telegram.TelegramConnect; -import dev.struchkov.godfather.telegram.domain.FileContainer; import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; +import dev.struchkov.godfather.telegram.domain.files.ByteContainer; +import dev.struchkov.godfather.telegram.domain.files.FileContainer; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,7 +19,6 @@ import java.io.InputStream; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Optional; import java.util.UUID; import java.util.stream.Stream; @@ -66,6 +67,23 @@ public class AttachmentServiceImpl { return FileContainer.empty(); } + public ByteContainer uploadBytes(@NotNull DocumentAttachment documentAttachment) { + isNotNull(documentAttachment); + try { + final byte[] bytes = downloadBytes(documentAttachment); + return new ByteContainer(documentAttachment.getFileName(), bytes); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return ByteContainer.empty(); + } + + private byte[] downloadBytes(DocumentAttachment documentAttachment) throws TelegramApiException, IOException { + final org.telegram.telegrambots.meta.api.objects.File file = getFilePath(documentAttachment); + final URL url = new URL(file.getFileUrl(botToken)); + return IOUtils.toByteArray(url); + } + private File downloadFile(DocumentAttachment documentAttachment) throws IOException, TelegramApiException { final org.telegram.telegrambots.meta.api.objects.File file = getFilePath(documentAttachment);