Administrator
20 hours ago cbdf8c392064125ef90738a73d1e65afcf27fe77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package cc.mrbird.febs.ai.controller.productPoint;
 
import cc.mrbird.febs.ai.req.memberTalkStream.ApiMemberTalkReloadStreamDto;
import cc.mrbird.febs.ai.req.product.ApiProductInfoDto;
import cc.mrbird.febs.ai.req.product.ApiProductPageDto;
import cc.mrbird.febs.ai.req.productPoint.ApiProductPointInfoDto;
import cc.mrbird.febs.ai.req.productPoint.ApiProductPointPageDto;
import cc.mrbird.febs.ai.req.productPoint.ApiProductPointRecommendDto;
import cc.mrbird.febs.ai.res.product.ApiProductInfoVo;
import cc.mrbird.febs.ai.res.product.ApiProductVo;
import cc.mrbird.febs.ai.res.productPoint.ApiProductPointInfoVo;
import cc.mrbird.febs.ai.res.productPoint.ApiProductPointListVo;
import cc.mrbird.febs.ai.res.productPoint.ApiProductPointRecommendVo;
import cc.mrbird.febs.ai.service.AiProductPointService;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.utils.LoginUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
 
/**
 * @author Administrator
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/api/ai/productPoint")
@Api(value = "ApiProductPointController", tags = "AI-知识点(学习)")
public class ApiProductPointController {
 
    private final AiProductPointService aiProductPointService;
 
    @ApiOperation(value = "知识点列表", notes = "知识点列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "success", response = ApiProductPointListVo.class)
    })
    @PostMapping(value = "/list")
    public FebsResponse list(@RequestBody @Validated ApiProductPointPageDto dto) {
 
        return aiProductPointService.productPointList(dto);
    }
 
    @ApiOperation(value = "知识点详情", notes = "知识点详情")
    @ApiResponses({
            @ApiResponse(code = 200, message = "success", response = ApiProductPointInfoVo.class)
    })
    @PostMapping(value = "/info")
    public FebsResponse info(@RequestBody @Validated ApiProductPointInfoDto dto) {
 
        return aiProductPointService.productPointInfo(dto);
    }
 
 
    @ApiOperation(value = "推荐", notes = "推荐")
    @ApiResponses({
            @ApiResponse(code = 200, message = "success", response = ApiProductPointRecommendVo.class)
    })
    @PostMapping(value = "/recommend")
    public FebsResponse recommend(@RequestBody @Validated ApiProductPointRecommendDto dto) {
 
        return aiProductPointService.recommend(dto);
    }
 
 
    public static final String baseUploadDir = "/home/javaweb/webresource/ai/file";
    /**
     * 播放视频文件
     */
    @GetMapping("/play/{fileName}")
    public void playVideo(@PathVariable("fileName") String fileName,
                          HttpServletRequest request,
                          HttpServletResponse response) {
        try {
            String companyId = LoginUserUtil.getLoginUser().getCompanyId();
            String uploadDir = baseUploadDir + "/" + companyId;
            Path filePath = Paths.get(uploadDir, fileName);
            if (!Files.exists(filePath)) {
                response.setStatus(HttpStatus.NOT_FOUND.value());
                return;
            }
 
            // 获取文件大小
            long fileSize = Files.size(filePath);
 
            // 检查是否有Range请求头
            String rangeHeader = request.getHeader("Range");
            if (rangeHeader != null) {
                // 解析Range头,格式为"bytes=start-end"
                String[] ranges = rangeHeader.replace("bytes=", "").split("-");
                long start = 0, end = fileSize - 1;
 
                if (!ranges[0].isEmpty()) {
                    start = Long.parseLong(ranges[0]);
                }
                if (ranges.length > 1 && !ranges[1].isEmpty()) {
                    end = Long.parseLong(ranges[1]);
                } else {
                    end = fileSize - 1;
                }
 
                // 确保范围有效
                if (start > end || start >= fileSize) {
                    response.setStatus(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE.value());
                    return;
                }
 
                // 限制结束位置不超过文件大小
                if (end >= fileSize) {
                    end = fileSize - 1;
                }
 
                // 计算内容长度
                long contentLength = end - start + 1;
 
                // 设置响应状态和头部信息
                response.setStatus(HttpStatus.PARTIAL_CONTENT.value());
                response.setContentType(getContentTypeByExtension(fileName));
                response.setContentLengthLong(contentLength);
                response.setHeader("Content-Range", "bytes " + start + "-" + end + "/" + fileSize);
                response.setHeader("Accept-Ranges", "bytes");
                response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
 
                // 从指定位置开始读取文件
                try (RandomAccessFile randomAccessFile = new RandomAccessFile(filePath.toFile(), "r");
                     OutputStream outputStream = response.getOutputStream()) {
 
                    randomAccessFile.seek(start);
                    byte[] buffer = new byte[8192];
                    long remaining = contentLength;
                    int bytesRead;
 
                    while (remaining > 0 && (bytesRead = randomAccessFile.read(buffer, 0, (int) Math.min(buffer.length, remaining))) != -1) {
                        outputStream.write(buffer, 0, bytesRead);
                        remaining -= bytesRead;
                    }
                }
            } else {
 
                // 没有Range头,返回前30秒内容(如果可能的话)
                // 首先尝试估计前30秒对应的字节数(假设平均比特率为5 Mbps)
                long estimatedFirst30Seconds = (long) (5 * 1024 * 1024 / 8 * 30); // 5 Mbps => bytes for 30 seconds
                long actualEnd = Math.min(estimatedFirst30Seconds, fileSize - 1);
 
                // 设置响应状态和头部信息
                response.setStatus(HttpStatus.PARTIAL_CONTENT.value()); // 使用206状态码,因为只返回部分内容
                response.setContentType(getContentTypeByExtension(fileName));
                response.setContentLengthLong(actualEnd + 1);
                response.setHeader("Content-Range", "bytes 0-" + actualEnd + "/" + fileSize);
                response.setHeader("Accept-Ranges", "bytes");
                response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
 
                // 流式传输文件的前30秒
                try (InputStream inputStream = Files.newInputStream(filePath);
                     OutputStream outputStream = response.getOutputStream()) {
                    byte[] buffer = new byte[8192];
                    long written = 0;
                    int bytesRead;
 
                    while (written <= actualEnd && (bytesRead = inputStream.read(buffer)) != -1) {
                        long toWrite = Math.min(bytesRead, (int)(actualEnd + 1 - written));
                        outputStream.write(buffer, 0, (int)toWrite);
                        written += toWrite;
 
                        // 如果已经写完前30秒的内容,则停止
                        if (written > actualEnd) {
                            break;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
    }
 
    /**
     * 根据文件扩展名获取MIME类型
     */
    private String getContentTypeByExtension(String fileName) {
        String extension = "";
        int lastDotIndex = fileName.lastIndexOf('.');
        if (lastDotIndex > 0) {
            extension = fileName.substring(lastDotIndex).toLowerCase();
        }
 
        switch (extension) {
            case ".mp4":
                return "video/mp4";
            case ".avi":
                return "video/x-msvideo";
            case ".mov":
                return "video/quicktime";
            case ".wmv":
                return "video/x-ms-wmv";
            case ".flv":
                return "video/x-flv";
            case ".webm":
                return "video/webm";
            case ".mkv":
                return "video/x-matroska";
            case ".m3u8":
                return "application/vnd.apple.mpegurl";
            case ".ts":
                return "video/MP2T";
            default:
                return "application/octet-stream";
        }
    }
 
}