完成动态发布摸块(有OSS文件上传,没有审核)
This commit is contained in:
@@ -1,18 +1,26 @@
|
||||
package com.bao.dating.service.impl;
|
||||
|
||||
import com.bao.dating.mapper.PostMapper;
|
||||
import com.bao.dating.pojo.dto.PostRequestDTO;
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
import com.bao.dating.service.PostService;
|
||||
import com.bao.dating.util.AliOssUtil;
|
||||
import com.bao.dating.util.FileUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 动态服务实现类
|
||||
*
|
||||
* @author KilLze
|
||||
*/
|
||||
@Service
|
||||
public class PostServiceImpl implements PostService {
|
||||
|
||||
@@ -22,47 +30,64 @@ public class PostServiceImpl implements PostService {
|
||||
@Autowired
|
||||
private PostMapper postMapper;
|
||||
|
||||
/**
|
||||
* 上传媒体文件
|
||||
* @param files 媒体文件数组
|
||||
* @return 上传后的文件URL列表
|
||||
*/
|
||||
@Override
|
||||
public Post createPost(Post post) {
|
||||
// 检查并确保userId不为空,如果为空则设置默认值
|
||||
if (post.getUserId() == null) {
|
||||
post.setUserId(1); // 设置默认用户ID为1
|
||||
}
|
||||
|
||||
// 处理图片和视频上传到 OSS
|
||||
List<String> mediaOssKeys = new ArrayList<>();
|
||||
|
||||
// 判断媒体文件是否为空
|
||||
if (post.getMediaOssKeys() != null && !post.getMediaOssKeys().isEmpty()) {
|
||||
|
||||
// 上传媒体文件并判断文件类型
|
||||
for (String mediaUrl : post.getMediaOssKeys()) {
|
||||
// 根据文件扩展名判断文件类型
|
||||
String fileType = FileUtil.getFileType(mediaUrl);
|
||||
|
||||
// 生成唯一文件名
|
||||
String fileName = UUID.randomUUID().toString() + "." + FileUtil.getFileExtension(mediaUrl);
|
||||
|
||||
// 获取文件字节数据
|
||||
byte[] fileBytes = mediaUrl.getBytes();
|
||||
|
||||
// 根据文件类型处理
|
||||
String ossUrl = "";
|
||||
if ("image".equals(fileType)) {
|
||||
// 上传图片
|
||||
ossUrl = ossUtil.upload(fileBytes, fileName);
|
||||
} else if ("video".equals(fileType)) {
|
||||
// 上传视频
|
||||
ossUrl = ossUtil.upload(fileBytes, fileName);
|
||||
public List<String> uploadMedia(MultipartFile[] files) {
|
||||
List<String> mediaUrls = new ArrayList<>();
|
||||
if (files != null && files.length > 0) {
|
||||
for (MultipartFile file : files) {
|
||||
if (!file.isEmpty()) {
|
||||
try {
|
||||
// 根据文件扩展名判断文件类型
|
||||
String fileType = FileUtil.getFileType(file.getOriginalFilename());
|
||||
// 生成唯一文件名
|
||||
String fileName = UUID.randomUUID().toString() + "." + FileUtil.getFileExtension(file.getOriginalFilename());
|
||||
// 获取文件字节数据
|
||||
byte[] fileBytes = file.getBytes();
|
||||
// 根据文件类型处理
|
||||
String ossUrl = "";
|
||||
if ("image".equals(fileType) || "video".equals(fileType)) {
|
||||
// 上传图片或视频
|
||||
ossUrl = ossUtil.upload(fileBytes, fileName);
|
||||
}
|
||||
// 添加上传后的 URL
|
||||
mediaUrls.add(ossUrl);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// 添加上传后的 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.setUpdatedAt(LocalDateTime.now());
|
||||
post.setCreatedAt(LocalDateTime.now());
|
||||
|
||||
// 保存动态到数据库
|
||||
postMapper.insert(post);
|
||||
return post;
|
||||
|
||||
Reference in New Issue
Block a user