# Conflicts: # jianshui-admin/src/main/java/com/jianshui/api/controller/http/invoice/v1/InvoiceController.javabeta-prop-all
commit
b6b2acced5
@ -0,0 +1,77 @@ |
||||
package com.jianshui.invoice.task; |
||||
|
||||
import cn.hutool.core.codec.Base64Decoder; |
||||
import cn.hutool.core.util.StrUtil; |
||||
import com.github.pagehelper.PageHelper; |
||||
import com.github.pagehelper.PageInfo; |
||||
import com.jianshui.common.constant.Constants; |
||||
import com.jianshui.common.core.domain.AjaxResult; |
||||
import com.jianshui.common.exception.jianshui.JianshiSystemErrorException; |
||||
import com.jianshui.invoice.domain.InvoiceFile; |
||||
import com.jianshui.invoice.mapper.InvoiceFileMapper; |
||||
import com.jianshui.invoice.mapper.InvoiceMapper; |
||||
import com.jianshui.storage.factory.StorageFactory; |
||||
import com.jianshui.storage.service.StorageBaseAbstractService; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.collections4.CollectionUtils; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.util.List; |
||||
|
||||
@Slf4j |
||||
@Component("minioTask") |
||||
public class MinioTask { |
||||
|
||||
@Autowired |
||||
private InvoiceFileMapper invoiceFileMapper; |
||||
|
||||
@Autowired |
||||
private InvoiceMapper invoiceMapper; |
||||
|
||||
@Autowired |
||||
private StorageFactory storageFactory; |
||||
|
||||
public void dbInvoiceFileToMinioTask(Integer pageSize,Boolean isContinue) { |
||||
try { |
||||
StorageBaseAbstractService storageInstance = storageFactory.getStorageInstance(Constants.MINIO); |
||||
//是否继续
|
||||
boolean hasNext = true; |
||||
//页码
|
||||
int pageNum = Constants.INT_1; |
||||
while (hasNext) { |
||||
int count = Constants.INT_0; |
||||
PageHelper.startPage(pageNum, pageSize); |
||||
List<InvoiceFile> invoiceFileList = invoiceFileMapper.selectInvoiceFileListWithContentNotEmpty(); |
||||
if (CollectionUtils.isNotEmpty(invoiceFileList)){ |
||||
for (InvoiceFile invoiceFile : invoiceFileList) { |
||||
String fileContent = invoiceFile.getFileContent(); |
||||
if (StrUtil.isNotEmpty(fileContent)){ |
||||
try { |
||||
byte[] buffer = Base64Decoder.decode(fileContent); |
||||
String fileName = Constants.DB_SAVE.concat(invoiceFile.getFileName()).concat(Constants.POINT).concat(Constants.PDF); |
||||
AjaxResult upload = storageInstance.upload(buffer, fileName); |
||||
if (upload.isSuccess()){ |
||||
count += invoiceFileMapper.updateFileContent(invoiceFile.getId(),fileName); |
||||
} |
||||
} catch (Exception e) { |
||||
log.error("定时任务文件上传失败发票号码:{}",invoiceFile.getFphm(),e); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
if (!isContinue){ |
||||
break; |
||||
} |
||||
PageInfo<InvoiceFile> invoiceFilePageInfo = new PageInfo<>(invoiceFileList); |
||||
hasNext = invoiceFilePageInfo.isHasNextPage(); |
||||
pageNum = count >Constants.INT_0?pageNum:invoiceFilePageInfo.getNextPage(); |
||||
} |
||||
|
||||
} catch (JianshiSystemErrorException e) { |
||||
log.info("minio初始化失败",e); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,31 @@ |
||||
package com.jianshui.storage.minio.config; |
||||
|
||||
import com.jianshui.common.constant.Constants; |
||||
import io.minio.MinioClient; |
||||
import lombok.Data; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
@Data |
||||
@Configuration |
||||
@ConfigurationProperties(prefix = "minio") |
||||
@ConditionalOnProperty(name = Constants.INVOICE_FILE_SAVE_TYPE, havingValue = Constants.STRING_3) |
||||
public class MinioConfig { |
||||
|
||||
private String endpoint; |
||||
private String accessKey; |
||||
private String secretKey; |
||||
private String bucketName; |
||||
|
||||
@Bean |
||||
@ConditionalOnProperty(name = Constants.INVOICE_FILE_SAVE_TYPE, havingValue = Constants.STRING_3) |
||||
public MinioClient minioClient() { |
||||
return MinioClient.builder() |
||||
.endpoint(endpoint) |
||||
.credentials(accessKey, secretKey) |
||||
.build(); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,215 @@ |
||||
package com.jianshui.storage.minio.util; |
||||
|
||||
import cn.hutool.core.date.DateUtil; |
||||
import com.jianshui.storage.minio.config.MinioConfig; |
||||
import io.minio.*; |
||||
import io.minio.http.Method; |
||||
import io.minio.messages.Bucket; |
||||
import io.minio.messages.Item; |
||||
import lombok.SneakyThrows; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.util.FastByteArrayOutputStream; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
import javax.annotation.Resource; |
||||
import javax.servlet.ServletOutputStream; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.InputStream; |
||||
import java.util.ArrayList; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
import java.util.UUID; |
||||
|
||||
@Slf4j |
||||
@Component |
||||
@ConditionalOnBean({MinioClient.class}) |
||||
public class MinioUtil { |
||||
|
||||
@Autowired |
||||
private MinioConfig prop; |
||||
|
||||
@Resource |
||||
private MinioClient minioClient; |
||||
|
||||
/** |
||||
* 查看存储bucket是否存在 |
||||
* @return boolean |
||||
*/ |
||||
public Boolean bucketExists(String bucketName) { |
||||
Boolean found; |
||||
try { |
||||
found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); |
||||
} catch (Exception e) { |
||||
log.error("查看minio存储bucket异常",e); |
||||
return false; |
||||
} |
||||
return found; |
||||
} |
||||
|
||||
/** |
||||
* 创建存储bucket |
||||
* @return Boolean |
||||
*/ |
||||
public Boolean makeBucket(String bucketName) { |
||||
try { |
||||
minioClient.makeBucket(MakeBucketArgs.builder() |
||||
.bucket(bucketName) |
||||
.build()); |
||||
} catch (Exception e) { |
||||
log.error("minio创建bucket异常",e); |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
/** |
||||
* 删除存储bucket |
||||
* @return Boolean |
||||
*/ |
||||
public Boolean removeBucket(String bucketName) { |
||||
try { |
||||
minioClient.removeBucket(RemoveBucketArgs.builder() |
||||
.bucket(bucketName) |
||||
.build()); |
||||
} catch (Exception e) { |
||||
log.error("minio删除bucket异常",e); |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
/** |
||||
* 获取全部bucket |
||||
*/ |
||||
public List<Bucket> getAllBuckets() { |
||||
try { |
||||
List<Bucket> buckets = minioClient.listBuckets(); |
||||
return buckets; |
||||
} catch (Exception e) { |
||||
log.error("minio获取所有bucket异常",e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
|
||||
|
||||
/** |
||||
* 文件上传 |
||||
* |
||||
* @param inputStream 文件 |
||||
* @return Boolean |
||||
*/ |
||||
public boolean upload(InputStream inputStream,String fileName,long size,String contentType) { |
||||
|
||||
try { |
||||
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(prop.getBucketName()).build()); |
||||
if (!found){ |
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(prop.getBucketName()).build()); |
||||
} |
||||
PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(prop.getBucketName()).object(fileName) |
||||
.stream(inputStream, size,-1).contentType(contentType).build(); |
||||
//文件名称相同会覆盖
|
||||
minioClient.putObject(objectArgs); |
||||
} catch (Exception e) { |
||||
log.error("minio上传文件异常",e); |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* 预览图片 |
||||
* @param fileName |
||||
* @return |
||||
*/ |
||||
public String preview(String fileName){ |
||||
// 查看文件地址
|
||||
GetPresignedObjectUrlArgs build = new GetPresignedObjectUrlArgs().builder().bucket(prop.getBucketName()).object(fileName).method(Method.GET).build(); |
||||
try { |
||||
String url = minioClient.getPresignedObjectUrl(build); |
||||
return url; |
||||
} catch (Exception e) { |
||||
log.error("minio生成预览url异常",e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 文件下载 |
||||
* @param fileName 文件名称 |
||||
* @param res response |
||||
* @return Boolean |
||||
*/ |
||||
public void download(String fileName, HttpServletResponse res) { |
||||
GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(prop.getBucketName()) |
||||
.object(fileName).build(); |
||||
try (GetObjectResponse response = minioClient.getObject(objectArgs)){ |
||||
byte[] buf = new byte[1024]; |
||||
int len; |
||||
try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()){ |
||||
while ((len=response.read(buf))!=-1){ |
||||
os.write(buf,0,len); |
||||
} |
||||
os.flush(); |
||||
byte[] bytes = os.toByteArray(); |
||||
res.setCharacterEncoding("utf-8"); |
||||
// 设置强制下载不打开
|
||||
res.setContentType("application/pdf"); |
||||
res.addHeader("Content-Disposition", "inline;fileName=" + fileName); |
||||
try (ServletOutputStream stream = res.getOutputStream()){ |
||||
stream.write(bytes); |
||||
stream.flush(); |
||||
} |
||||
} |
||||
} catch (Exception e) { |
||||
log.error("minio下载文件异常",e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 查看文件对象 |
||||
* @return 存储bucket内文件对象信息 |
||||
*/ |
||||
public List<Item> listObjects() { |
||||
Iterable<Result<Item>> results = minioClient.listObjects( |
||||
ListObjectsArgs.builder().bucket(prop.getBucketName()).build()); |
||||
List<Item> items = new ArrayList<>(); |
||||
try { |
||||
for (Result<Item> result : results) { |
||||
items.add(result.get()); |
||||
} |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
return null; |
||||
} |
||||
return items; |
||||
} |
||||
|
||||
/** |
||||
* 删除 |
||||
* @param fileName |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public boolean remove(String fileName){ |
||||
try { |
||||
minioClient.removeObject( RemoveObjectArgs.builder().bucket(prop.getBucketName()).object(fileName).build()); |
||||
}catch (Exception e){ |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
@SneakyThrows |
||||
public String downloadUrl(String fileName) { |
||||
GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs.builder() |
||||
.method(Method.GET) |
||||
.bucket(prop.getBucketName()) |
||||
.object(fileName) |
||||
.expiry(60 * 60 * 24) |
||||
.build(); |
||||
return minioClient.getPresignedObjectUrl(getPresignedObjectUrlArgs); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,93 @@ |
||||
package com.jianshui.storage.service.impl; |
||||
|
||||
import com.jianshui.common.constant.Constants; |
||||
import com.jianshui.common.core.domain.AjaxResult; |
||||
import com.jianshui.common.utils.StringUtils; |
||||
import com.jianshui.storage.domain.StorageUrlDTO; |
||||
import com.jianshui.storage.service.StorageBaseAbstractService; |
||||
import com.qcloud.cos.COSClient; |
||||
import io.minio.*; |
||||
import io.minio.http.Method; |
||||
import lombok.Setter; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.io.ByteArrayInputStream; |
||||
|
||||
@Setter |
||||
@Slf4j |
||||
@Service("storage_minio") |
||||
@ConfigurationProperties(prefix = "minio") |
||||
@ConditionalOnProperty(name = Constants.INVOICE_FILE_SAVE_TYPE, havingValue = Constants.STRING_3) |
||||
public class MinioStorageService extends StorageBaseAbstractService { |
||||
|
||||
private String endpoint; |
||||
private String accessKey; |
||||
private String secretKey; |
||||
private String bucketName; |
||||
|
||||
@Override |
||||
public AjaxResult init() { |
||||
this.storageInstance = MinioClient.builder() |
||||
.endpoint(endpoint) |
||||
.credentials(accessKey, secretKey) |
||||
.build(); |
||||
return AjaxResult.success(); |
||||
} |
||||
|
||||
@Override |
||||
public AjaxResult getFile(StorageUrlDTO storageUrlDTO) { |
||||
// 查看文件地址
|
||||
String storagePath = storageUrlDTO.getStoragePath(); |
||||
GetPresignedObjectUrlArgs build = new GetPresignedObjectUrlArgs().builder().bucket(this.bucketName).object(storagePath).method(Method.GET).build(); |
||||
try { |
||||
String url = getClient().getPresignedObjectUrl(build); |
||||
return AjaxResult.success(Constants.STRING_SUCCESS, url); |
||||
} catch (Exception e) { |
||||
log.error("minio生成预览url异常",e); |
||||
} |
||||
return AjaxResult.error(); |
||||
} |
||||
|
||||
@Override |
||||
public AjaxResult upload(byte[] data, String storagePath) { |
||||
String contentType = Constants.STRING_BLANK; |
||||
try { |
||||
boolean found = getClient().bucketExists(BucketExistsArgs.builder().bucket(this.bucketName).build()); |
||||
if (!found) { |
||||
getClient().makeBucket(MakeBucketArgs.builder().bucket(this.bucketName).build()); |
||||
} |
||||
String suffix = storagePath.substring(storagePath.lastIndexOf(Constants.POINT)+Constants.INT_1); |
||||
if (StringUtils.equals(suffix.toUpperCase(), Constants.OFD_UPCASE)){ |
||||
contentType = Constants.APPLICATION_OFD; |
||||
}else if (StringUtils.equals(suffix.toUpperCase(), Constants.PDF_UPCASE)){ |
||||
contentType = Constants.APPLICATION_PDF; |
||||
}else { |
||||
contentType = Constants.APPLICATION_XML; |
||||
} |
||||
PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(this.bucketName).object(storagePath) |
||||
.stream(new ByteArrayInputStream(data), data.length, -1).contentType(contentType).build(); |
||||
//文件名称相同会覆盖
|
||||
getClient().putObject(objectArgs); |
||||
} catch (Exception e) { |
||||
log.error("minio上传文件异常", e); |
||||
AjaxResult.error(); |
||||
} |
||||
StorageUrlDTO storageUrlDTO = new StorageUrlDTO(); |
||||
storageUrlDTO.setStoragePath(storagePath); |
||||
storageUrlDTO.setServiceProvider(Constants.MINIO); |
||||
storageUrlDTO.setContentType(contentType); |
||||
return AjaxResult.success(storageUrlDTO); |
||||
} |
||||
|
||||
@Override |
||||
public AjaxResult upload(String filePath, String storagePath) { |
||||
return null; |
||||
} |
||||
|
||||
private MinioClient getClient() { |
||||
return (MinioClient) this.storageInstance; |
||||
} |
||||
} |
Loading…
Reference in new issue