Добавил новый тип получения файлов ByteContainer. [ci skip]

This commit is contained in:
Struchkov Mark 2022-07-18 19:51:03 +03:00
parent 1c43f1d797
commit fb240a7b36
5 changed files with 58 additions and 9 deletions

View File

@ -5,7 +5,7 @@
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.14</version> <version>0.0.15</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.14</version> <version>0.0.15</version>
</parent> </parent>
<artifactId>telegram-core</artifactId> <artifactId>telegram-core</artifactId>

View File

@ -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;
}
}

View File

@ -1,4 +1,4 @@
package dev.struchkov.godfather.telegram.domain; package dev.struchkov.godfather.telegram.domain.files;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -21,6 +21,10 @@ public class FileContainer {
this.file = file; this.file = file;
} }
public static FileContainer empty() {
return EMPTY;
}
public String getFileName() { public String getFileName() {
return fileName; return fileName;
} }
@ -29,10 +33,6 @@ public class FileContainer {
return file; return file;
} }
public static FileContainer empty() {
return EMPTY;
}
public boolean isNotEmpty() { public boolean isNotEmpty() {
return file != null; return file != null;
} }

View File

@ -1,9 +1,11 @@
package dev.struchkov.godfather.telegram.service; package dev.struchkov.godfather.telegram.service;
import dev.struchkov.godfather.telegram.TelegramConnect; 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.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.FileUtils;
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -17,7 +19,6 @@ import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -66,6 +67,23 @@ public class AttachmentServiceImpl {
return FileContainer.empty(); 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 { private File downloadFile(DocumentAttachment documentAttachment) throws IOException, TelegramApiException {
final org.telegram.telegrambots.meta.api.objects.File file = getFilePath(documentAttachment); final org.telegram.telegrambots.meta.api.objects.File file = getFilePath(documentAttachment);