Добавил новый тип получения файлов ByteContainer. [ci skip]
This commit is contained in:
parent
1c43f1d797
commit
fb240a7b36
2
pom.xml
2
pom.xml
@ -5,7 +5,7 @@
|
||||
|
||||
<groupId>dev.struchkov.godfather</groupId>
|
||||
<artifactId>telegram-bot</artifactId>
|
||||
<version>0.0.14</version>
|
||||
<version>0.0.15</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>dev.struchkov.godfather</groupId>
|
||||
<artifactId>telegram-bot</artifactId>
|
||||
<version>0.0.14</version>
|
||||
<version>0.0.15</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>telegram-core</artifactId>
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user