remove Attachment.ext
This commit is contained in:
@ -1,7 +1,5 @@
|
||||
package com.imyeyu.api.modules.common.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2025-10-20 15:04
|
||||
@ -18,22 +16,4 @@ public class MediaAttach {
|
||||
|
||||
THUMB
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-10-20 15:04
|
||||
*/
|
||||
@Data
|
||||
public static class ExtData {
|
||||
|
||||
private Long sourceId;
|
||||
|
||||
private String sourceMongoId;
|
||||
|
||||
private boolean isImage;
|
||||
|
||||
private boolean isVideo;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.imyeyu.api.modules.common.bean;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
@ -9,7 +10,7 @@ import lombok.Data;
|
||||
public class Metadata {
|
||||
|
||||
/**
|
||||
*
|
||||
* 图片
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-12-11 18:15
|
||||
@ -21,4 +22,21 @@ public class Metadata {
|
||||
|
||||
private int height;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缩略图
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2026-01-04 18:10
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public static class ThumbImage extends Image {
|
||||
|
||||
private long sourceId;
|
||||
|
||||
private String sourceMongoId;
|
||||
|
||||
private String sourceMimeType;
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,8 +79,6 @@ public class Attachment extends Entity implements MultilingualHandler {
|
||||
|
||||
protected String md5;
|
||||
|
||||
protected String ext;
|
||||
|
||||
protected Long destroyAt;
|
||||
|
||||
@Transient
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.imyeyu.api.modules.common.service.implement;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.imyeyu.api.config.dbsource.TimiServerDBConfig;
|
||||
import com.imyeyu.api.modules.common.bean.MediaAttach;
|
||||
import com.imyeyu.api.modules.common.bean.Metadata;
|
||||
@ -90,10 +91,10 @@ public class AttachmentServiceImplement extends AbstractEntityService<Attachment
|
||||
attachment.setMimeType(new Tika().detect(gridFSBucket.openDownloadStream(gridFSFile.getObjectId())));
|
||||
if (attachment.getMimeType().startsWith("image")) {
|
||||
BufferedImage image = ImageIO.read(gridFSBucket.openDownloadStream(gridFSFile.getObjectId()));
|
||||
Metadata.Image metadata = new Metadata.Image();
|
||||
metadata.setWidth(image.getWidth());
|
||||
metadata.setHeight(image.getHeight());
|
||||
attachment.setMetadata(gson.toJsonTree(metadata).getAsJsonObject());
|
||||
attachment.setMetadata(TimiJava.defaultIfNull(attachment.getMetadata(), new JsonObject()));
|
||||
JsonObject metadata = attachment.getMetadata();
|
||||
metadata.addProperty("width", image.getWidth());
|
||||
metadata.addProperty("height", image.getHeight());
|
||||
}
|
||||
mapper.insert(attachment);
|
||||
} catch (Exception e) {
|
||||
@ -112,7 +113,9 @@ public class AttachmentServiceImplement extends AbstractEntityService<Attachment
|
||||
Attachment attachment = get(id);
|
||||
gridFsTemplate.delete(Query.query(Criteria.where("_id").is(attachment.getMongoId())));
|
||||
|
||||
attachment.setDeletedAt(Time.now());
|
||||
if (!attachment.isDeleted()) {
|
||||
attachment.setDeletedAt(Time.now());
|
||||
}
|
||||
attachment.setDestroyAt(Time.now());
|
||||
mapper.update(attachment);
|
||||
} catch (Exception e) {
|
||||
@ -132,13 +135,11 @@ public class AttachmentServiceImplement extends AbstractEntityService<Attachment
|
||||
// 生成缩略图
|
||||
InputStream mimeStream = getInputStreamByMongoId(attachment.getMongoId());
|
||||
String mimeType = new Tika().detect(mimeStream);
|
||||
boolean isImage = false;
|
||||
|
||||
InputStream sourceStream = getInputStreamByMongoId(attachment.getMongoId());
|
||||
ByteArrayOutputStream thumbStream = new ByteArrayOutputStream();
|
||||
switch (mimeType) {
|
||||
case "image/png", "image/bmp", "image/jpeg" -> {
|
||||
isImage = true;
|
||||
Thumbnails.of(sourceStream).width(256).keepAspectRatio(true).toOutputStream(thumbStream);
|
||||
}
|
||||
case "video/mp4", "video/quicktime" -> {
|
||||
@ -155,18 +156,17 @@ public class AttachmentServiceImplement extends AbstractEntityService<Attachment
|
||||
}
|
||||
}
|
||||
}
|
||||
MediaAttach.ExtData extData = new MediaAttach.ExtData();
|
||||
extData.setImage(isImage);
|
||||
extData.setVideo(!isImage);
|
||||
extData.setSourceId(attachment.getId());
|
||||
extData.setSourceMongoId(attachment.getMongoId());
|
||||
Metadata.ThumbImage thumbMetadata = new Metadata.ThumbImage();
|
||||
thumbMetadata.setSourceId(attachment.getId());
|
||||
thumbMetadata.setSourceMongoId(attachment.getMongoId());
|
||||
thumbMetadata.setSourceMimeType(mimeType);
|
||||
|
||||
Attachment thumbAttach = new Attachment();
|
||||
thumbAttach.setName(Network.simpleURIFileName(attachment.getName()) + ".png");
|
||||
thumbAttach.setBizType(attachment.getBizType());
|
||||
thumbAttach.setBizId(attachment.getBizId());
|
||||
thumbAttach.setAttachTypeValue(MediaAttach.Type.THUMB);
|
||||
thumbAttach.setExt(gson.toJson(extData));
|
||||
thumbAttach.setMetadata(gson.toJsonTree(thumbMetadata).getAsJsonObject());
|
||||
thumbAttach.setInputStream(new ByteArrayInputStream(thumbStream.toByteArray()));
|
||||
create(thumbAttach);
|
||||
|
||||
@ -182,8 +182,8 @@ public class AttachmentServiceImplement extends AbstractEntityService<Attachment
|
||||
public void deleteMedia(Long thumbId) throws TimiException {
|
||||
Attachment attachment = get(thumbId);
|
||||
delete(attachment.getId());
|
||||
MediaAttach.ExtData data = gson.fromJson(attachment.getExt(), MediaAttach.ExtData.class);
|
||||
delete(data.getSourceId());
|
||||
Metadata.ThumbImage thumbMetadata = gson.fromJson(attachment.getMetadata(), Metadata.ThumbImage.class);
|
||||
delete(thumbMetadata.getSourceId());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -3,6 +3,7 @@ package com.imyeyu.api.modules.journal.service.implement;
|
||||
import com.google.gson.Gson;
|
||||
import com.imyeyu.api.config.dbsource.TimiServerDBConfig;
|
||||
import com.imyeyu.api.modules.common.bean.MediaAttach;
|
||||
import com.imyeyu.api.modules.common.bean.Metadata;
|
||||
import com.imyeyu.api.modules.common.bean.TempFileMetaData;
|
||||
import com.imyeyu.api.modules.common.entity.Attachment;
|
||||
import com.imyeyu.api.modules.common.service.AttachmentService;
|
||||
@ -196,8 +197,8 @@ public class JournalServiceImplement extends AbstractEntityService<Journal, Long
|
||||
thumbAttach.setBizId(journal.getId());
|
||||
attachmentService.update(thumbAttach);
|
||||
|
||||
MediaAttach.ExtData extData = gson.fromJson(thumbAttach.getExt(), MediaAttach.ExtData.class);
|
||||
Attachment sourceAttach = attachmentService.get(extData.getSourceId());
|
||||
Metadata.ThumbImage thumbMetadata = gson.fromJson(thumbAttach.getMetadata(), Metadata.ThumbImage.class);
|
||||
Attachment sourceAttach = attachmentService.get(thumbMetadata.getSourceId());
|
||||
sourceAttach.setBizType(Attachment.BizType.JOURNAL);
|
||||
sourceAttach.setBizId(journal.getId());
|
||||
attachmentService.update(sourceAttach);
|
||||
|
||||
Reference in New Issue
Block a user