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.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.luoo.user.dao.UserMessageDao; import com.luoo.user.dao.UserMessageDao;
import com.luoo.user.dto.FeedbackImage;
import com.luoo.user.dto.TotalCommentVo; import com.luoo.user.dto.TotalCommentVo;
import com.luoo.user.dto.request.LoginReq; import com.luoo.user.dto.request.LoginReq;
import com.luoo.user.dto.request.LogoffReq; 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.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
import lombok.SneakyThrows;
import util.DateUtil; import util.DateUtil;
import util.IdWorker; import util.IdWorker;
import util.ScaleFilter; import util.ScaleFilter;
@ -283,25 +283,27 @@ public class MyController extends BaseController {
feedback.setUserId(userLoginDto.getUserId()); feedback.setUserId(userLoginDto.getUserId());
feedback.setNickName(EmojiConverterUtil.encode(userLoginDto.getNickName())); feedback.setNickName(EmojiConverterUtil.encode(userLoginDto.getNickName()));
feedback.setContent(EmojiConverterUtil.encode(content)); feedback.setContent(EmojiConverterUtil.encode(content));
feedback.setImages(uploadImages(feedback, files)); feedbackService.send(feedback, getInputStream(files));
feedbackService.send(feedback);
return Result.success(); return Result.success();
} }
private String uploadImages(Feedback feedback, List<MultipartFile> files) { private List<FeedbackImage> getInputStream(List<MultipartFile> files) {
if (null == files || files.isEmpty()) { if(null==files) {
return null; return Collections.emptyList();
} }
return files.parallelStream().map(f -> uploadImages(feedback, f)).collect(Collectors.joining(",")); return files.stream().map(this::getFeedbackImage).collect(Collectors.toList());
} }
private FeedbackImage getFeedbackImage(MultipartFile multipartFile) {
@SneakyThrows FeedbackImage feedbackImage=new FeedbackImage();
private String uploadImages(Feedback feedback, MultipartFile file) { try {
String imageName = feedback.getUserId() + "_" + feedback.getFeedbackId() + "_" + idWorker.nextId() feedbackImage.setInputStream(multipartFile.getInputStream());
+ StringTools.getFileSuffix(file.getOriginalFilename()); }catch(Exception e) {
String filePath = Constants.USER_FEEDBACK_IMAGE_DIRECTORY + imageName; e.printStackTrace();
s3Service.upload("indie", filePath, file); }
return imageName; 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, 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.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -15,22 +17,32 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.luoo.user.dao.FeedbackDao; import com.luoo.user.dao.FeedbackDao;
import com.luoo.user.dto.FeedbackImage;
import com.luoo.user.pojo.Feedback; import com.luoo.user.pojo.Feedback;
import com.luoo.user.util.EmojiConverterUtil; import com.luoo.user.util.EmojiConverterUtil;
import constants.Constants; import constants.Constants;
import lombok.SneakyThrows;
import util.IdWorker;
import util.StringTools;
@Service @Service
public class FeedbackService { public class FeedbackService {
private static final String[] EMPTY_IMAGE_ARRAY = new String[] {}; 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"; 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 @Autowired
private FeedbackDao feedbackDao; private FeedbackDao feedbackDao;
@Autowired @Autowired
private RestTemplate restTemplate; private RestTemplate restTemplate;
@Async @Async
public void send(Feedback feedback) { public void send(Feedback feedback,List<FeedbackImage> files) {
feedback.setImages(uploadImages(feedback, files));
feedbackDao.save(feedback); feedbackDao.save(feedback);
seedToFeishu(feedback); seedToFeishu(feedback);
} }
@ -64,4 +76,20 @@ public class FeedbackService {
} }
restTemplate.postForEntity(FEI_SHU_URL, requestEntity, Map.class); 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.http.MediaType;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.luoo.user.dto.FeedbackImage;
import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.http.SdkHttpResponse; import software.amazon.awssdk.http.SdkHttpResponse;
import software.amazon.awssdk.services.s3.S3Client; 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