remove Attachment.ext

This commit is contained in:
Timi
2026-01-04 19:13:02 +08:00
parent 040a46934d
commit 3410c540ab
5 changed files with 37 additions and 40 deletions

View File

@ -1,7 +1,5 @@
package com.imyeyu.api.modules.common.bean; package com.imyeyu.api.modules.common.bean;
import lombok.Data;
/** /**
* @author 夜雨 * @author 夜雨
* @since 2025-10-20 15:04 * @since 2025-10-20 15:04
@ -18,22 +16,4 @@ public class MediaAttach {
THUMB 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;
}
} }

View File

@ -1,6 +1,7 @@
package com.imyeyu.api.modules.common.bean; package com.imyeyu.api.modules.common.bean;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
/** /**
* @author 夜雨 * @author 夜雨
@ -9,7 +10,7 @@ import lombok.Data;
public class Metadata { public class Metadata {
/** /**
* * 图片
* *
* @author 夜雨 * @author 夜雨
* @since 2025-12-11 18:15 * @since 2025-12-11 18:15
@ -21,4 +22,21 @@ public class Metadata {
private int height; 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;
}
} }

View File

@ -79,8 +79,6 @@ public class Attachment extends Entity implements MultilingualHandler {
protected String md5; protected String md5;
protected String ext;
protected Long destroyAt; protected Long destroyAt;
@Transient @Transient

View File

@ -1,6 +1,7 @@
package com.imyeyu.api.modules.common.service.implement; package com.imyeyu.api.modules.common.service.implement;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.imyeyu.api.config.dbsource.TimiServerDBConfig; import com.imyeyu.api.config.dbsource.TimiServerDBConfig;
import com.imyeyu.api.modules.common.bean.MediaAttach; import com.imyeyu.api.modules.common.bean.MediaAttach;
import com.imyeyu.api.modules.common.bean.Metadata; 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()))); attachment.setMimeType(new Tika().detect(gridFSBucket.openDownloadStream(gridFSFile.getObjectId())));
if (attachment.getMimeType().startsWith("image")) { if (attachment.getMimeType().startsWith("image")) {
BufferedImage image = ImageIO.read(gridFSBucket.openDownloadStream(gridFSFile.getObjectId())); BufferedImage image = ImageIO.read(gridFSBucket.openDownloadStream(gridFSFile.getObjectId()));
Metadata.Image metadata = new Metadata.Image(); attachment.setMetadata(TimiJava.defaultIfNull(attachment.getMetadata(), new JsonObject()));
metadata.setWidth(image.getWidth()); JsonObject metadata = attachment.getMetadata();
metadata.setHeight(image.getHeight()); metadata.addProperty("width", image.getWidth());
attachment.setMetadata(gson.toJsonTree(metadata).getAsJsonObject()); metadata.addProperty("height", image.getHeight());
} }
mapper.insert(attachment); mapper.insert(attachment);
} catch (Exception e) { } catch (Exception e) {
@ -112,7 +113,9 @@ public class AttachmentServiceImplement extends AbstractEntityService<Attachment
Attachment attachment = get(id); Attachment attachment = get(id);
gridFsTemplate.delete(Query.query(Criteria.where("_id").is(attachment.getMongoId()))); 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()); attachment.setDestroyAt(Time.now());
mapper.update(attachment); mapper.update(attachment);
} catch (Exception e) { } catch (Exception e) {
@ -132,13 +135,11 @@ public class AttachmentServiceImplement extends AbstractEntityService<Attachment
// 生成缩略图 // 生成缩略图
InputStream mimeStream = getInputStreamByMongoId(attachment.getMongoId()); InputStream mimeStream = getInputStreamByMongoId(attachment.getMongoId());
String mimeType = new Tika().detect(mimeStream); String mimeType = new Tika().detect(mimeStream);
boolean isImage = false;
InputStream sourceStream = getInputStreamByMongoId(attachment.getMongoId()); InputStream sourceStream = getInputStreamByMongoId(attachment.getMongoId());
ByteArrayOutputStream thumbStream = new ByteArrayOutputStream(); ByteArrayOutputStream thumbStream = new ByteArrayOutputStream();
switch (mimeType) { switch (mimeType) {
case "image/png", "image/bmp", "image/jpeg" -> { case "image/png", "image/bmp", "image/jpeg" -> {
isImage = true;
Thumbnails.of(sourceStream).width(256).keepAspectRatio(true).toOutputStream(thumbStream); Thumbnails.of(sourceStream).width(256).keepAspectRatio(true).toOutputStream(thumbStream);
} }
case "video/mp4", "video/quicktime" -> { case "video/mp4", "video/quicktime" -> {
@ -155,18 +156,17 @@ public class AttachmentServiceImplement extends AbstractEntityService<Attachment
} }
} }
} }
MediaAttach.ExtData extData = new MediaAttach.ExtData(); Metadata.ThumbImage thumbMetadata = new Metadata.ThumbImage();
extData.setImage(isImage); thumbMetadata.setSourceId(attachment.getId());
extData.setVideo(!isImage); thumbMetadata.setSourceMongoId(attachment.getMongoId());
extData.setSourceId(attachment.getId()); thumbMetadata.setSourceMimeType(mimeType);
extData.setSourceMongoId(attachment.getMongoId());
Attachment thumbAttach = new Attachment(); Attachment thumbAttach = new Attachment();
thumbAttach.setName(Network.simpleURIFileName(attachment.getName()) + ".png"); thumbAttach.setName(Network.simpleURIFileName(attachment.getName()) + ".png");
thumbAttach.setBizType(attachment.getBizType()); thumbAttach.setBizType(attachment.getBizType());
thumbAttach.setBizId(attachment.getBizId()); thumbAttach.setBizId(attachment.getBizId());
thumbAttach.setAttachTypeValue(MediaAttach.Type.THUMB); thumbAttach.setAttachTypeValue(MediaAttach.Type.THUMB);
thumbAttach.setExt(gson.toJson(extData)); thumbAttach.setMetadata(gson.toJsonTree(thumbMetadata).getAsJsonObject());
thumbAttach.setInputStream(new ByteArrayInputStream(thumbStream.toByteArray())); thumbAttach.setInputStream(new ByteArrayInputStream(thumbStream.toByteArray()));
create(thumbAttach); create(thumbAttach);
@ -182,8 +182,8 @@ public class AttachmentServiceImplement extends AbstractEntityService<Attachment
public void deleteMedia(Long thumbId) throws TimiException { public void deleteMedia(Long thumbId) throws TimiException {
Attachment attachment = get(thumbId); Attachment attachment = get(thumbId);
delete(attachment.getId()); delete(attachment.getId());
MediaAttach.ExtData data = gson.fromJson(attachment.getExt(), MediaAttach.ExtData.class); Metadata.ThumbImage thumbMetadata = gson.fromJson(attachment.getMetadata(), Metadata.ThumbImage.class);
delete(data.getSourceId()); delete(thumbMetadata.getSourceId());
} }
@Override @Override

View File

@ -3,6 +3,7 @@ package com.imyeyu.api.modules.journal.service.implement;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.imyeyu.api.config.dbsource.TimiServerDBConfig; import com.imyeyu.api.config.dbsource.TimiServerDBConfig;
import com.imyeyu.api.modules.common.bean.MediaAttach; 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.bean.TempFileMetaData;
import com.imyeyu.api.modules.common.entity.Attachment; import com.imyeyu.api.modules.common.entity.Attachment;
import com.imyeyu.api.modules.common.service.AttachmentService; import com.imyeyu.api.modules.common.service.AttachmentService;
@ -196,8 +197,8 @@ public class JournalServiceImplement extends AbstractEntityService<Journal, Long
thumbAttach.setBizId(journal.getId()); thumbAttach.setBizId(journal.getId());
attachmentService.update(thumbAttach); attachmentService.update(thumbAttach);
MediaAttach.ExtData extData = gson.fromJson(thumbAttach.getExt(), MediaAttach.ExtData.class); Metadata.ThumbImage thumbMetadata = gson.fromJson(thumbAttach.getMetadata(), Metadata.ThumbImage.class);
Attachment sourceAttach = attachmentService.get(extData.getSourceId()); Attachment sourceAttach = attachmentService.get(thumbMetadata.getSourceId());
sourceAttach.setBizType(Attachment.BizType.JOURNAL); sourceAttach.setBizType(Attachment.BizType.JOURNAL);
sourceAttach.setBizId(journal.getId()); sourceAttach.setBizId(journal.getId());
attachmentService.update(sourceAttach); attachmentService.update(sourceAttach);