Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[YouTube] Improve download speed #9948

Merged
merged 1 commit into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public void run() {
long lowestSize = Long.MAX_VALUE;

for (int i = 0; i < mMission.urls.length && mMission.running; i++) {
mConn = mMission.openConnection(mMission.urls[i], true, -1, -1);
mConn = mMission.openConnection(mMission.urls[i], true, 0, 0);
mMission.establishConnection(mId, mConn);
dispose();

if (Thread.interrupted()) return;
long length = Utility.getContentLength(mConn);
long length = Utility.getTotalContentLength(mConn);

if (i == 0) {
httpCode = mConn.getResponseCode();
Expand All @@ -84,14 +84,14 @@ public void run() {
}
} else {
// ask for the current resource length
mConn = mMission.openConnection(true, -1, -1);
mConn = mMission.openConnection(true, 0, 0);
mMission.establishConnection(mId, mConn);
dispose();

if (!mMission.running || Thread.interrupted()) return;

httpCode = mConn.getResponseCode();
mMission.length = Utility.getContentLength(mConn);
mMission.length = Utility.getTotalContentLength(mConn);
}

if (mMission.length == 0 || httpCode == 204) {
Expand Down
27 changes: 24 additions & 3 deletions app/src/main/java/us/shandian/giga/util/Utility.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package us.shandian.giga.util;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
Expand All @@ -29,8 +26,10 @@
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.util.Locale;
import java.util.Random;

import okio.ByteString;
import us.shandian.giga.get.DownloadMission;

public class Utility {

Expand Down Expand Up @@ -232,6 +231,28 @@ public static long getContentLength(HttpURLConnection connection) {
return -1;
}

/**
* Get the content length of the entire file even if the HTTP response is partial
* (response code 206).
* @param connection http connection
* @return content length
*/
public static long getTotalContentLength(final HttpURLConnection connection) {
try {
if (connection.getResponseCode() == 206) {
final String rangeStr = connection.getHeaderField("Content-Range");
final String bytesStr = rangeStr.split("/", 2)[1];
return Long.parseLong(bytesStr);
} else {
return getContentLength(connection);
}
} catch (Exception err) {
// nothing to do
}

return -1;
}

private static String pad(int number) {
return number < 10 ? ("0" + number) : String.valueOf(number);
}
Expand Down