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

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()
.map(photoSize -> {
final Picture picture = new Picture();
picture.setFileSize(photoSize.getFileSize());
picture.setFileSize(photoSize.getFileSize().longValue());
picture.setFileId(photoSize.getFileId());
picture.setHeight(photoSize.getHeight());
picture.setWeight(photoSize.getWidth());
picture.setFileUniqueId(photoSize.getFileUniqueId());
return picture;
}).toList();

View File

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

View File

@ -1,7 +1,6 @@
package dev.struchkov.godfather.telegram.quarkus.context.service;
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.FileContainer;
import io.smallrye.mutiny.Uni;
@ -13,6 +12,4 @@ public interface AttachmentService {
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) {
isNotNull(attachment);
return TelegramAttachmentType.PICTURE.name().equals(attachment.getType());
return TelegramAttachmentType.PICTURE_GROUP.name().equals(attachment.getType());
}
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));
}
@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) {
return telegramDownloadBytes(documentAttachment.getFileId());
}

View File

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

View File

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

View File

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

View File

@ -1,16 +1,11 @@
package dev.struchkov.godfather.telegram.domain.attachment;
import dev.struchkov.godfather.main.domain.content.Attachment;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class VideoAttachment extends Attachment {
private String fileId;
private Long fileSize;
private String fileName;
public class VideoAttachment extends FileAttachment {
public VideoAttachment() {
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 DOCUMENT -> parser.getCodec().treeToValue(node, DocumentAttachment.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 COMMAND -> parser.getCodec().treeToValue(node, CommandAttachment.class);
case VIDEO -> parser.getCodec().treeToValue(node, VideoAttachment.class);
case VOICE -> parser.getCodec().treeToValue(node, VoiceAttachment.class);
case STICKER -> parser.getCodec().treeToValue(node, StickerAttachment.class);
case PICTURE -> null;
};
}