fix(video): 优化视频下载逻辑

- 优化 HttpRequestComponent 中的 download 方法,添加资源释放和异常处理
- 在 MadouVideoService 中增加备选 m3u8 地址,提高视频下载成功率
This commit is contained in:
燕陇琪 2024-12-31 00:26:48 +08:00
parent 9eb1fea369
commit fa95c0d9d3
2 changed files with 19 additions and 8 deletions

View File

@ -270,6 +270,9 @@ public class MadouVideoService extends ServiceImpl<MadouVideoMapper, MadouVideoI
if (httpRequestComponent.checkUrl(url)) {
madouVideoInfo.setM3u8Url(url);
}
else {
madouVideoInfo.setM3u8Url("/api/video/" + id + "/index.m3u8");
}
return madouVideoInfo;
}
}

View File

@ -114,17 +114,25 @@ public class HttpRequestComponent {
}
public void download(String url, OutputStream os) throws IOException {
public void download(String url, OutputStream os) {
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
try (CloseableHttpResponse response = closeableHttpClient.execute(httpGet)) {
int statusCode = response.getStatusLine().getStatusCode();
if (HttpStatus.SC_OK != statusCode) {
log.error("文件下载失败,状态码:{}", statusCode);
throw new RuntimeException("文件下载失败");
}
HttpEntity entity = response.getEntity();
IOUtils.copy(entity.getContent(), os);
try (InputStream content = entity.getContent()) {
IOUtils.copy(content, os);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
httpGet.releaseConnection();
}
}
public boolean checkUrl(String url) {