完成动态发布功能(审核未实现)
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package com.bao.dating.service.impl;
|
||||
|
||||
import com.bao.dating.mapper.PostMapper;
|
||||
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 java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class PostServiceImpl implements PostService {
|
||||
|
||||
@Autowired
|
||||
private AliOssUtil ossUtil;
|
||||
|
||||
@Autowired
|
||||
private PostMapper postMapper;
|
||||
|
||||
@Override
|
||||
public Post createPost(Post post) {
|
||||
// 处理图片和视频上传到 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);
|
||||
}
|
||||
// 添加上传后的 URL
|
||||
mediaOssKeys.add(ossUrl);
|
||||
}
|
||||
}
|
||||
post.setMediaOssKeys(mediaOssKeys);
|
||||
post.setIsPublic(0);
|
||||
post.setUpdatedAt(LocalDateTime.now());
|
||||
post.setCreatedAt(LocalDateTime.now());
|
||||
// 保存动态到数据库
|
||||
postMapper.insert(post);
|
||||
return post;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user