视频功能
一、发布视频
数据库表结构
{
"_id": ObjectId("5e82dd6164019531fc471ff0"),
"vid": NumberLong("100001"),
"userId": NumberLong("3"),
"picUrl": "https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/photo/4/1.jpg",
"videoUrl": "https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/video/1576134125940400.mp4",
"created": NumberLong("1585634657964"),
"seeType": NumberInt("1"),
"locationName": "上海市",
"_class": "com.tanhua.dubbo.server.pojo.Video",
"likeCount": 0,
"commentCount": 0,
"loveCount": 0
}
数据模型实体类
package com.tanhua.model.mongo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "video")
public class Video implements java.io.Serializable {
private static final long serialVersionUID = -3136732836884933873L;
private ObjectId id; //主键id
private Long vid; //自动增长
private Long created; //创建时间
private Long userId;//当前用户id
private String text; //文字
private String picUrl; //视频封面文件,URL
private String videoUrl; //视频文件,URL
private Integer likeCount=0; //点赞数
private Integer commentCount=0; //评论数
private Integer loveCount=0; //喜欢数
}
1. VideoController
package com.tanhua.server.controller;
import com.tanhua.model.domain.PageResult;
import com.tanhua.server.service.SmallVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@RestController
@RequestMapping("/smallVideos")
public class VideoController {
@Autowired
private SmallVideoService smallVideoService;
/**
* 发布视频:
* 请求路径:/smallVideos
* 请求方式:post
* 请求参数:videoThumbnail(视频封面文件),videoFile(视频文件)
*/
@PostMapping
public ResponseEntity uploadSmallVideos(MultipartFile videoThumbnail,MultipartFile videoFile) throws IOException {
smallVideoService.uploadSmallVideos(videoThumbnail,videoFile);
return ResponseEntity.ok(null);
}
}
2.SmallVideoService
package com.tanhua.server.service;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.PageUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.FocusUserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.domain.PageResult;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.FocusUser;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.VideoVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.ThreadLocalUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class SmallVideoService {
@DubboReference
private VideoApi videoApi;
@DubboReference
private FocusUserApi focusUserApi;
@Autowired
private OssTemplate ossTemplate;
@DubboReference
private UserInfoApi userInfoApi;
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Autowired
private FastFileStorageClient client;
@Autowired
private FdfsWebServer webServer;
/**
* 发布视频
* @param videoThumbnail 视频封面,图片;
* @param videoFile 视频
*/
public void uploadSmallVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
//1.判断文件或视频是否为空
if(videoThumbnail.isEmpty() || videoFile.isEmpty()){
throw new BusinessException(ErrorResult.error());
}
//2.图片上传阿里云oss,并得到访问路径
String imageUrl = ossTemplate.upload(videoThumbnail.getOriginalFilename(), videoThumbnail.getInputStream());
//3.视频上传fastDFS
String filename = videoFile.getOriginalFilename();
//文件后缀名
filename = filename.substring(filename.lastIndexOf(".") + 1);
StorePath path = client.uploadFile(videoFile.getInputStream(), videoFile.getSize(), filename, null);
String videoUrl = webServer.getWebServerUrl()+path.getFullPath();
//4.构造Video对象封装数据
Video video = new Video();
video.setUserId(ThreadLocalUtils.getUserId());
video.setPicUrl(imageUrl);
video.setVideoUrl(videoUrl);
video.setText("自古忠孝难两全");
//5.调用api保存数据
String videoId = videoApi.save(video);
if(StringUtils.isEmpty(videoId)){
throw new BusinessException(ErrorResult.error());
}
}
3.VideoApiImpl
package com.tanhua.dubbo.api;
import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.mongo.FocusUser;
import com.tanhua.model.mongo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.util.List;
@DubboService
public class VideoApiImpl implements VideoApi {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private IdWorker idWorker;
/**
*发布视频
*/
public String save(Video video) {
//1.补充缺失的属性
video.setVid(idWorker.getNextId("video"));
video.setCreated(System.currentTimeMillis());
//2.调用方法保存
mongoTemplate.save(video);
//3.返回用户id
return video.getId().toHexString();
}
}
二、查看视频列表
1.VideoController
/**
* 刷视频
* 请求路径:/smallVideos
* 请求方式:get
* 请求参数:page(当前页码),pagesize(每页展示数)
* 响应数据:VideoVo
*/
@GetMapping
public ResponseEntity lookSmallVideos(
@RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue = "10") Integer pagesize
){
PageResult pageResult = smallVideoService.lookSmallVideos(page,pagesize);
return ResponseEntity.ok(pageResult);
}
2.SmallVideoService
/**
* 查看小视频列表
* @param page
* @param pagesize
* @return
*/
public PageResult lookSmallVideos(Integer page, Integer pagesize) {
//1.查看redis中是否缓存有大数据推荐系统的推荐id
Long userId = ThreadLocalUtils.getUserId();
String redisKey = Constants.VIDEOS_RECOMMEND + userId;
String redisValues = redisTemplate.opsForValue().get(redisKey);
int redisPage = 0;
List<Video> videos = new ArrayList<>();
if(!StringUtils.isEmpty(redisValues)){
//2.redis缓存的数据不为空,根据redis缓存的vid分页查询小视频
String[] values = redisValues.split(",");
if(( page-1 ) * pagesize < values.length) {
List<Long> vids = Arrays.stream(values).skip((page - 1) * pagesize).limit(pagesize)
.map(e -> Long.valueOf(e))
.collect(Collectors.toList());
videos = videoApi.lookRcommendVideos(vids);
}
redisPage = PageUtil.totalPage(values.length, pagesize);
}
if(CollUtil.isEmpty(videos)){
//3.redis缓存中没有数据
videos = videoApi.lookVideos(page-redisPage,pagesize);
}
//4.提取小视频的用户id,再根据用户id查询用户详情
List<Long> userIds = CollUtil.getFieldValues(videos, "userId", Long.class);
Map<Long, UserInfo> userInfoMap = userInfoApi.batchQueryUserInfo(userIds, null);
//5.遍历videos集合,构造vo对象
List<VideoVo> vos = new ArrayList<>();
for (Video video : videos) {
Long id = video.getUserId();
UserInfo userInfo = userInfoMap.get(id);
if(userInfo != null){
VideoVo vo = VideoVo.init(userInfo, video);
String key = Constants.FOCUS_USER_KEY + userId;
if(redisTemplate.hasKey(key)){
vo.setHasFocus(1);
}
vos.add(vo);
}
}
return new PageResult(page,pagesize,0,vos);
}
3.VideoApiImpl
/**
* 根据vid查
* @param vids
* @return
*/
public List<Video> lookRcommendVideos(List<Long> vids) {
Criteria criteria = Criteria.where("vid").in(vids);
Query query = Query.query(criteria);
List<Video> videos = mongoTemplate.find(query, Video.class);
return videos;
}
/**
* 根据发布时间倒序查
* @param page
* @param pagesize
* @return
*/
public List<Video> lookVideos(int page, Integer pagesize) {
Query query = new Query();
Query query1 = query.skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
List<Video> videos = mongoTemplate.find(query1, Video.class);
return videos;
}
三、关注视频发布用户
数据库表结构分析
{
"_id": ObjectId("6364ca6fb25a645456f713f3"),
"userId": NumberLong("1"),
"followUserId": NumberLong("106"),
"created": NumberLong("1667549807183"),
"_class": "com.tanhua.model.mongo.FocusUser"
}
封装数据的实体类
package com.tanhua.model.mongo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.Document;
//用户关注表(关注小视频的发布作者)
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "focus_user")
public class FocusUser implements java.io.Serializable{
private static final long serialVersionUID = 3148619072405056052L;
private ObjectId id; //主键id
private Long userId; //用户id 106
private Long followUserId; //关注的用户id 1
private Long created; //关注时间
}
1.VideoController
/**
* 关注视频发布用户:
* 请求路径:/smallVideos/:uid/userFocus
* 请求方式:post
* 请求参数:路径参数uid,要关注得用户id
*
*/
@PostMapping("/{uid}/userFocus")
public ResponseEntity focusUser(@PathVariable("uid") Long focusUserId){
smallVideoService.focusVideoUser(focusUserId);
return ResponseEntity.ok(null);
}
2.SmallVideoService
/**
* 关注视频发布作者
* @param focusUserId
*/
public void focusVideoUser(Long focusUserId) {
//1.创建对象封装数据
Long currentUserId = ThreadLocalUtils.getUserId();
FocusUser focusUser = new FocusUser();
focusUser.setUserId(currentUserId);
focusUser.setFollowUserId(focusUserId);
focusUser.setCreated(System.currentTimeMillis());
//2.把关注的数据缓存进redis
String key = Constants.FOCUS_USER_KEY + currentUserId;
String hashKey = String.valueOf(focusUserId);
redisTemplate.opsForHash().put(key, hashKey, "1");
//3.调用api保存数据
focusUserApi.save(focusUser);
}
3.FocusUserApiImpl
package com.tanhua.dubbo.api;
import com.tanhua.model.mongo.FocusUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
@DubboService
public class FocusUserApiImpl implements FocusUserApi {
@Autowired
private MongoTemplate mongoTemplate;
/**
*
*保存关注用户的数据
*/
public void save(FocusUser focusUser) {
//保证不重复关注
Criteria criteria =
Criteria.where("userId")
.is(focusUser.getUserId())
.and("followUserId")
.is(focusUser.getFollowUserId());
Query query = Query.query(criteria);
FocusUser user = mongoTemplate.findOne(query, FocusUser.class);
if( user== null){
focusUser.setId(ObjectId.get());
mongoTemplate.save(focusUser);
}
}
}
四、取消关注用户
1.VideoController
/**
* 取消关注
* 请求路径:/smallVideos/:uid/userUnFocus
* 请求方式:post
* 请求参数:uid(要取消关注的用户id)
*/
@PostMapping("/{uid}/userUnFocus")
public ResponseEntity userUnFocus(@PathVariable("uid") Integer unFocusUserId){
smallVideoService.unFoucus(unFocusUserId);
return ResponseEntity.ok(null);
}
2.SmallVideoController
/**
* 取消关注
* @param unFocusUserId
*/
public void unFoucus(Integer unFocusUserId) {
Long currentUserId = ThreadLocalUtils.getUserId();
//1.先删除数据库记录
videoApi.delete(currentUserId,unFocusUserId);
//2.再删除redis中的缓存
String key = Constants.FOCUS_USER_KEY + currentUserId;
String hashKey = String.valueOf(unFocusUserId);
redisTemplate.opsForHash().delete(key, hashKey);
}
3.VideoApiImpl
/**
* 取消关注
*/
public void delete(Long currentUserId, Integer unFocusUserId) {
Criteria criteria = Criteria.where("userId").is(currentUserId).and("followUserId").is(unFocusUserId);
Query query = Query.query(criteria);
mongoTemplate.remove(query, FocusUser.class);
}