Улучшил работу с файлами

This commit is contained in:
Struchkov Mark 2023-11-27 14:21:50 +03:00
parent 02f037c292
commit c8bb581599
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
10 changed files with 17 additions and 33 deletions

View File

@ -79,11 +79,10 @@ public final class MessageChatMailConvert {
final List<Picture> pictures = photoSizes.stream() final List<Picture> pictures = photoSizes.stream()
.map(photoSize -> { .map(photoSize -> {
final Picture picture = new Picture(); final Picture picture = new Picture();
picture.setFileSize(photoSize.getFileSize()); picture.setFileSize(photoSize.getFileSize().longValue());
picture.setFileId(photoSize.getFileId()); picture.setFileId(photoSize.getFileId());
picture.setHeight(photoSize.getHeight()); picture.setHeight(photoSize.getHeight());
picture.setWeight(photoSize.getWidth()); picture.setWeight(photoSize.getWidth());
picture.setFileUniqueId(photoSize.getFileUniqueId());
return picture; return picture;
}).toList(); }).toList();

View File

@ -74,6 +74,7 @@ public final class MessageMailConvert {
convertPhoto(message.getPhoto()).ifPresent(mail::addAttachment); convertPhoto(message.getPhoto()).ifPresent(mail::addAttachment);
convertVideo(message.getVideo()).ifPresent(mail::addAttachment); convertVideo(message.getVideo()).ifPresent(mail::addAttachment);
convertVoice(message.getVoice()).ifPresent(mail::addAttachment); convertVoice(message.getVoice()).ifPresent(mail::addAttachment);
convertSticker(message.getSticker()).ifPresent(mail::addAttachment);
final List<MessageEntity> entities = message.getEntities(); final List<MessageEntity> entities = message.getEntities();
if (entities != null) { if (entities != null) {
@ -104,11 +105,10 @@ public final class MessageMailConvert {
final List<Picture> pictures = photoSizes.stream() final List<Picture> pictures = photoSizes.stream()
.map(photoSize -> { .map(photoSize -> {
final Picture picture = new Picture(); final Picture picture = new Picture();
picture.setFileSize(photoSize.getFileSize()); picture.setFileSize(photoSize.getFileSize().longValue());
picture.setFileId(photoSize.getFileId()); picture.setFileId(photoSize.getFileId());
picture.setHeight(photoSize.getHeight()); picture.setHeight(photoSize.getHeight());
picture.setWeight(photoSize.getWidth()); picture.setWeight(photoSize.getWidth());
picture.setFileUniqueId(photoSize.getFileUniqueId());
return picture; return picture;
}).toList(); }).toList();
@ -162,7 +162,7 @@ public final class MessageMailConvert {
return Optional.empty(); return Optional.empty();
} }
private static Optional<StickerAttachment> convertVoice(Sticker sticker) { private static Optional<StickerAttachment> convertSticker(Sticker sticker) {
if (sticker != null) { if (sticker != null) {
final StickerAttachment attachment = new StickerAttachment(); final StickerAttachment attachment = new StickerAttachment();
attachment.setFileId(sticker.getFileId()); attachment.setFileId(sticker.getFileId());
@ -181,6 +181,7 @@ public final class MessageMailConvert {
attachment.setFileId(video.getFileId()); attachment.setFileId(video.getFileId());
attachment.setFileSize(video.getFileSize()); attachment.setFileSize(video.getFileSize());
attachment.setFileName(video.getFileName()); attachment.setFileName(video.getFileName());
attachment.setMimeType(video.getMimeType());
return Optional.of(attachment); return Optional.of(attachment);
} }
return Optional.empty(); return Optional.empty();

View File

@ -1,7 +1,6 @@
package dev.struchkov.godfather.telegram.quarkus.context.service; package dev.struchkov.godfather.telegram.quarkus.context.service;
import dev.struchkov.godfather.telegram.domain.attachment.FileAttachment; import dev.struchkov.godfather.telegram.domain.attachment.FileAttachment;
import dev.struchkov.godfather.telegram.domain.attachment.Picture;
import dev.struchkov.godfather.telegram.domain.files.ByteContainer; import dev.struchkov.godfather.telegram.domain.files.ByteContainer;
import dev.struchkov.godfather.telegram.domain.files.FileContainer; import dev.struchkov.godfather.telegram.domain.files.FileContainer;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
@ -13,6 +12,4 @@ public interface AttachmentService {
Uni<ByteContainer> uploadBytes(@NotNull FileAttachment fileAttachment); Uni<ByteContainer> uploadBytes(@NotNull FileAttachment fileAttachment);
Uni<ByteContainer> uploadBytes(@NotNull Picture picture);
} }

View File

@ -176,7 +176,7 @@ public final class Attachments {
public static boolean isPictureGroup(Attachment attachment) { public static boolean isPictureGroup(Attachment attachment) {
isNotNull(attachment); isNotNull(attachment);
return TelegramAttachmentType.PICTURE.name().equals(attachment.getType()); return TelegramAttachmentType.PICTURE_GROUP.name().equals(attachment.getType());
} }
public static boolean isLink(Attachment attachment) { public static boolean isLink(Attachment attachment) {

View File

@ -77,17 +77,6 @@ public class AttachmentServiceImpl implements AttachmentService {
.onItem().ifNotNull().transform(bytes -> new ByteContainer(fileAttachment.getFileName(), fileAttachment.getMimeType(), bytes)); .onItem().ifNotNull().transform(bytes -> new ByteContainer(fileAttachment.getFileName(), fileAttachment.getMimeType(), bytes));
} }
@Override
public Uni<ByteContainer> uploadBytes(@NotNull Picture picture) {
isNotNull(picture);
return downloadBytes(picture)
.onItem().ifNotNull().transform(bytes -> new ByteContainer(null, "image/jpeg", bytes));
}
private Uni<byte[]> downloadBytes(Picture picture) {
return telegramDownloadBytes(picture.getFileId());
}
private Uni<byte[]> downloadBytes(FileAttachment documentAttachment) { private Uni<byte[]> downloadBytes(FileAttachment documentAttachment) {
return telegramDownloadBytes(documentAttachment.getFileId()); return telegramDownloadBytes(documentAttachment.getFileId());
} }

View File

@ -5,12 +5,13 @@ import lombok.Setter;
@Getter @Getter
@Setter @Setter
public class Picture { public class Picture extends FileAttachment {
private String fileId;
private String fileUniqueId;
private Integer fileSize;
private Integer weight; private Integer weight;
private Integer height; private Integer height;
public Picture() {
super(TelegramAttachmentType.PICTURE.name());
}
} }

View File

@ -15,12 +15,12 @@ public class PictureGroupAttachment extends Attachment {
private List<Picture> pictures; private List<Picture> pictures;
public PictureGroupAttachment() { public PictureGroupAttachment() {
super(TelegramAttachmentType.PICTURE.name()); super(TelegramAttachmentType.PICTURE_GROUP.name());
} }
public Optional<Picture> getLargePicture() { public Optional<Picture> getLargePicture() {
return pictures.stream() return pictures.stream()
.max(Comparator.comparingInt(Picture::getFileSize)); .max(Comparator.comparing(Picture::getFileSize));
} }
} }

View File

@ -4,6 +4,7 @@ public enum TelegramAttachmentType {
DOCUMENT, DOCUMENT,
CONTACT, CONTACT,
PICTURE_GROUP,
PICTURE, PICTURE,
LINK, LINK,
COMMAND, COMMAND,

View File

@ -1,16 +1,11 @@
package dev.struchkov.godfather.telegram.domain.attachment; package dev.struchkov.godfather.telegram.domain.attachment;
import dev.struchkov.godfather.main.domain.content.Attachment;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@Getter @Getter
@Setter @Setter
public class VideoAttachment extends Attachment { public class VideoAttachment extends FileAttachment {
private String fileId;
private Long fileSize;
private String fileName;
public VideoAttachment() { public VideoAttachment() {
super(TelegramAttachmentType.VIDEO.name()); super(TelegramAttachmentType.VIDEO.name());

View File

@ -39,12 +39,13 @@ public class TelegramAttachmentDeserializer extends StdDeserializer<Attachment>
case BUTTON_CLICK -> parser.getCodec().treeToValue(node, ButtonClickAttachment.class); case BUTTON_CLICK -> parser.getCodec().treeToValue(node, ButtonClickAttachment.class);
case DOCUMENT -> parser.getCodec().treeToValue(node, DocumentAttachment.class); case DOCUMENT -> parser.getCodec().treeToValue(node, DocumentAttachment.class);
case CONTACT -> parser.getCodec().treeToValue(node, ContactAttachment.class); case CONTACT -> parser.getCodec().treeToValue(node, ContactAttachment.class);
case PICTURE -> parser.getCodec().treeToValue(node, PictureGroupAttachment.class); case PICTURE_GROUP -> parser.getCodec().treeToValue(node, PictureGroupAttachment.class);
case LINK -> parser.getCodec().treeToValue(node, LinkAttachment.class); case LINK -> parser.getCodec().treeToValue(node, LinkAttachment.class);
case COMMAND -> parser.getCodec().treeToValue(node, CommandAttachment.class); case COMMAND -> parser.getCodec().treeToValue(node, CommandAttachment.class);
case VIDEO -> parser.getCodec().treeToValue(node, VideoAttachment.class); case VIDEO -> parser.getCodec().treeToValue(node, VideoAttachment.class);
case VOICE -> parser.getCodec().treeToValue(node, VoiceAttachment.class); case VOICE -> parser.getCodec().treeToValue(node, VoiceAttachment.class);
case STICKER -> parser.getCodec().treeToValue(node, StickerAttachment.class); case STICKER -> parser.getCodec().treeToValue(node, StickerAttachment.class);
case PICTURE -> null;
}; };
} }