Improve recording download from server

This commit is contained in:
0xboobface 2019-12-29 15:39:44 +01:00
parent 77e0a0737a
commit e0f494d6da
1 changed files with 16 additions and 14 deletions

View File

@ -22,6 +22,7 @@ import ctbrec.Hmac;
import ctbrec.Model;
import ctbrec.OS;
import ctbrec.io.HttpClient;
import ctbrec.io.HttpException;
import ctbrec.io.StreamRedirectThread;
import ctbrec.recorder.ProgressListener;
import ctbrec.recorder.RecordingManager;
@ -150,21 +151,22 @@ public class MergedHlsDownload extends HlsDownload {
private void downloadFile(String fileUri, File tempDir) throws IOException {
LOG.trace("Downloading file {} to {}", fileUri, tempDir);
Request request = new Request.Builder().url(fileUri).addHeader("connection", "keep-alive").build();
Response response = client.execute(request);
InputStream in = null;
File file = new File(request.url().encodedPath());
try (FileOutputStream fos = new FileOutputStream(new File(tempDir, file.getName()))) {
in = response.body().byteStream();
byte[] b = new byte[1024 * 100];
int length = -1;
while ((length = in.read(b)) >= 0) {
fos.write(b, 0, length);
try (Response response = client.execute(request)) {
if (response.isSuccessful()) {
InputStream in = null;
File file = new File(request.url().encodedPath());
File downloadedSegment = new File(tempDir, file.getName());
try (FileOutputStream fos = new FileOutputStream(downloadedSegment)) {
in = response.body().byteStream();
byte[] b = new byte[1024 * 100];
int length = -1;
while ((length = in.read(b)) >= 0) {
fos.write(b, 0, length);
}
}
} else {
throw new HttpException(response.code(), response.message());
}
} finally {
if (in != null) {
in.close();
}
response.close();
}
}