feat(video): 优化视频 URL检验和获取逻辑

- 在 HttpRequestComponent 中添加 checkUrl 方法,用于检查 URL 是否有效
- 在 MadouVideoService 中实现 getMadouVideoInfo 方法,整合视频信息获取和 URL 检查
- 修改 VideoController 中的 getMadouVideoById 方法,使用新的视频信息获取逻辑
This commit is contained in:
燕陇琪 2024-12-30 22:46:33 +08:00
parent fcd88baf67
commit dd1a20ec9b
3 changed files with 39 additions and 2 deletions

View File

@ -44,8 +44,7 @@ public class VideoController {
@GetMapping("/madou/{id}")
public JsonResult<MadouVideoInfo> getMadouVideoById(@PathVariable("id") Long id) {
MadouVideoInfo madouVideoInfo = madouVideoService.getById(id);
madouVideoInfo.setM3u8Url("/api/video/" + id + "/index.m3u8");
MadouVideoInfo madouVideoInfo = madouVideoService.getMadouVideoInfo(id);
return JsonResult.ok(madouVideoInfo);
}

View File

@ -260,4 +260,16 @@ public class MadouVideoService extends ServiceImpl<MadouVideoMapper, MadouVideoI
}
httpRequestComponent.download(videoInfo.getM3u8Url().replace("index.m3u8", ts), httpServletResponse.getOutputStream());
}
public MadouVideoInfo getMadouVideoInfo(Long id) {
MadouVideoInfo madouVideoInfo = getById(id);
if (madouVideoInfo == null) {
throw new RuntimeException("视频不存在");
}
String url = "https://video.yuchat.top/m3u8/" + id + "/index.m3u8";
if (httpRequestComponent.checkUrl(url)) {
madouVideoInfo.setM3u8Url(url);
}
return madouVideoInfo;
}
}

View File

@ -3,6 +3,7 @@ package top.yuchat.crawler.video.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
@ -126,6 +127,31 @@ public class HttpRequestComponent {
IOUtils.copy(entity.getContent(), os);
}
public boolean checkUrl(String url) {
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = null;
try {
response = closeableHttpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (HttpStatus.SC_OK == statusCode) {
return true;
}
} catch (Exception ignored) {
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error("Failed to close response", e);
}
}
// 关闭 httpGet 请求
httpGet.releaseConnection();
}
return false;
}
public long getFileSize(String url) throws IOException {
HttpGet httpGet = new HttpGet(url);