From fb240a7b36913b0149f57ba3449f9562833cfbbc Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Mon, 18 Jul 2022 19:51:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=82=D0=B8=D0=BF=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=84=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=BE=D0=B2=20ByteContainer.=20[ci=20skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- telegram-core/pom.xml | 2 +- .../telegram/domain/files/ByteContainer.java | 31 +++++++++++++++++++ .../domain/{ => files}/FileContainer.java | 10 +++--- .../service/AttachmentServiceImpl.java | 22 +++++++++++-- 5 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/files/ByteContainer.java rename telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/{ => files}/FileContainer.java (94%) 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);