package com.ruoyi.common.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.common.config.MinioConfig;
|
import com.ruoyi.common.enums.AttachmentType;
|
import com.ruoyi.common.mapper.AttachmentTableMapper;
|
import com.ruoyi.common.pojo.AttachmentTable;
|
import com.ruoyi.common.service.AttachmentTableService;
|
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.uuid.UUID;
|
import io.minio.*;
|
import io.minio.http.Method;
|
import lombok.AllArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import javax.servlet.ServletOutputStream;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.InputStream;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
|
@Service
|
@AllArgsConstructor
|
public class AttachmentTableServiceImpl extends ServiceImpl<AttachmentTableMapper, AttachmentTable> implements AttachmentTableService {
|
|
private MinioConfig minioConfig;
|
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void upload(MultipartFile file, Integer type, Integer id) {
|
MinioClient build = MinioClient.builder()
|
.endpoint(minioConfig.getEndpoint(), minioConfig.getPort(), minioConfig.getSecure())
|
.credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey())
|
.build();
|
String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename();
|
try(InputStream inputStream = file.getInputStream()) {
|
build.putObject(
|
PutObjectArgs.builder()
|
.bucket(minioConfig.getBucketName())
|
.object(fileName)
|
.stream(inputStream, file.getSize(), -1)
|
.contentType("application/octet-stream")
|
.userMetadata(Collections.singletonMap("Content-Disposition", "attachment; filename=\"" + fileName + "\""))
|
.build()
|
);
|
// 保存到数据库
|
AttachmentTable attachmentTable = new AttachmentTable();
|
attachmentTable.setSubclassId(id);
|
attachmentTable.setFileName(fileName);
|
attachmentTable.setOriginalFileName(file.getOriginalFilename());
|
attachmentTable.setFileType(file.getContentType());
|
attachmentTable.setBucketName(minioConfig.getBucketName());
|
attachmentTable.setOtherTableName(AttachmentType.getAttachmentValue(type));
|
baseMapper.insert(attachmentTable);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
@Override
|
public void downLoad(Integer id, Integer type,String code,String suffix, HttpServletResponse response) {
|
// 查询数据库
|
List<AttachmentTable> list = baseMapper.selectList(new LambdaQueryWrapper<AttachmentTable>()
|
.eq(AttachmentTable::getSubclassId, id)
|
.eq(AttachmentTable::getOtherTableName, AttachmentType.getAttachmentValue(type))
|
.like(AttachmentTable::getFileName, suffix)
|
.orderByDesc(AttachmentTable::getId));
|
if(CollectionUtils.isNotEmpty(list)) {
|
AttachmentTable attachmentTable = list.get(0);
|
String fileName = attachmentTable.getFileName(); // 文件名
|
MinioClient build = MinioClient.builder()
|
.endpoint(minioConfig.getEndpoint(), minioConfig.getPort(), minioConfig.getSecure())
|
.credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey())
|
.build();
|
try( InputStream inputStream = build.getObject(GetObjectArgs.builder().bucket(minioConfig.getBucketName()).object(fileName).build());
|
ServletOutputStream stream = response.getOutputStream()) {
|
String finalName = "";
|
String fileNameMinIo = attachmentTable.getFileName();
|
String suffix1 = fileNameMinIo.substring(fileNameMinIo.lastIndexOf(".")+1);
|
String contentType = getContentType(suffix1);
|
if(StringUtils.isNotEmpty(code)) {
|
finalName = code + "." + suffix1;
|
}else {
|
finalName = fileNameMinIo;
|
}
|
// 设置响应头
|
response.setContentType(contentType);
|
response.setHeader("Content-disposition", "attachment;filename=" + finalName);
|
byte[] bytes = new byte[1024];
|
int len;
|
while ((len = inputStream.read(bytes)) != -1) {
|
stream.write(bytes, 0, len);
|
stream.flush();
|
}
|
}catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
|
}
|
}
|
|
@Override
|
public Map<String, Object> getAttachmentList(Integer id, Integer type,String suffix) {
|
HashMap<String, Object> map = new HashMap<>();
|
// 查询数据库
|
List<AttachmentTable> list = baseMapper.selectList(new LambdaQueryWrapper<AttachmentTable>()
|
.eq(AttachmentTable::getSubclassId, id)
|
.eq(AttachmentTable::getOtherTableName, AttachmentType.getAttachmentValue(type))
|
.like(StringUtils.isNotEmpty(suffix),AttachmentTable::getFileName, suffix)
|
.orderByDesc(AttachmentTable::getId));
|
if(CollectionUtils.isNotEmpty(list)) {
|
String contentType = getContentType(suffix.replace(".",""));
|
map.put("contentType", contentType);
|
map.put("suffix", suffix);
|
}
|
return map;
|
}
|
|
@Override
|
public String getURL(Integer id, Integer type , String suffix) {
|
String url = "";
|
// 查询数据库 获取最新文件名
|
List<AttachmentTable> attachmentTables = baseMapper.selectList(new LambdaQueryWrapper<AttachmentTable>()
|
.eq(AttachmentTable::getSubclassId, id)
|
.eq(AttachmentTable::getOtherTableName, AttachmentType.getAttachmentValue(type))
|
.like(AttachmentTable::getFileName, suffix)
|
.orderByDesc(AttachmentTable::getId));
|
if(CollectionUtils.isNotEmpty(attachmentTables)) {
|
AttachmentTable attachmentTable = attachmentTables.get(0);
|
MinioClient build = MinioClient.builder()
|
.endpoint(minioConfig.getEndpoint(), minioConfig.getPort(), minioConfig.getSecure())
|
.credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey())
|
.build();
|
try {
|
url = build.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
.method(Method.GET)
|
.bucket(minioConfig.getBucketName())
|
.object(attachmentTable.getFileName())
|
.expiry(60 * 60 * 24)
|
.build());
|
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
|
}
|
return url;
|
}
|
|
public InputStream getInputStream(Integer id, Integer type , String suffix) {
|
InputStream inputStream = null;
|
// 查询数据库 获取最新文件名
|
List<AttachmentTable> attachmentTables = baseMapper.selectList(new LambdaQueryWrapper<AttachmentTable>()
|
.eq(AttachmentTable::getSubclassId, id)
|
.eq(AttachmentTable::getOtherTableName, AttachmentType.getAttachmentValue(type))
|
.like(StringUtils.isNotEmpty(suffix),AttachmentTable::getFileName, suffix)
|
.orderByDesc(AttachmentTable::getId));
|
if(CollectionUtils.isNotEmpty(attachmentTables)) {
|
AttachmentTable attachmentTable = attachmentTables.get(0);
|
MinioClient build = MinioClient.builder()
|
.endpoint(minioConfig.getEndpoint(), minioConfig.getPort(), minioConfig.getSecure())
|
.credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey())
|
.build();
|
try {
|
inputStream = build.getObject(
|
GetObjectArgs.builder()
|
.bucket(minioConfig.getBucketName())
|
.object(attachmentTable.getFileName())
|
.build());
|
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
}
|
return inputStream;
|
}
|
|
|
|
|
// 根据文件扩展名获取 MIME 类型
|
private String getContentType(String fileExtension) {
|
switch (fileExtension) {
|
case "jpg":
|
case "jpeg":
|
return "image/jpeg";
|
case "png":
|
return "image/png";
|
case "gif":
|
return "image/gif";
|
case "pdf":
|
return "application/pdf";
|
case "doc":
|
return "application/msword";
|
case "docx":
|
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
case "xls":
|
return "application/vnd.ms-excel";
|
case "xlsx":
|
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
case "ppt":
|
case "pptx":
|
return "application/vnd.ms-powerpoint";
|
default:
|
return "application/octet-stream"; // 默认二进制流
|
}
|
}
|
}
|