From 12cfe0a184dca7c9b534d75038a0ce2054777ea7 Mon Sep 17 00:00:00 2001 From: Guwan Date: Sun, 8 Dec 2024 19:25:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=A7=86=E9=A2=91):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 视频初步 --- pom.xml | 9 +- .../com/guwan/backend/client/SrsClient.java | 166 ++++++------ .../backend/controller/LiveController.java | 164 ++++++------ .../backend/controller/VideoController.java | 4 +- .../com/guwan/backend/entity/LiveMessage.java | 6 + .../com/guwan/backend/entity/LiveRoom.java | 7 + .../guwan/backend/service/LiveService.java | 252 +++++++++--------- .../guwan/backend/service/VideoService.java | 3 +- .../service/impl/VideoServiceImpl.java | 9 +- .../com/guwan/backend/util/MinioUtil.java | 16 ++ .../websocket/LiveWebSocketHandler.java | 164 ++++++------ .../com/guwan/backend/util/MinioUtilTest.java | 7 + 12 files changed, 423 insertions(+), 384 deletions(-) diff --git a/pom.xml b/pom.xml index 8cbe2ac..2a08e6e 100644 --- a/pom.xml +++ b/pom.xml @@ -229,13 +229,6 @@ spring-boot-starter-websocket - - - com.github.ossrs - srs-sdk - 1.0.0 - - io.netty @@ -245,7 +238,7 @@ - cn.easyes + cn.easy-es easy-es-boot-starter 1.1.1 diff --git a/src/main/java/com/guwan/backend/client/SrsClient.java b/src/main/java/com/guwan/backend/client/SrsClient.java index eeb59eb..58f54d4 100644 --- a/src/main/java/com/guwan/backend/client/SrsClient.java +++ b/src/main/java/com/guwan/backend/client/SrsClient.java @@ -1,83 +1,83 @@ -package com.guwan.backend.client; - -import lombok.Data; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; - -@Slf4j -@Component -@RequiredArgsConstructor -public class SrsClient { - - private final RestTemplate restTemplate; - - @Value("${srs.server.url}") - private String srsServerUrl; - - /** - * 开始录制 - */ - public void startRecord(String streamKey, RecordConfig config) { - String url = String.format("%s/api/v1/streams/%s/recording/start", srsServerUrl, streamKey); - try { - ResponseEntity response = restTemplate.postForEntity(url, config, String.class); - if (!response.getStatusCode().is2xxSuccessful()) { - throw new RuntimeException("开始录制失败: " + response.getBody()); - } - } catch (Exception e) { - log.error("开始录制失败", e); - throw new RuntimeException("开始录制失败", e); - } - } - - /** - * 停止录制 - */ - public void stopRecord(String streamKey) { - String url = String.format("%s/api/v1/streams/%s/recording/stop", srsServerUrl, streamKey); - try { - ResponseEntity response = restTemplate.postForEntity(url, null, String.class); - if (!response.getStatusCode().is2xxSuccessful()) { - throw new RuntimeException("停止录制失败: " + response.getBody()); - } - } catch (Exception e) { - log.error("停止录制失败", e); - throw new RuntimeException("停止录制失败", e); - } - } - - /** - * 获取流信息 - */ - public StreamInfo getStreamInfo(String streamKey) { - String url = String.format("%s/api/v1/streams/%s", srsServerUrl, streamKey); - try { - ResponseEntity response = restTemplate.getForEntity(url, StreamInfo.class); - return response.getBody(); - } catch (Exception e) { - log.error("获取流信息失败", e); - throw new RuntimeException("获取流信息失败", e); - } - } - - @Data - public static class RecordConfig { - private String format = "flv"; // 录制格式 - private String filePath; // 文件路径 - } - - @Data - public static class StreamInfo { - private String streamId; // 流ID - private String clientId; // 客户端ID - private String ip; // 客户端IP - private Long startTime; // 开始时间 - private String status; // 状态 - private Long bytesIn; // 输入字节数 - private Long bytesOut; // 输出字节数 - } -} \ No newline at end of file +//package com.guwan.backend.client; +// +//import lombok.Data; +//import lombok.RequiredArgsConstructor; +//import lombok.extern.slf4j.Slf4j; +//import org.springframework.beans.factory.annotation.Value; +//import org.springframework.http.ResponseEntity; +//import org.springframework.stereotype.Component; +//import org.springframework.web.client.RestTemplate; +// +//@Slf4j +//@Component +//@RequiredArgsConstructor +//public class SrsClient { +// +// private final RestTemplate restTemplate; +// +// @Value("${srs.server.url}") +// private String srsServerUrl; +// +// /** +// * 开始录制 +// */ +// public void startRecord(String streamKey, RecordConfig config) { +// String url = String.format("%s/api/v1/streams/%s/recording/start", srsServerUrl, streamKey); +// try { +// ResponseEntity response = restTemplate.postForEntity(url, config, String.class); +// if (!response.getStatusCode().is2xxSuccessful()) { +// throw new RuntimeException("开始录制失败: " + response.getBody()); +// } +// } catch (Exception e) { +// log.error("开始录制失败", e); +// throw new RuntimeException("开始录制失败", e); +// } +// } +// +// /** +// * 停止录制 +// */ +// public void stopRecord(String streamKey) { +// String url = String.format("%s/api/v1/streams/%s/recording/stop", srsServerUrl, streamKey); +// try { +// ResponseEntity response = restTemplate.postForEntity(url, null, String.class); +// if (!response.getStatusCode().is2xxSuccessful()) { +// throw new RuntimeException("停止录制失败: " + response.getBody()); +// } +// } catch (Exception e) { +// log.error("停止录制失败", e); +// throw new RuntimeException("停止录制失败", e); +// } +// } +// +// /** +// * 获取流信息 +// */ +// public StreamInfo getStreamInfo(String streamKey) { +// String url = String.format("%s/api/v1/streams/%s", srsServerUrl, streamKey); +// try { +// ResponseEntity response = restTemplate.getForEntity(url, StreamInfo.class); +// return response.getBody(); +// } catch (Exception e) { +// log.error("获取流信息失败", e); +// throw new RuntimeException("获取流信息失败", e); +// } +// } +// +// @Data +// public static class RecordConfig { +// private String format = "flv"; // 录制格式 +// private String filePath; // 文件路径 +// } +// +// @Data +// public static class StreamInfo { +// private String streamId; // 流ID +// private String clientId; // 客户端ID +// private String ip; // 客户端IP +// private Long startTime; // 开始时间 +// private String status; // 状态 +// private Long bytesIn; // 输入字节数 +// private Long bytesOut; // 输出字节数 +// } +//} \ No newline at end of file diff --git a/src/main/java/com/guwan/backend/controller/LiveController.java b/src/main/java/com/guwan/backend/controller/LiveController.java index 58728f9..ad92228 100644 --- a/src/main/java/com/guwan/backend/controller/LiveController.java +++ b/src/main/java/com/guwan/backend/controller/LiveController.java @@ -1,82 +1,82 @@ -package com.guwan.backend.controller; - -import com.guwan.backend.entity.LiveRoom; -import com.guwan.backend.entity.LiveRoomDTO; -import com.guwan.backend.service.LiveService; -import com.guwan.backend.util.Result; -import com.guwan.backend.util.SecurityUtil; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.*; -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.media.Schema; -import io.swagger.v3.oas.annotations.security.SecurityRequirement; -import io.swagger.v3.oas.annotations.tags.Tag; - -@Slf4j -@Tag(name = "直播管理", description = "直播相关接口") -@RestController -@RequestMapping("/api/live") -@RequiredArgsConstructor -public class LiveController { - - private final LiveService liveService; - private final SecurityUtil securityUtil; - - @Operation(summary = "创建直播间") - @SecurityRequirement(name = "bearer-jwt") - @PostMapping("/room") - public Result createLiveRoom(@RequestBody LiveRoomDTO dto) { - try { - return Result.success(liveService.createLiveRoom(dto)); - } catch (Exception e) { - log.error("创建直播间失败", e); - return Result.error(e.getMessage()); - } - } - - @Operation(summary = "开始直播") - @SecurityRequirement(name = "bearer-jwt") - @PostMapping("/room/{id}/start") - public Result startLive(@PathVariable Long id) { - try { - // 检查权限 - checkPermission(id); - liveService.startLive(id); - return Result.success(); - } catch (Exception e) { - log.error("开始直播失败", e); - return Result.error(e.getMessage()); - } - } - - @Operation(summary = "结束直播") - @SecurityRequirement(name = "bearer-jwt") - @PostMapping("/room/{id}/end") - public Result endLive(@PathVariable Long id) { - try { - // 检查权限 - checkPermission(id); - liveService.endLive(id); - return Result.success(); - } catch (Exception e) { - log.error("结束直播失败", e); - return Result.error(e.getMessage()); - } - } - - /** - * 检查权限 - */ - private void checkPermission(Long roomId) { - LiveRoom room = liveService.getLiveRoom(roomId); - if (room == null) { - throw new IllegalArgumentException("直播间不存在"); - } - - Long currentUserId = securityUtil.getCurrentUserId(); - if (!room.getUserId().equals(currentUserId)) { - throw new IllegalStateException("无权操作此直播间"); - } - } -} \ No newline at end of file +//package com.guwan.backend.controller; +// +//import com.guwan.backend.entity.LiveRoom; +//import com.guwan.backend.entity.LiveRoomDTO; +//import com.guwan.backend.service.LiveService; +//import com.guwan.backend.util.Result; +//import com.guwan.backend.util.SecurityUtil; +//import lombok.RequiredArgsConstructor; +//import lombok.extern.slf4j.Slf4j; +//import org.springframework.web.bind.annotation.*; +//import io.swagger.v3.oas.annotations.Operation; +//import io.swagger.v3.oas.annotations.media.Schema; +//import io.swagger.v3.oas.annotations.security.SecurityRequirement; +//import io.swagger.v3.oas.annotations.tags.Tag; +// +//@Slf4j +//@Tag(name = "直播管理", description = "直播相关接口") +//@RestController +//@RequestMapping("/api/live") +//@RequiredArgsConstructor +//public class LiveController { +// +// private final LiveService liveService; +// private final SecurityUtil securityUtil; +// +// @Operation(summary = "创建直播间") +// @SecurityRequirement(name = "bearer-jwt") +// @PostMapping("/room") +// public Result createLiveRoom(@RequestBody LiveRoomDTO dto) { +// try { +// return Result.success(liveService.createLiveRoom(dto)); +// } catch (Exception e) { +// log.error("创建直播间失败", e); +// return Result.error(e.getMessage()); +// } +// } +// +// @Operation(summary = "开始直播") +// @SecurityRequirement(name = "bearer-jwt") +// @PostMapping("/room/{id}/start") +// public Result startLive(@PathVariable Long id) { +// try { +// // 检查权限 +// checkPermission(id); +// liveService.startLive(id); +// return Result.success(); +// } catch (Exception e) { +// log.error("开始直播失败", e); +// return Result.error(e.getMessage()); +// } +// } +// +// @Operation(summary = "结束直播") +// @SecurityRequirement(name = "bearer-jwt") +// @PostMapping("/room/{id}/end") +// public Result endLive(@PathVariable Long id) { +// try { +// // 检查权限 +// checkPermission(id); +// liveService.endLive(id); +// return Result.success(); +// } catch (Exception e) { +// log.error("结束直播失败", e); +// return Result.error(e.getMessage()); +// } +// } +// +// /** +// * 检查权限 +// */ +// private void checkPermission(Long roomId) { +// LiveRoom room = liveService.getLiveRoom(roomId); +// if (room == null) { +// throw new IllegalArgumentException("直播间不存在"); +// } +// +// Long currentUserId = securityUtil.getCurrentUserId(); +// if (!room.getUserId().equals(currentUserId)) { +// throw new IllegalStateException("无权操作此直播间"); +// } +// } +//} \ No newline at end of file diff --git a/src/main/java/com/guwan/backend/controller/VideoController.java b/src/main/java/com/guwan/backend/controller/VideoController.java index 55569ac..a1c169b 100644 --- a/src/main/java/com/guwan/backend/controller/VideoController.java +++ b/src/main/java/com/guwan/backend/controller/VideoController.java @@ -27,12 +27,12 @@ public class VideoController { // @SecurityRequirement(name = "bearer-jwt") @PostMapping("/upload") public Result uploadVideo( - @Parameter(description = "视频文件") @RequestParam("file") MultipartFile file, + @Parameter(description = "视频文件") @RequestParam("fileUrl") String fileUrl, @Parameter(description = "视频标题") @RequestParam("title") String title, @Parameter(description = "视频描述") @RequestParam("description") String description, @Parameter(description = "视频标签,多个用逗号分隔") @RequestParam(value = "tags", required = false) String tags) { try { - VideoDTO video = videoService.uploadVideo(file, title, description, tags); + VideoDTO video = videoService.uploadVideo(fileUrl, title, description, tags); return Result.success(video); } catch (Exception e) { log.error("上传视频失败", e); diff --git a/src/main/java/com/guwan/backend/entity/LiveMessage.java b/src/main/java/com/guwan/backend/entity/LiveMessage.java index 9475b7c..c99e4c4 100644 --- a/src/main/java/com/guwan/backend/entity/LiveMessage.java +++ b/src/main/java/com/guwan/backend/entity/LiveMessage.java @@ -1,3 +1,9 @@ +package com.guwan.backend.entity; + +import lombok.Data; + +import java.time.LocalDateTime; + @Data public class LiveMessage { private String type; // 消息类型:CHAT-聊天,GIFT-礼物,LIKE-点赞,ENTER-进入,LEAVE-离开 diff --git a/src/main/java/com/guwan/backend/entity/LiveRoom.java b/src/main/java/com/guwan/backend/entity/LiveRoom.java index e4aff7b..8403d00 100644 --- a/src/main/java/com/guwan/backend/entity/LiveRoom.java +++ b/src/main/java/com/guwan/backend/entity/LiveRoom.java @@ -1,3 +1,10 @@ +package com.guwan.backend.entity; + +import com.baomidou.mybatisplus.annotation.*; +import lombok.Data; + +import java.time.LocalDateTime; + @Data @TableName("live_room") public class LiveRoom { diff --git a/src/main/java/com/guwan/backend/service/LiveService.java b/src/main/java/com/guwan/backend/service/LiveService.java index 7d606be..bbcaa57 100644 --- a/src/main/java/com/guwan/backend/service/LiveService.java +++ b/src/main/java/com/guwan/backend/service/LiveService.java @@ -1,120 +1,132 @@ -@Slf4j -@Service -@RequiredArgsConstructor -public class LiveService { - - private final LiveRoomMapper liveRoomMapper; - private final SrsClient srsClient; - private final SecurityUtil securityUtil; - - /** - * 创建直播间 - */ - public LiveRoom createLiveRoom(LiveRoomDTO dto) { - // 获取当前用户 - Long userId = securityUtil.getCurrentUserId(); - String username = securityUtil.getCurrentUsername(); - if (userId == null) { - throw new IllegalStateException("用户未登录"); - } - - LiveRoom room = new LiveRoom(); - BeanUtils.copyProperties(dto, room); - - // 设置用户信息 - room.setUserId(userId); - room.setUsername(username); - - // 生成推流密钥 - String streamKey = generateStreamKey(); - room.setStreamKey(streamKey); - - // 生成推流地址 - String rtmpUrl = generateRtmpUrl(streamKey); - room.setRtmpUrl(rtmpUrl); - - // 生成播放地址 - String hlsUrl = generateHlsUrl(streamKey); - room.setHlsUrl(hlsUrl); - - room.setStatus("PREPARING"); - room.setOnlineCount(0); - room.setLikeCount(0); - - liveRoomMapper.insert(room); - return room; - } - - /** - * 开始直播 - */ - public void startLive(Long roomId) { - LiveRoom room = liveRoomMapper.selectById(roomId); - room.setStatus("LIVING"); - liveRoomMapper.updateById(room); - - // 开始录制 - startRecording(room.getStreamKey()); - } - - /** - * 结束直播 - */ - public void endLive(Long roomId) { - LiveRoom room = liveRoomMapper.selectById(roomId); - room.setStatus("ENDED"); - liveRoomMapper.updateById(room); - - // 停止录制 - stopRecording(room.getStreamKey()); - - // 生成回放地址 - String replayUrl = generateReplayUrl(room.getStreamKey()); - room.setReplayUrl(replayUrl); - liveRoomMapper.updateById(room); - } - - /** - * 开始录制 - */ - private void startRecording(String streamKey) { - SrsClient.RecordConfig config = new SrsClient.RecordConfig(); - config.setFilePath("/recordings/" + streamKey + ".flv"); - srsClient.startRecord(streamKey, config); - } - - /** - * 停止录制 - */ - private void stopRecording(String streamKey) { - srsClient.stopRecord(streamKey); - } - - /** - * 生成推流密钥 - */ - private String generateStreamKey() { - return UUID.randomUUID().toString(); - } - - /** - * 生成推流地址 - */ - private String generateRtmpUrl(String streamKey) { - return "rtmp://localhost:1935/live/" + streamKey; - } - - /** - * 生成播放地址 - */ - private String generateHlsUrl(String streamKey) { - return "http://localhost:8088/live/" + streamKey + ".m3u8"; - } - - /** - * 生成回放地址 - */ - private String generateReplayUrl(String streamKey) { - return "http://localhost:8080/recordings/" + streamKey + ".flv"; - } -} \ No newline at end of file +//package com.guwan.backend.service; +// +//import com.guwan.backend.client.SrsClient; +//import com.guwan.backend.dto.live.LiveRoomDTO; +//import com.guwan.backend.entity.LiveRoom; +//import com.guwan.backend.mapper.LiveRoomMapper; +//import com.guwan.backend.util.SecurityUtil; +//import lombok.RequiredArgsConstructor; +//import lombok.extern.slf4j.Slf4j; +//import org.springframework.beans.BeanUtils; +//import org.springframework.stereotype.Service; +// +//@Slf4j +//@Service +//@RequiredArgsConstructor +//public class LiveService { +// +// private final LiveRoomMapper liveRoomMapper; +// private final SrsClient srsClient; +// private final SecurityUtil securityUtil; +// +// /** +// * 创建直播间 +// */ +// public LiveRoom createLiveRoom(LiveRoomDTO dto) { +// // 获取当前用户 +// Long userId = securityUtil.getCurrentUserId(); +// String username = securityUtil.getCurrentUsername(); +// if (userId == null) { +// throw new IllegalStateException("用户未登录"); +// } +// +// LiveRoom room = new LiveRoom(); +// BeanUtils.copyProperties(dto, room); +// +// // 设置用户信息 +// room.setUserId(userId); +// room.setUsername(username); +// +// // 生成推流密钥 +// String streamKey = generateStreamKey(); +// room.setStreamKey(streamKey); +// +// // 生成推流地址 +// String rtmpUrl = generateRtmpUrl(streamKey); +// room.setRtmpUrl(rtmpUrl); +// +// // 生成播放地址 +// String hlsUrl = generateHlsUrl(streamKey); +// room.setHlsUrl(hlsUrl); +// +// room.setStatus("PREPARING"); +// room.setOnlineCount(0); +// room.setLikeCount(0); +// +// liveRoomMapper.insert(room); +// return room; +// } +// +// /** +// * 开始直播 +// */ +// public void startLive(Long roomId) { +// LiveRoom room = liveRoomMapper.selectById(roomId); +// room.setStatus("LIVING"); +// liveRoomMapper.updateById(room); +// +// // 开始录制 +// startRecording(room.getStreamKey()); +// } +// +// /** +// * 结束直播 +// */ +// public void endLive(Long roomId) { +// LiveRoom room = liveRoomMapper.selectById(roomId); +// room.setStatus("ENDED"); +// liveRoomMapper.updateById(room); +// +// // 停止录制 +// stopRecording(room.getStreamKey()); +// +// // 生成回放地址 +// String replayUrl = generateReplayUrl(room.getStreamKey()); +// room.setReplayUrl(replayUrl); +// liveRoomMapper.updateById(room); +// } +// +// /** +// * 开始录制 +// */ +// private void startRecording(String streamKey) { +// SrsClient.RecordConfig config = new SrsClient.RecordConfig(); +// config.setFilePath("/recordings/" + streamKey + ".flv"); +// srsClient.startRecord(streamKey, config); +// } +// +// /** +// * 停止录制 +// */ +// private void stopRecording(String streamKey) { +// srsClient.stopRecord(streamKey); +// } +// +// /** +// * 生成推流密钥 +// */ +// private String generateStreamKey() { +// return UUID.randomUUID().toString(); +// } +// +// /** +// * 生成推流地址 +// */ +// private String generateRtmpUrl(String streamKey) { +// return "rtmp://localhost:1935/live/" + streamKey; +// } +// +// /** +// * 生成播放地址 +// */ +// private String generateHlsUrl(String streamKey) { +// return "http://localhost:8088/live/" + streamKey + ".m3u8"; +// } +// +// /** +// * 生成回放地址 +// */ +// private String generateReplayUrl(String streamKey) { +// return "http://localhost:8080/recordings/" + streamKey + ".flv"; +// } +//} \ No newline at end of file diff --git a/src/main/java/com/guwan/backend/service/VideoService.java b/src/main/java/com/guwan/backend/service/VideoService.java index cfcedc5..e40945f 100644 --- a/src/main/java/com/guwan/backend/service/VideoService.java +++ b/src/main/java/com/guwan/backend/service/VideoService.java @@ -2,11 +2,10 @@ package com.guwan.backend.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.guwan.backend.dto.video.VideoDTO; -import org.springframework.web.multipart.MultipartFile; public interface VideoService { // 上传视频 - VideoDTO uploadVideo(MultipartFile file, String title, String description, String tags); + VideoDTO uploadVideo(String fileUrl, String title, String description, String tags); // 更新视频信息 VideoDTO updateVideo(VideoDTO videoDTO); diff --git a/src/main/java/com/guwan/backend/service/impl/VideoServiceImpl.java b/src/main/java/com/guwan/backend/service/impl/VideoServiceImpl.java index a4fd690..658c55f 100644 --- a/src/main/java/com/guwan/backend/service/impl/VideoServiceImpl.java +++ b/src/main/java/com/guwan/backend/service/impl/VideoServiceImpl.java @@ -18,7 +18,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.multipart.MultipartFile; import java.util.List; @@ -36,7 +35,7 @@ public class VideoServiceImpl implements VideoService { @Override @Transactional @OperationLog(description = "上传视频", operationType = "上传") - public VideoDTO uploadVideo(MultipartFile file, String title, String description, String tags) { + public VideoDTO uploadVideo(String fileUrl, String title, String description, String tags) { // 获取当前用户 Long userId = securityUtil.getCurrentUserId(); if (userId == null) { @@ -45,15 +44,15 @@ public class VideoServiceImpl implements VideoService { try { // 上传视频文件到MinIO - String fileName = minioUtil.uploadFile("videos", file); - String url = minioUtil.getUrl(minioUtil.getFileUrl("videos", fileName)); + + String url = fileUrl; // 创建视频记录 Video video = new Video(); video.setTitle(title); video.setDescription(description); video.setUrl(url); - video.setSize(file.getSize()); + // video.setSize(file.getSize()); video.setUserId(userId); video.setStatus("PUBLISHED"); video.setTags(tags); diff --git a/src/main/java/com/guwan/backend/util/MinioUtil.java b/src/main/java/com/guwan/backend/util/MinioUtil.java index 1cf1077..957216c 100644 --- a/src/main/java/com/guwan/backend/util/MinioUtil.java +++ b/src/main/java/com/guwan/backend/util/MinioUtil.java @@ -172,6 +172,22 @@ public class MinioUtil { return UUID.randomUUID() + "." + extension; } + public long getFileSize(String bucketName, String objectName) { + try { + StatObjectResponse objectInfo = minioClient.statObject( + StatObjectArgs.builder() + .bucket(bucketName) + .object(objectName) + .build()); + return objectInfo.size(); + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + + // 初始化时创建视频桶 @PostConstruct public void init() { diff --git a/src/main/java/com/guwan/backend/websocket/LiveWebSocketHandler.java b/src/main/java/com/guwan/backend/websocket/LiveWebSocketHandler.java index f29a10c..88e6244 100644 --- a/src/main/java/com/guwan/backend/websocket/LiveWebSocketHandler.java +++ b/src/main/java/com/guwan/backend/websocket/LiveWebSocketHandler.java @@ -1,82 +1,82 @@ -package com.guwan.backend.websocket; - -import com.alibaba.fastjson.JSON; -import com.guwan.backend.entity.LiveMessage; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.web.socket.CloseStatus; -import org.springframework.web.socket.TextMessage; -import org.springframework.web.socket.WebSocketSession; -import org.springframework.web.socket.handler.TextWebSocketHandler; - -import java.io.IOException; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentHashSet; - -@Slf4j -@Component -public class LiveWebSocketHandler extends TextWebSocketHandler { - - private static final Map> roomSessions = new ConcurrentHashMap<>(); - private static final Map userSessions = new ConcurrentHashMap<>(); - - @Override - public void afterConnectionEstablished(WebSocketSession session) { - String roomId = getRoomId(session); - String userId = getUserId(session); - - // 加入房间 - roomSessions.computeIfAbsent(roomId, k -> new ConcurrentHashSet<>()).add(session); - userSessions.put(userId, session); - - // 广播进入消息 - broadcastMessage(roomId, createEnterMessage(userId)); - } - - @Override - public void handleTextMessage(WebSocketSession session, TextMessage message) { - String roomId = getRoomId(session); - LiveMessage liveMessage = JSON.parseObject(message.getPayload(), LiveMessage.class); - - // 处理不同类型的消息 - switch (liveMessage.getType()) { - case "CHAT": - broadcastMessage(roomId, message); - break; - case "GIFT": - handleGiftMessage(roomId, liveMessage); - break; - case "LIKE": - handleLikeMessage(roomId, liveMessage); - break; - } - } - - @Override - public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { - String roomId = getRoomId(session); - String userId = getUserId(session); - - // 离开房间 - roomSessions.get(roomId).remove(session); - userSessions.remove(userId); - - // 广播离开消息 - broadcastMessage(roomId, createLeaveMessage(userId)); - } - - private void broadcastMessage(String roomId, TextMessage message) { - Set sessions = roomSessions.get(roomId); - if (sessions != null) { - sessions.forEach(session -> { - try { - session.sendMessage(message); - } catch (IOException e) { - log.error("发送消息失败", e); - } - }); - } - } -} \ No newline at end of file +//package com.guwan.backend.websocket; +// +//import com.alibaba.fastjson.JSON; +//import com.guwan.backend.entity.LiveMessage; +//import lombok.extern.slf4j.Slf4j; +//import org.springframework.stereotype.Component; +//import org.springframework.web.socket.CloseStatus; +//import org.springframework.web.socket.TextMessage; +//import org.springframework.web.socket.WebSocketSession; +//import org.springframework.web.socket.handler.TextWebSocketHandler; +// +//import java.io.IOException; +//import java.util.Map; +//import java.util.Set; +//import java.util.concurrent.ConcurrentHashMap; +//import java.util.concurrent.ConcurrentHashSet; +// +//@Slf4j +//@Component +//public class LiveWebSocketHandler extends TextWebSocketHandler { +// +// private static final Map> roomSessions = new ConcurrentHashMap<>(); +// private static final Map userSessions = new ConcurrentHashMap<>(); +// +// @Override +// public void afterConnectionEstablished(WebSocketSession session) { +// String roomId = getRoomId(session); +// String userId = getUserId(session); +// +// // 加入房间 +// roomSessions.computeIfAbsent(roomId, k -> new ConcurrentHashSet<>()).add(session); +// userSessions.put(userId, session); +// +// // 广播进入消息 +// broadcastMessage(roomId, createEnterMessage(userId)); +// } +// +// @Override +// public void handleTextMessage(WebSocketSession session, TextMessage message) { +// String roomId = getRoomId(session); +// LiveMessage liveMessage = JSON.parseObject(message.getPayload(), LiveMessage.class); +// +// // 处理不同类型的消息 +// switch (liveMessage.getType()) { +// case "CHAT": +// broadcastMessage(roomId, message); +// break; +// case "GIFT": +// handleGiftMessage(roomId, liveMessage); +// break; +// case "LIKE": +// handleLikeMessage(roomId, liveMessage); +// break; +// } +// } +// +// @Override +// public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { +// String roomId = getRoomId(session); +// String userId = getUserId(session); +// +// // 离开房间 +// roomSessions.get(roomId).remove(session); +// userSessions.remove(userId); +// +// // 广播离开消息 +// broadcastMessage(roomId, createLeaveMessage(userId)); +// } +// +// private void broadcastMessage(String roomId, TextMessage message) { +// Set sessions = roomSessions.get(roomId); +// if (sessions != null) { +// sessions.forEach(session -> { +// try { +// session.sendMessage(message); +// } catch (IOException e) { +// log.error("发送消息失败", e); +// } +// }); +// } +// } +//} \ No newline at end of file diff --git a/src/test/java/com/guwan/backend/util/MinioUtilTest.java b/src/test/java/com/guwan/backend/util/MinioUtilTest.java index 952e6b5..45da29d 100644 --- a/src/test/java/com/guwan/backend/util/MinioUtilTest.java +++ b/src/test/java/com/guwan/backend/util/MinioUtilTest.java @@ -71,4 +71,11 @@ class MinioUtilTest { // 删除图片 // minioUtil.deleteFile(minioConfig.getBucket().getImages(), fileName); } + + + + @Test + void testSize() { + System.out.println(minioUtil.getFileSize("images", "7c9360ba-0cf7-41db-9e11-6c02379e1170.jpg")); + } } \ No newline at end of file