feat(video): 优化视频 URL检验和获取逻辑
- 在 HttpRequestComponent 中添加 checkUrl 方法,用于检查 URL 是否有效 - 在 MadouVideoService 中实现 getMadouVideoInfo 方法,整合视频信息获取和 URL 检查 - 修改 VideoController 中的 getMadouVideoById 方法,使用新的视频信息获取逻辑
This commit is contained in:
parent
fcd88baf67
commit
dd1a20ec9b
@ -44,8 +44,7 @@ public class VideoController {
|
|||||||
|
|
||||||
@GetMapping("/madou/{id}")
|
@GetMapping("/madou/{id}")
|
||||||
public JsonResult<MadouVideoInfo> getMadouVideoById(@PathVariable("id") Long id) {
|
public JsonResult<MadouVideoInfo> getMadouVideoById(@PathVariable("id") Long id) {
|
||||||
MadouVideoInfo madouVideoInfo = madouVideoService.getById(id);
|
MadouVideoInfo madouVideoInfo = madouVideoService.getMadouVideoInfo(id);
|
||||||
madouVideoInfo.setM3u8Url("/api/video/" + id + "/index.m3u8");
|
|
||||||
return JsonResult.ok(madouVideoInfo);
|
return JsonResult.ok(madouVideoInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -260,4 +260,16 @@ public class MadouVideoService extends ServiceImpl<MadouVideoMapper, MadouVideoI
|
|||||||
}
|
}
|
||||||
httpRequestComponent.download(videoInfo.getM3u8Url().replace("index.m3u8", ts), httpServletResponse.getOutputStream());
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package top.yuchat.crawler.video.utils;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpStatus;
|
import org.apache.http.HttpStatus;
|
||||||
|
import org.apache.http.client.ClientProtocolException;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
@ -126,6 +127,31 @@ public class HttpRequestComponent {
|
|||||||
IOUtils.copy(entity.getContent(), os);
|
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 {
|
public long getFileSize(String url) throws IOException {
|
||||||
HttpGet httpGet = new HttpGet(url);
|
HttpGet httpGet = new HttpGet(url);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user