1.async upload feedback image

main
Gary 7 months ago
parent 2faa608070
commit d80fc65764

@ -7,6 +7,7 @@ import java.util.stream.Collectors;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.luoo.user.dao.UserMessageDao;
import com.luoo.user.dto.FeedbackImage;
import com.luoo.user.dto.TotalCommentVo;
import com.luoo.user.dto.request.LoginReq;
import com.luoo.user.dto.request.LogoffReq;
@ -60,7 +61,6 @@ import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.SneakyThrows;
import util.DateUtil;
import util.IdWorker;
import util.ScaleFilter;
@ -283,25 +283,27 @@ public class MyController extends BaseController {
feedback.setUserId(userLoginDto.getUserId());
feedback.setNickName(EmojiConverterUtil.encode(userLoginDto.getNickName()));
feedback.setContent(EmojiConverterUtil.encode(content));
feedback.setImages(uploadImages(feedback, files));
feedbackService.send(feedback);
feedbackService.send(feedback, getInputStream(files));
return Result.success();
}
private String uploadImages(Feedback feedback, List<MultipartFile> files) {
if (null == files || files.isEmpty()) {
return null;
private List<FeedbackImage> getInputStream(List<MultipartFile> files) {
if(null==files) {
return Collections.emptyList();
}
return files.parallelStream().map(f -> uploadImages(feedback, f)).collect(Collectors.joining(","));
return files.stream().map(this::getFeedbackImage).collect(Collectors.toList());
}
@SneakyThrows
private String uploadImages(Feedback feedback, MultipartFile file) {
String imageName = feedback.getUserId() + "_" + feedback.getFeedbackId() + "_" + idWorker.nextId()
+ StringTools.getFileSuffix(file.getOriginalFilename());
String filePath = Constants.USER_FEEDBACK_IMAGE_DIRECTORY + imageName;
s3Service.upload("indie", filePath, file);
return imageName;
private FeedbackImage getFeedbackImage(MultipartFile multipartFile) {
FeedbackImage feedbackImage=new FeedbackImage();
try {
feedbackImage.setInputStream(multipartFile.getInputStream());
}catch(Exception e) {
e.printStackTrace();
}
feedbackImage.setContentType(multipartFile.getContentType());
feedbackImage.setOriginalFilename(multipartFile.getOriginalFilename());
feedbackImage.setSize(multipartFile.getSize());
return feedbackImage;
}
private Result<PageResult<UserRespDTO>> getCollectedUserInfo(String userId, Integer pageNum, Integer pageSize,

@ -0,0 +1,13 @@
package com.luoo.user.dto;
import java.io.InputStream;
import lombok.Data;
@Data
public class FeedbackImage {
private InputStream inputStream;
private String originalFilename;
private String contentType;
private long size;
}

@ -4,6 +4,8 @@ import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
@ -15,22 +17,32 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;
import com.luoo.user.dao.FeedbackDao;
import com.luoo.user.dto.FeedbackImage;
import com.luoo.user.pojo.Feedback;
import com.luoo.user.util.EmojiConverterUtil;
import constants.Constants;
import lombok.SneakyThrows;
import util.IdWorker;
import util.StringTools;
@Service
public class FeedbackService {
private static final String[] EMPTY_IMAGE_ARRAY = new String[] {};
private static final String FEI_SHU_URL = "https://open.feishu.cn/open-apis/bot/v2/hook/84316603-355a-4dea-bb86-65f4b8919431";
@Autowired
private S3Service s3Service;
@Autowired
private IdWorker idWorker;
@Autowired
private FeedbackDao feedbackDao;
@Autowired
private RestTemplate restTemplate;
@Async
public void send(Feedback feedback) {
public void send(Feedback feedback,List<FeedbackImage> files) {
feedback.setImages(uploadImages(feedback, files));
feedbackDao.save(feedback);
seedToFeishu(feedback);
}
@ -64,4 +76,20 @@ public class FeedbackService {
}
restTemplate.postForEntity(FEI_SHU_URL, requestEntity, Map.class);
}
private String uploadImages(Feedback feedback, List<FeedbackImage> files) {
if (null == files || files.isEmpty()) {
return null;
}
return files.parallelStream().map(f -> uploadImages(feedback, f)).collect(Collectors.joining(","));
}
@SneakyThrows
private String uploadImages(Feedback feedback, FeedbackImage file) {
String imageName = feedback.getUserId() + "_" + feedback.getFeedbackId() + "_" + idWorker.nextId()
+ StringTools.getFileSuffix(file.getOriginalFilename());
String filePath = Constants.USER_FEEDBACK_IMAGE_DIRECTORY + imageName;
s3Service.upload("indie", filePath, file);
return imageName;
}
}

@ -3,6 +3,9 @@ package com.luoo.user.service;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.luoo.user.dto.FeedbackImage;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.http.SdkHttpResponse;
import software.amazon.awssdk.services.s3.S3Client;
@ -132,4 +135,24 @@ public class S3Service {
}
}
public int upload(String bucket, String key, FeedbackImage file) {
try {
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucket)
.contentType(file.getContentType())
.key(key)
.build();
RequestBody requestBody = RequestBody.fromInputStream(file.getInputStream(), file.getSize());
PutObjectResponse putObjectResponse = s3Client.putObject(putObjectRequest, requestBody);
SdkHttpResponse sdkHttpResponse = putObjectResponse.sdkHttpResponse();
if (!sdkHttpResponse.isSuccessful()) {
return -1;
} else {
return 1;
}
} catch (Exception e) {
return -1;
}
}
}

Loading…
Cancel
Save