Commit 8bc94872 authored by 以墨为白's avatar 以墨为白 🎧

图片上传-bug优化

parent 8a2e1b61
package com.zksy.szpt.domain.po;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.util.Date;
@TableName
public class XxImgindex {
/**
* 图片IID
......@@ -11,6 +16,7 @@ public class XxImgindex {
/**
* 图片ID
*/
@TableId(type = IdType.ASSIGN_ID)
private String id;
/**
......
package com.zksy.szpt.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zksy.szpt.domain.po.XxImgindex;
import com.zksy.szpt.domain.po.XxImgindexExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface XxImgindexMapper {
public interface XxImgindexMapper extends BaseMapper<XxImgindex> {
long countByExample(XxImgindexExample example);
int deleteByExample(XxImgindexExample example);
......
......@@ -2,8 +2,11 @@ package com.zksy.szpt.service;
import com.zksy.szpt.domain.UploadImageDTO;
import com.zksy.szpt.domain.po.XxImgfile;
import com.zksy.szpt.domain.po.XxImgindex;
import com.zksy.szpt.domain.po.XxImgindexExample;
import com.zksy.szpt.exception.NotificationException;
import com.zksy.szpt.mapper.XxImgfileMapper;
import com.zksy.szpt.mapper.XxImgindexMapper;
import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
......@@ -16,6 +19,8 @@ import reactor.netty.http.client.HttpClient;
import reactor.netty.tcp.TcpClient;
import java.net.URISyntaxException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@Service
public class ImageUploadService {
......@@ -24,17 +29,20 @@ public class ImageUploadService {
private final XxImgfileMapper xxImgfileMapper;
// 创建一个 TcpClient 并设置超时时间
final TcpClient tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) // 连接超时时间
.doOnConnected(connection -> connection.addHandlerLast(new ReadTimeoutHandler(5)) // 读取超时时间
.addHandlerLast(new WriteTimeoutHandler(5))); // 写入超时时间
private final XxImgindexMapper xxImgindexMapper;
public ImageUploadService(XxImgfileMapper xxImgfileMapper) {
public ImageUploadService(XxImgfileMapper xxImgfileMapper, XxImgindexMapper xxImgindexMapper) {
this.xxImgfileMapper = xxImgfileMapper;
this.xxImgindexMapper = xxImgindexMapper;
}
public Integer upload(UploadImageDTO uploadImageDTO) throws NotificationException {
// 创建一个 TcpClient 并设置超时时间
final TcpClient tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) // 连接超时时间
.doOnConnected(connection -> connection.addHandlerLast(new ReadTimeoutHandler(5)) // 读取超时时间
.addHandlerLast(new WriteTimeoutHandler(5))); // 写入超时时间
// 创建 WebClient 实例用于下载文件
WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient)))
......@@ -68,6 +76,16 @@ public class ImageUploadService {
throw new NotificationException("Failed to download file");
}
xxImgfile.setData(fileContent);
// 将文件内容转换为十六进制字符串
String fileContentHexString = toMd5(fileContent);
if (checkByMd5(fileContentHexString)) {
throw new NotificationException("File already exists");
}
//入库
XxImgindex xxImgindex = new XxImgindex();
xxImgindex.setOurl(uploadImageDTO.getImagePath());
xxImgindex.setMd5(fileContentHexString);
xxImgindexMapper.insert(xxImgindex);
return xxImgfileMapper.insert(xxImgfile);
} catch (Exception e) {
throw new NotificationException(e.getCause() == null ? e.getMessage() : e.getCause().getMessage());
......@@ -87,4 +105,24 @@ public class ImageUploadService {
// error.printStackTrace();
// });
}
private boolean checkByMd5(String imageUrl) {
XxImgindexExample example = new XxImgindexExample();
example.createCriteria().andMd5EqualTo(imageUrl);
return xxImgindexMapper.countByExample(example) >= 1;
}
private static final char[] hexCode = "0123456789ABCDEF".toCharArray();
private static String toMd5(byte[] data) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(data, 0, data.length);
byte[] hashBytes = digest.digest();
StringBuilder r = new StringBuilder(hashBytes.length * 2);
for (byte b : hashBytes) {
r.append(hexCode[(b >> 4) & 0xF]);
r.append(hexCode[(b & 0xF)]);
}
return r.toString();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment