forked from makefriends/dating
完成动态发布摸块(有OSS文件上传,没有审核)
This commit is contained in:
@@ -3,12 +3,15 @@ package com.bao.dating.controller;
|
|||||||
|
|
||||||
import com.bao.dating.common.Result;
|
import com.bao.dating.common.Result;
|
||||||
import com.bao.dating.common.ResultCode;
|
import com.bao.dating.common.ResultCode;
|
||||||
|
import com.bao.dating.pojo.dto.PostRequestDTO;
|
||||||
import com.bao.dating.pojo.entity.Post;
|
import com.bao.dating.pojo.entity.Post;
|
||||||
import com.bao.dating.service.PostService;
|
import com.bao.dating.service.PostService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/posts")
|
@RequestMapping("/posts")
|
||||||
public class PostController {
|
public class PostController {
|
||||||
@@ -16,10 +19,26 @@ public class PostController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private PostService postService;
|
private PostService postService;
|
||||||
|
|
||||||
@PostMapping
|
/**
|
||||||
public Result<Post> createPost(@RequestBody Post post, MultipartFile file) {
|
* 上传媒体文件接口
|
||||||
|
* @param files 媒体文件数组
|
||||||
|
* @return 上传后的文件URL列表
|
||||||
|
*/
|
||||||
|
@PostMapping(value = "/upload", consumes = "multipart/form-data")
|
||||||
|
public Result<List<String>> uploadMedia(@RequestParam("files") MultipartFile[] files) {
|
||||||
|
List<String> fileUrls = postService.uploadMedia(files);
|
||||||
|
return Result.success(ResultCode.SUCCESS_REVIEW, "文件上传成功", fileUrls);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发布动态接口 - JSON格式请求
|
||||||
|
* @param postRequestDTO 动态信息
|
||||||
|
* @return 发布的动态对象
|
||||||
|
*/
|
||||||
|
@PostMapping(consumes = "application/json")
|
||||||
|
public Result<Post> createPostJson(@RequestBody PostRequestDTO postRequestDTO) {
|
||||||
// 调用 Service 层处理发布动态业务逻辑
|
// 调用 Service 层处理发布动态业务逻辑
|
||||||
Post result = postService.createPost(post);
|
Post result = postService.createPost(postRequestDTO);
|
||||||
return Result.success(ResultCode.SUCCESS_REVIEW,"动态发布成功,等待审核。",result);
|
return Result.success(ResultCode.SUCCESS_REVIEW, "动态发布成功,等待审核。", result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
14
src/main/java/com/bao/dating/pojo/dto/PostRequestDTO.java
Normal file
14
src/main/java/com/bao/dating/pojo/dto/PostRequestDTO.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com.bao.dating.pojo.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class PostRequestDTO {
|
||||||
|
private Integer userId;
|
||||||
|
private String content;
|
||||||
|
private List<String> tags;
|
||||||
|
private List<String> mediaUrls;
|
||||||
|
}
|
||||||
@@ -33,4 +33,4 @@ public class Post {
|
|||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
private LocalDateTime updatedAt;
|
private LocalDateTime updatedAt;
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,23 @@
|
|||||||
package com.bao.dating.service;
|
package com.bao.dating.service;
|
||||||
|
|
||||||
|
import com.bao.dating.pojo.dto.PostRequestDTO;
|
||||||
import com.bao.dating.pojo.entity.Post;
|
import com.bao.dating.pojo.entity.Post;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface PostService {
|
public interface PostService {
|
||||||
Post createPost(Post post);
|
/**
|
||||||
}
|
* 上传媒体文件
|
||||||
|
* @param files 媒体文件数组
|
||||||
|
* @return 上传后的文件URL列表
|
||||||
|
*/
|
||||||
|
List<String> uploadMedia(MultipartFile[] files);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建动态
|
||||||
|
* @param postRequestDTO 动态请求数据传输对象
|
||||||
|
* @return 创建的动态对象
|
||||||
|
*/
|
||||||
|
Post createPost(PostRequestDTO postRequestDTO);
|
||||||
|
}
|
||||||
@@ -1,18 +1,26 @@
|
|||||||
package com.bao.dating.service.impl;
|
package com.bao.dating.service.impl;
|
||||||
|
|
||||||
import com.bao.dating.mapper.PostMapper;
|
import com.bao.dating.mapper.PostMapper;
|
||||||
|
import com.bao.dating.pojo.dto.PostRequestDTO;
|
||||||
import com.bao.dating.pojo.entity.Post;
|
import com.bao.dating.pojo.entity.Post;
|
||||||
import com.bao.dating.service.PostService;
|
import com.bao.dating.service.PostService;
|
||||||
import com.bao.dating.util.AliOssUtil;
|
import com.bao.dating.util.AliOssUtil;
|
||||||
import com.bao.dating.util.FileUtil;
|
import com.bao.dating.util.FileUtil;
|
||||||
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.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态服务实现类
|
||||||
|
*
|
||||||
|
* @author KilLze
|
||||||
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class PostServiceImpl implements PostService {
|
public class PostServiceImpl implements PostService {
|
||||||
|
|
||||||
@@ -22,47 +30,64 @@ public class PostServiceImpl implements PostService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private PostMapper postMapper;
|
private PostMapper postMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传媒体文件
|
||||||
|
* @param files 媒体文件数组
|
||||||
|
* @return 上传后的文件URL列表
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Post createPost(Post post) {
|
public List<String> uploadMedia(MultipartFile[] files) {
|
||||||
// 检查并确保userId不为空,如果为空则设置默认值
|
List<String> mediaUrls = new ArrayList<>();
|
||||||
if (post.getUserId() == null) {
|
if (files != null && files.length > 0) {
|
||||||
post.setUserId(1); // 设置默认用户ID为1
|
for (MultipartFile file : files) {
|
||||||
}
|
if (!file.isEmpty()) {
|
||||||
|
try {
|
||||||
// 处理图片和视频上传到 OSS
|
// 根据文件扩展名判断文件类型
|
||||||
List<String> mediaOssKeys = new ArrayList<>();
|
String fileType = FileUtil.getFileType(file.getOriginalFilename());
|
||||||
|
// 生成唯一文件名
|
||||||
// 判断媒体文件是否为空
|
String fileName = UUID.randomUUID().toString() + "." + FileUtil.getFileExtension(file.getOriginalFilename());
|
||||||
if (post.getMediaOssKeys() != null && !post.getMediaOssKeys().isEmpty()) {
|
// 获取文件字节数据
|
||||||
|
byte[] fileBytes = file.getBytes();
|
||||||
// 上传媒体文件并判断文件类型
|
// 根据文件类型处理
|
||||||
for (String mediaUrl : post.getMediaOssKeys()) {
|
String ossUrl = "";
|
||||||
// 根据文件扩展名判断文件类型
|
if ("image".equals(fileType) || "video".equals(fileType)) {
|
||||||
String fileType = FileUtil.getFileType(mediaUrl);
|
// 上传图片或视频
|
||||||
|
ossUrl = ossUtil.upload(fileBytes, fileName);
|
||||||
// 生成唯一文件名
|
}
|
||||||
String fileName = UUID.randomUUID().toString() + "." + FileUtil.getFileExtension(mediaUrl);
|
// 添加上传后的 URL
|
||||||
|
mediaUrls.add(ossUrl);
|
||||||
// 获取文件字节数据
|
} catch (IOException e) {
|
||||||
byte[] fileBytes = mediaUrl.getBytes();
|
e.printStackTrace();
|
||||||
|
}
|
||||||
// 根据文件类型处理
|
|
||||||
String ossUrl = "";
|
|
||||||
if ("image".equals(fileType)) {
|
|
||||||
// 上传图片
|
|
||||||
ossUrl = ossUtil.upload(fileBytes, fileName);
|
|
||||||
} else if ("video".equals(fileType)) {
|
|
||||||
// 上传视频
|
|
||||||
ossUrl = ossUtil.upload(fileBytes, fileName);
|
|
||||||
}
|
}
|
||||||
// 添加上传后的 URL
|
|
||||||
mediaOssKeys.add(ossUrl);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
post.setMediaOssKeys(mediaOssKeys);
|
return mediaUrls;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建动态
|
||||||
|
*
|
||||||
|
* @param postRequestDTO 动态请求数据传输对象
|
||||||
|
* @return 创建的动态对象
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Post createPost(PostRequestDTO postRequestDTO) {
|
||||||
|
Post post = new Post();
|
||||||
|
post.setUserId(postRequestDTO.getUserId());
|
||||||
|
post.setContent(postRequestDTO.getContent());
|
||||||
|
post.setTags(postRequestDTO.getTags());
|
||||||
|
post.setMediaOssKeys(new ArrayList<>());
|
||||||
|
|
||||||
|
// 如果有传入的媒体链接,则使用它们
|
||||||
|
if (postRequestDTO.getMediaUrls() != null && !postRequestDTO.getMediaUrls().isEmpty()) {
|
||||||
|
post.setMediaOssKeys(postRequestDTO.getMediaUrls());
|
||||||
|
}
|
||||||
|
|
||||||
post.setIsPublic(0);
|
post.setIsPublic(0);
|
||||||
post.setUpdatedAt(LocalDateTime.now());
|
post.setUpdatedAt(LocalDateTime.now());
|
||||||
post.setCreatedAt(LocalDateTime.now());
|
post.setCreatedAt(LocalDateTime.now());
|
||||||
|
|
||||||
// 保存动态到数据库
|
// 保存动态到数据库
|
||||||
postMapper.insert(post);
|
postMapper.insert(post);
|
||||||
return post;
|
return post;
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ public class AliOssUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传
|
* 文件上传
|
||||||
* @param bytes
|
* @param bytes 文件字节数组
|
||||||
* @param objectName
|
* @param objectName 对象名称
|
||||||
* @return
|
* @return 完整的文件访问URL
|
||||||
*/
|
*/
|
||||||
public String upload(byte[] bytes, String objectName) {
|
public String upload(byte[] bytes, String objectName) {
|
||||||
|
|
||||||
@@ -44,7 +44,8 @@ public class AliOssUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//文件访问路径规则 https://BucketName.Endpoint/ObjectName
|
// 文件访问路径规则 https://BucketName.Endpoint/ObjectName
|
||||||
|
// 构造完整的文件访问URL,格式如:https://oss.yourdomain.com/upload/img12345.jpg
|
||||||
StringBuilder stringBuilder = new StringBuilder("https://");
|
StringBuilder stringBuilder = new StringBuilder("https://");
|
||||||
stringBuilder
|
stringBuilder
|
||||||
.append(bucketName)
|
.append(bucketName)
|
||||||
@@ -52,8 +53,10 @@ public class AliOssUtil {
|
|||||||
.append(endpoint)
|
.append(endpoint)
|
||||||
.append("/")
|
.append("/")
|
||||||
.append(objectName);
|
.append(objectName);
|
||||||
log.info("文件上传到:{}", stringBuilder.toString());
|
|
||||||
|
String fileUrl = stringBuilder.toString();
|
||||||
|
log.info("文件上传到:{}", fileUrl);
|
||||||
|
|
||||||
return stringBuilder.toString();
|
return fileUrl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,7 @@ mybatis:
|
|||||||
# 阿里云 OSS 配置
|
# 阿里云 OSS 配置
|
||||||
aliyun:
|
aliyun:
|
||||||
oss:
|
oss:
|
||||||
endpoint: oss-cn-hangzhou.aliyuncs.com
|
endpoint: oss-cn-beijing.aliyuncs.com
|
||||||
access-key-id: LTAI5tKo9TpWH1aW6JxWm1Gp
|
access-key-id: LTAI5tKo9TpWH1aW6JxWm1Gp
|
||||||
access-key-secret: LHk9DdHECKCwIdaIM9fkGgEuowt18W
|
access-key-secret: LHk9DdHECKCwIdaIM9fkGgEuowt18W
|
||||||
bucket-name: heimato
|
bucket-name: heimato
|
||||||
Reference in New Issue
Block a user