Skip to content

Commit

Permalink
perf($Starter): reduce operating Minio directly
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Sep 15, 2021
1 parent 6dc91ae commit 6473942
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 25 deletions.
4 changes: 4 additions & 0 deletions auth-center/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>

<!-- ORM Library -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jmsoftware.maf.authcenter.configuration;

import com.jmsoftware.maf.authcenter.remoteapi.osscenter.OssCenterRemoteApi;
import com.jmsoftware.maf.springcloudstarter.poi.OssUploader;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

/**
* Description: OssConfiguration, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 9/15/2021 11:51 AM
**/
@Slf4j
@Configuration
@RequiredArgsConstructor
public class OssConfiguration {
private final OssCenterRemoteApi ossCenterRemoteApi;

@Bean
public OssUploader ossUploader() {
return (name, inputStream) -> {
MultipartFile file = new MockMultipartFile(name, name, null, inputStream);
val response = this.ossCenterRemoteApi.uploadSingleResource(file);
log.info("Called {} to upload file. {}", OssCenterRemoteApi.SERVICE_NAME, response);
return String.format("%s/%s", response.getData().getBucket(), response.getData().getObject());
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.jmsoftware.maf.authcenter.remoteapi.osscenter;

import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import com.jmsoftware.maf.common.domain.osscenter.write.ObjectResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

/**
* Description: OssCenterRemoteApi, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 9/15/2021 11:10 AM
* @see <a href='https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/'>Spring Cloud OpenFeign</a>
**/
@FeignClient(value = OssCenterRemoteApi.SERVICE_NAME)
public interface OssCenterRemoteApi {
String SERVICE_NAME = "oss-center";

/**
* Upload single resource response body bean.
*
* @param multipartFile the multipart file
* @return the response body bean
*/
@PostMapping(value = "/upload/single", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResponseBodyBean<ObjectResponse> uploadSingleResource(@RequestPart("file") MultipartFile multipartFile);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.jmsoftware.maf.authcenter.remoteapi;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.jmsoftware.maf.authcenter.role.entity.RoleExcelBean;
import com.jmsoftware.maf.authcenter.role.service.RoleService;
import com.jmsoftware.maf.springcloudstarter.controller.AbstractExcelDataController;
import com.jmsoftware.maf.springcloudstarter.poi.AbstractExcelDataController;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.jmsoftware.maf.common.domain.osscenter;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jmsoftware.maf.osscenter.write.entity;
package com.jmsoftware.maf.common.domain.osscenter.write;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.jmsoftware.maf.osscenter.write.controller;

import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import com.jmsoftware.maf.common.domain.osscenter.write.ObjectResponse;
import com.jmsoftware.maf.osscenter.write.entity.MergeResourceChunkPayload;
import com.jmsoftware.maf.osscenter.write.entity.ObjectResponse;
import com.jmsoftware.maf.osscenter.write.entity.UploadResourceChunkPayload;
import com.jmsoftware.maf.osscenter.write.service.WriteResourceService;
import io.swagger.annotations.Api;
Expand Down Expand Up @@ -33,15 +33,15 @@ public class WriteResourceController {

@PostMapping("/upload/single")
@ApiOperation(value = "Upload single resource", notes = "Upload single resource")
public ResponseBodyBean<ObjectResponse> uploadSingleResource(@RequestParam("file") MultipartFile multipartFile) {
public ResponseBodyBean<ObjectResponse> uploadSingleResource(@RequestPart("file") MultipartFile multipartFile) {
return ResponseBodyBean.ofSuccess(this.writeResourceService.uploadSingleResource(multipartFile),
this.messageSource.getMessage("uploaded", null,
LocaleContextHolder.getLocale()));
}

@PostMapping("/upload/chunk")
@ApiOperation(value = "Upload chunk of resource", notes = "Upload chunk of resource")
public ResponseBodyBean<ObjectResponse> uploadResourceChunk(@RequestParam("file") MultipartFile multipartFile,
public ResponseBodyBean<ObjectResponse> uploadResourceChunk(@RequestPart("file") MultipartFile multipartFile,
@Valid UploadResourceChunkPayload payload) {
return ResponseBodyBean.ofSuccess(this.writeResourceService.uploadResourceChunk(multipartFile, payload));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jmsoftware.maf.osscenter.write.service;

import com.jmsoftware.maf.common.domain.osscenter.write.ObjectResponse;
import com.jmsoftware.maf.osscenter.write.entity.MergeResourceChunkPayload;
import com.jmsoftware.maf.osscenter.write.entity.ObjectResponse;
import com.jmsoftware.maf.osscenter.write.entity.UploadResourceChunkPayload;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.jmsoftware.maf.common.domain.osscenter.write.ObjectResponse;
import com.jmsoftware.maf.osscenter.read.service.ReadResourceService;
import com.jmsoftware.maf.osscenter.write.entity.MergeResourceChunkPayload;
import com.jmsoftware.maf.osscenter.write.entity.ObjectResponse;
import com.jmsoftware.maf.osscenter.write.entity.UploadResourceChunkPayload;
import com.jmsoftware.maf.osscenter.write.service.WriteResourceService;
import com.jmsoftware.maf.springcloudstarter.minio.MinioHelper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jmsoftware.maf.springcloudstarter.controller;
package com.jmsoftware.maf.springcloudstarter.poi;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
Expand All @@ -11,7 +11,6 @@
import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import com.jmsoftware.maf.springcloudstarter.annotation.ExcelColumn;
import com.jmsoftware.maf.springcloudstarter.configuration.ExcelImportConfiguration;
import com.jmsoftware.maf.springcloudstarter.minio.MinioHelper;
import com.jmsoftware.maf.springcloudstarter.util.CaseConversionUtil;
import io.swagger.annotations.ApiOperation;
import lombok.Cleanup;
Expand All @@ -20,16 +19,17 @@
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.unit.DataSize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import javax.annotation.Resource;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -115,10 +115,10 @@ public abstract class AbstractExcelDataController<T> {
*/
protected final Map<String, String> exportingFieldAliasMap = Maps.newHashMap();

@Resource
@Autowired
protected ExcelImportConfiguration excelImportConfiguration;
@Resource
protected MinioHelper minioHelper;
@Autowired
protected OssUploader ossUploader;
/**
* Deny all data when data validation fails. Default value is true.
*/
Expand Down Expand Up @@ -226,9 +226,8 @@ protected void beforeDatabaseOperation(List<T> beanList) {
* Execute database operation.
*
* @param beanList the bean list that can be used for DB operations
* @throws Exception the exception
*/
protected abstract void executeDatabaseOperation(List<T> beanList) throws Exception;
protected abstract void executeDatabaseOperation(List<T> beanList);

/**
* After execute. Delete file.
Expand Down Expand Up @@ -337,8 +336,8 @@ private void exeDatabaseOperation() {
}

private void bindData() {
this.setReturnMessageList("Starting - Validate and bind data…");
try {
this.setReturnMessageList("Starting - Validate and bind data…");
this.bindData(this.workbook.get());
this.setReturnMessageList("Finished - Validate and bind data");
} catch (Exception e) {
Expand Down Expand Up @@ -407,13 +406,13 @@ private Workbook readFile(@NonNull MultipartFile multipartFile) throws IOExcepti

@SneakyThrows
private void uploadWorkbook() {
@Cleanup val outputStream = new ByteArrayOutputStream();
// FIXME: might cause OOM
@Cleanup val outputStream = new ByteArrayOutputStream((int) DataSize.ofBytes(512).toBytes());
this.workbook.get().write(outputStream);
final var bufferedInputStream = new BufferedInputStream(new ByteArrayInputStream(outputStream.toByteArray()));
this.minioHelper.makeBucket("temp");
this.minioHelper.putObject("temp", this.fileName.get(), bufferedInputStream,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
this.excelFilePath.set("temp/temp.xlsx");
@Cleanup val inputStream = new BufferedInputStream(new ByteArrayInputStream(outputStream.toByteArray()));
val filePath = this.ossUploader.upload(this.getTemplateFileName(), inputStream);
log.info("Uploaded excel with exception message. filePath: {}", filePath);
this.excelFilePath.set(filePath);
}

/**
Expand All @@ -426,10 +425,9 @@ private void uploadWorkbook() {
* </ul>
*
* @param workbook the workbook
* @throws Exception the exception
*/
@SuppressWarnings("RedundantThrows")
private void bindData(Workbook workbook) throws Exception {
private void bindData(Workbook workbook) {
for (int index = 0; index < workbook.getNumberOfSheets(); index++) {
val sheet = workbook.getSheetAt(index);
val excelReader = new ExcelReader(workbook, index);
Expand Down Expand Up @@ -461,7 +459,7 @@ private void validateBeanByRow(int sheetIndex, int beanIndex) {
/**
* Sets deny all. When exception occurred, deny all data or not
*
* @param denyAll the deny all
* @param denyAll true is deny all or vice versa
*/
public void setDenyAll(Boolean denyAll) {
this.denyAll = denyAll;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.jmsoftware.maf.springcloudstarter.poi;

import java.io.IOException;
import java.io.InputStream;

/**
* Description: OssUploader, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 9/15/2021 11:40 AM
**/
@FunctionalInterface
public interface OssUploader {
/**
* Upload.
*
* @param name the name
* @param inputStream the input stream
* @return the string
* @throws IOException the io exception
*/
String upload(String name, InputStream inputStream) throws IOException;
}

0 comments on commit 6473942

Please sign in to comment.