这个实现类被被蒋取代
This commit is contained in:
parent
067528e4de
commit
9e1a46bd52
|
|
@ -1,136 +0,0 @@
|
||||||
package com.bonus.file.service.impl;
|
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.bonus.common.core.utils.file.MimeTypeUtils;
|
|
||||||
import com.bonus.file.service.ISysFileService;
|
|
||||||
import io.minio.RemoveObjectArgs;
|
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
import com.bonus.file.utils.FileDownloadUtils;
|
|
||||||
import com.bonus.system.api.domain.SysFile;
|
|
||||||
import io.minio.GetObjectArgs;
|
|
||||||
import io.minio.errors.MinioException;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
import com.alibaba.nacos.common.utils.IoUtils;
|
|
||||||
import com.bonus.file.config.MinioConfig;
|
|
||||||
import com.bonus.file.utils.FileUploadUtils;
|
|
||||||
import io.minio.MinioClient;
|
|
||||||
import io.minio.PutObjectArgs;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Minio 文件存储
|
|
||||||
*
|
|
||||||
* @author bonus
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
@ConditionalOnProperty(name = "storage.type", havingValue = "minio")
|
|
||||||
public class MinioSysFileServiceImpl implements ISysFileService
|
|
||||||
{
|
|
||||||
@Autowired
|
|
||||||
private MinioConfig minioConfig;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private MinioClient client;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Minio文件上传接口
|
|
||||||
*
|
|
||||||
* @param file 上传的文件
|
|
||||||
* @return 访问地址
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public SysFile uploadFile(MultipartFile file) throws Exception
|
|
||||||
{
|
|
||||||
//验证文件扩展名和大小
|
|
||||||
FileUploadUtils.assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
|
||||||
|
|
||||||
String fileName = FileUploadUtils.extractFilename(file);
|
|
||||||
InputStream inputStream = file.getInputStream();
|
|
||||||
PutObjectArgs args = PutObjectArgs.builder()
|
|
||||||
.bucket(minioConfig.getBucketName())
|
|
||||||
.object(fileName)
|
|
||||||
.stream(inputStream, file.getSize(), -1)
|
|
||||||
.contentType(file.getContentType())
|
|
||||||
.build();
|
|
||||||
client.putObject(args);
|
|
||||||
IoUtils.closeQuietly(inputStream);
|
|
||||||
return SysFile.builder().url(minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName).name(fileName).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Minio文件上传接口
|
|
||||||
*
|
|
||||||
* @param files 上传的文件
|
|
||||||
* @return 访问地址
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public List<SysFile> uploadFiles(MultipartFile[] files) throws Exception {
|
|
||||||
List<SysFile> sysFiles = new ArrayList<>();
|
|
||||||
for (MultipartFile file : files) {
|
|
||||||
SysFile sysFile = uploadFile(file);
|
|
||||||
sysFiles.add(sysFile);
|
|
||||||
}
|
|
||||||
return sysFiles;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Minio文件下载接口,待测试
|
|
||||||
*
|
|
||||||
* @param response 响应对象
|
|
||||||
* @param urlStr 文件URL地址
|
|
||||||
* @return 是否成功
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void downloadFile(HttpServletResponse response, String urlStr) throws Exception
|
|
||||||
{
|
|
||||||
String fileName = com.bonus.file.utils.FileUtils.setResponseHeaderByUrl(response, urlStr);
|
|
||||||
if (fileName == null){
|
|
||||||
throw new Exception("Can't get fileName" + urlStr);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
// 获取文件的输入流
|
|
||||||
GetObjectArgs args = GetObjectArgs.builder()
|
|
||||||
.bucket(minioConfig.getBucketName())
|
|
||||||
.object(fileName)
|
|
||||||
.build();
|
|
||||||
InputStream inputStream = client.getObject(args);
|
|
||||||
|
|
||||||
// 将文件流写入响应
|
|
||||||
IOUtils.copy(inputStream, response.getOutputStream());
|
|
||||||
response.flushBuffer();
|
|
||||||
inputStream.close();
|
|
||||||
} catch (MinioException e) {
|
|
||||||
throw new Exception("Error occurred while downloading file from Minio" + urlStr, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Minio文件删除接口,待测试
|
|
||||||
*
|
|
||||||
* @param urlStr 文件URL地址
|
|
||||||
* @return 是否删除成功
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void deleteFile(String urlStr) throws Exception
|
|
||||||
{
|
|
||||||
String fileName = urlStr.substring(urlStr.lastIndexOf("/") + 1);
|
|
||||||
try {
|
|
||||||
// 删除文件
|
|
||||||
RemoveObjectArgs args = RemoveObjectArgs.builder()
|
|
||||||
.bucket(minioConfig.getBucketName())
|
|
||||||
.object(fileName)
|
|
||||||
.build();
|
|
||||||
client.removeObject(args);
|
|
||||||
} catch (MinioException e) {
|
|
||||||
throw new Exception("Error occurred while deleting file from Minio", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue