|
@@ -1,18 +1,34 @@
|
|
package com.zhentao.moment.service.impl;
|
|
package com.zhentao.moment.service.impl;
|
|
|
|
|
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
|
+import com.aliyun.oss.model.ObjectMetadata;
|
|
|
|
+import com.aliyun.oss.model.PutObjectRequest;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.zhentao.moment.config.OssConfig;
|
|
|
|
+import com.zhentao.moment.config.OssProperties;
|
|
import com.zhentao.moment.domain.UserMoments;
|
|
import com.zhentao.moment.domain.UserMoments;
|
|
import com.zhentao.moment.dto.MonmentDto;
|
|
import com.zhentao.moment.dto.MonmentDto;
|
|
import com.zhentao.moment.enums.ContentTypeEnum;
|
|
import com.zhentao.moment.enums.ContentTypeEnum;
|
|
import com.zhentao.moment.service.UserMomentsService;
|
|
import com.zhentao.moment.service.UserMomentsService;
|
|
import com.zhentao.moment.mapper.UserMomentsMapper;
|
|
import com.zhentao.moment.mapper.UserMomentsMapper;
|
|
import com.zhentao.tool.TokenUtils;
|
|
import com.zhentao.tool.TokenUtils;
|
|
|
|
+import com.zhentao.user.domain.UserLogin;
|
|
|
|
+import com.zhentao.user.mapper.UserLoginMapper;
|
|
import com.zhentao.utils.SensitiveWordFilter;
|
|
import com.zhentao.utils.SensitiveWordFilter;
|
|
import com.zhentao.utils.SnowflakeUtil;
|
|
import com.zhentao.utils.SnowflakeUtil;
|
|
import com.zhentao.vo.Result;
|
|
import com.zhentao.vo.Result;
|
|
import io.jsonwebtoken.Claims;
|
|
import io.jsonwebtoken.Claims;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author 86159
|
|
* @author 86159
|
|
@@ -24,6 +40,13 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
implements UserMomentsService{
|
|
implements UserMomentsService{
|
|
@Autowired
|
|
@Autowired
|
|
private UserMomentsMapper userMomentsMapper;
|
|
private UserMomentsMapper userMomentsMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+ private UserLoginMapper userLoginMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+
|
|
|
|
+ private OSSClient ossClient;
|
|
|
|
+ @Autowired
|
|
|
|
+ private OssProperties ossProperties;
|
|
@Override
|
|
@Override
|
|
public Result sendMonment(String token,MonmentDto monmentDto) {
|
|
public Result sendMonment(String token,MonmentDto monmentDto) {
|
|
if (monmentDto.getContent()==null){
|
|
if (monmentDto.getContent()==null){
|
|
@@ -37,56 +60,188 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
userMoments.setMomentId(SnowflakeUtil.nextId());
|
|
userMoments.setMomentId(SnowflakeUtil.nextId());
|
|
String userId = TokenUtils.getUserIdFromToken(token);
|
|
String userId = TokenUtils.getUserIdFromToken(token);
|
|
userMoments.setUserId(Long.valueOf(userId));
|
|
userMoments.setUserId(Long.valueOf(userId));
|
|
|
|
+ String filter = SensitiveWordFilter.filter(monmentDto.getContent());
|
|
|
|
+ userMoments.setContent(filter);
|
|
Integer typeCode = monmentDto.getContentType();
|
|
Integer typeCode = monmentDto.getContentType();
|
|
ContentTypeEnum type = ContentTypeEnum.getByCode(typeCode);
|
|
ContentTypeEnum type = ContentTypeEnum.getByCode(typeCode);
|
|
|
|
+ if (monmentDto.getFiles()!=null && !monmentDto.getFiles().isEmpty()){
|
|
|
|
+ if (type!=ContentTypeEnum.IMAGE){
|
|
|
|
+ return Result.error(400,"只有图片类型支持上传多张");
|
|
|
|
+ }
|
|
switch (type){
|
|
switch (type){
|
|
- case TEXT:
|
|
|
|
- String filter = SensitiveWordFilter.filter(monmentDto.getContent());
|
|
|
|
- userMoments.setContent(filter);
|
|
|
|
- break;
|
|
|
|
case IMAGE:
|
|
case IMAGE:
|
|
- validateImageContent(monmentDto.getContent());
|
|
|
|
-// userMoments.setContentType(ContentTypeEnum.IMAGE.getCode());
|
|
|
|
|
|
+ for (MultipartFile file : monmentDto.getFiles()){
|
|
|
|
+ if (file.isEmpty()){
|
|
|
|
+ return Result.error(400,"图片不能为空");
|
|
|
|
+ }
|
|
|
|
+ validateImageContent(file);
|
|
|
|
+ }
|
|
|
|
+ List<String> imageUrls = new ArrayList<>();
|
|
|
|
+ List<String> imageNames = new ArrayList<>();
|
|
|
|
+ Long totalSize = 0L;
|
|
|
|
+ String imageDir = ossProperties.getUpload().getImage().getDir();
|
|
|
|
+ for (MultipartFile file : monmentDto.getFiles()){
|
|
|
|
+ String imageFileName = userId + "/" + System.currentTimeMillis() +
|
|
|
|
+ getFileExtension(file.getOriginalFilename());
|
|
|
|
+ String ossFilePath = imageDir + imageFileName;
|
|
|
|
+// 上传OSS
|
|
|
|
+ String url = uploadToOss(file, ossFilePath);
|
|
|
|
+ imageUrls.add(url);
|
|
|
|
+ imageNames.add(file.getOriginalFilename());
|
|
|
|
+ totalSize+=file.getSize();
|
|
|
|
+ }
|
|
|
|
+ String urlJson = "[" + imageUrls.stream()
|
|
|
|
+ .map(url -> "\"" + url.replace("\"", "\\\"") + "\"") // 确保URL中的双引号被转义
|
|
|
|
+ .collect(Collectors.joining(",")) + "]";
|
|
|
|
+ userMoments.setUrl(urlJson);
|
|
|
|
+ String nameJson = "[" + imageNames.stream()
|
|
|
|
+ .map(name -> "\"" + name.replace("\"", "\\\"") + "\"")
|
|
|
|
+ .collect(Collectors.joining(",")) + "]";
|
|
|
|
+ userMoments.setFilename(nameJson);
|
|
|
|
+ userMoments.setFilesize(totalSize);
|
|
|
|
+
|
|
break;
|
|
break;
|
|
case VIDEO:
|
|
case VIDEO:
|
|
- validateVideoContent(monmentDto.getContent());
|
|
|
|
-// userMoments.setContentType(ContentTypeEnum.VIDEO.getCode());
|
|
|
|
|
|
+ MultipartFile videoFile = monmentDto.getFiles().get(0);
|
|
|
|
+ if (videoFile.isEmpty()){
|
|
|
|
+ return Result.error(400,"视频不能为空");
|
|
|
|
+ }
|
|
|
|
+ validateVideoContent(videoFile);
|
|
|
|
+ String videoDir = ossProperties.getUpload().getVideo().getDir();
|
|
|
|
+ String videoFileName = userId + "/" + System.currentTimeMillis() +
|
|
|
|
+ getFileExtension(videoFile.getOriginalFilename());
|
|
|
|
+ String ossFilePath = videoDir + videoFileName;
|
|
|
|
+ String url = uploadToOss(videoFile, ossFilePath);
|
|
|
|
+
|
|
|
|
+ userMoments.setUrl(url);
|
|
|
|
+
|
|
|
|
+ userMoments.setFilename(videoFile.getOriginalFilename());
|
|
|
|
+ userMoments.setFilesize(videoFile.getSize());
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
- return Result.error(400,"内容类型错误");
|
|
|
|
|
|
+ return Result.error(400,"内容类型错误");
|
|
|
|
+ }
|
|
|
|
+// 上传文件到OSS
|
|
|
|
+
|
|
|
|
+ }else if (type!=ContentTypeEnum.TEXT){
|
|
|
|
+ return Result.ERR(null,"类型上传不正确");
|
|
}
|
|
}
|
|
//// userMoments.setContentType();
|
|
//// userMoments.setContentType();
|
|
// userMoments.setContent(monmentDto.getContent());
|
|
// userMoments.setContent(monmentDto.getContent());
|
|
|
|
+ userMoments.setVisibility(monmentDto.getVisible());
|
|
userMomentsMapper.insert(userMoments);
|
|
userMomentsMapper.insert(userMoments);
|
|
|
|
+ Map<String,Object> map=new HashMap<>();
|
|
|
|
+ map.put("monmentId",userMoments.getMomentId());
|
|
|
|
+ map.put("ossFilePath",userMoments.getUrl());
|
|
|
|
+ return Result.OK(map,"发送成功");
|
|
}catch (Exception e){
|
|
}catch (Exception e){
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
|
+ return Result.error(500,"发送失败");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+// 删除朋友圈
|
|
|
|
+ @Override
|
|
|
|
+ public Result deleteMonment(MonmentDto monmentDto) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+//查询个人信息
|
|
|
|
+ @Override
|
|
|
|
+ public Result userinfo(String token) {
|
|
|
|
+ String userId = TokenUtils.getUserIdFromToken(token);
|
|
|
|
+ if (userId==null){
|
|
|
|
+ return Result.error(400,"没有userid");
|
|
}
|
|
}
|
|
- return Result.OK(null,"发送成功");
|
|
|
|
|
|
+ UserLogin userLogin = userLoginMapper.selectById(userId);
|
|
|
|
+ return Result.OK(userLogin,"查询成功");
|
|
}
|
|
}
|
|
|
|
|
|
-// @Override
|
|
|
|
-// public Result deleteMonment(MonmentDto monmentDto) {
|
|
|
|
-//
|
|
|
|
-//
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
- // 图片内容校验
|
|
|
|
- private void validateImageContent(String content) {
|
|
|
|
- // content 存图片 URL 列表,用逗号分隔
|
|
|
|
- String[] imageUrls = content.split(",");
|
|
|
|
- for (String url : imageUrls) {
|
|
|
|
- if (!url.matches("^https?://.*\\.(png|jpg|jpeg)$")) {
|
|
|
|
- throw new IllegalArgumentException("图片 URL 格式错误: " + url);
|
|
|
|
- }
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public List<UserMoments> getMyMonment(Long userId) {
|
|
|
|
+ QueryWrapper<UserMoments> queryWrapper=new QueryWrapper<>();
|
|
|
|
+ queryWrapper.eq("user_id",userId);
|
|
|
|
+ List<UserMoments> userMoments = userMomentsMapper.selectList(queryWrapper);
|
|
|
|
+ return userMoments;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // **
|
|
|
|
+// * 上传文件到OSS
|
|
|
|
+// */
|
|
|
|
+ private String uploadToOss(MultipartFile file, String ossFilePath) throws IOException {
|
|
|
|
+ String bucketName = ossProperties.getBucketName();
|
|
|
|
+
|
|
|
|
+ // 创建PutObject请求
|
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossFilePath,
|
|
|
|
+ file.getInputStream(), null);
|
|
|
|
+
|
|
|
|
+ // 设置对象元信息
|
|
|
|
+ ObjectMetadata metadata = new ObjectMetadata();
|
|
|
|
+ metadata.setContentType(file.getContentType());
|
|
|
|
+ metadata.setContentLength(file.getSize());
|
|
|
|
+ putObjectRequest.setMetadata(metadata);
|
|
|
|
+
|
|
|
|
+ // 上传文件
|
|
|
|
+ ossClient.putObject(putObjectRequest);
|
|
|
|
+
|
|
|
|
+ // 构建访问URL
|
|
|
|
+ String endpoint = ossProperties.getEndpoint();
|
|
|
|
+ String cleanEndpoint = endpoint.replaceAll("^https://", "");
|
|
|
|
+ String domain = "https://" + bucketName + "." + cleanEndpoint;
|
|
|
|
+ return domain + "/" + ossFilePath;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取文件扩展名
|
|
|
|
+ */
|
|
|
|
+ private String getFileExtension(String fileName) {
|
|
|
|
+ if (fileName == null || !fileName.contains(".")) {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ return fileName.substring(fileName.lastIndexOf("."));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 验证图片内容
|
|
|
|
+ */
|
|
|
|
+ private void validateImageContent(MultipartFile file) throws Exception {
|
|
|
|
+ if (file == null || file.isEmpty()) {
|
|
|
|
+ throw new Exception("图片不能为空");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ String contentType = file.getContentType();
|
|
|
|
+ if (!contentType.startsWith("image/")) {
|
|
|
|
+ throw new Exception("请上传图片格式文件");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 验证图片大小(例如限制为10MB)
|
|
|
|
+ if (file.getSize() > ossProperties.getUpload().getImage().getMaxSize()) {
|
|
|
|
+ throw new Exception("图片大小不能超过" +
|
|
|
|
+ (ossProperties.getUpload().getImage().getMaxSize() / (1024 * 1024)) + "MB");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 可以添加更多图片验证逻辑,如图片尺寸、格式等
|
|
}
|
|
}
|
|
|
|
|
|
- // 视频内容校验
|
|
|
|
- private void validateVideoContent(String content) {
|
|
|
|
- // content 存视频 URL
|
|
|
|
- if (!content.matches("^https?://.*\\.(mp4|avi)$")) {
|
|
|
|
- throw new IllegalArgumentException("视频 URL 格式错误: " + content);
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 验证视频内容
|
|
|
|
+ */
|
|
|
|
+ private void validateVideoContent(MultipartFile file) throws Exception {
|
|
|
|
+ if (file == null || file.isEmpty()) {
|
|
|
|
+ throw new Exception("视频不能为空");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ String contentType = file.getContentType();
|
|
|
|
+ if (!contentType.startsWith("video/")) {
|
|
|
|
+ throw new Exception("请上传视频格式文件");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 验证视频大小(例如限制为50MB)
|
|
|
|
+ if (file.getSize() > ossProperties.getUpload().getVideo().getMaxSize()) {
|
|
|
|
+ throw new Exception("视频大小不能超过" +
|
|
|
|
+ (ossProperties.getUpload().getVideo().getMaxSize() / (1024 * 1024)) + "MB");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 可以添加更多视频验证逻辑,如视频时长、分辨率等
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|