Skip to content

Commit

Permalink
fix download function overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
amakropoulos committed Jan 25, 2024
1 parent 2014c3b commit 18dd89a
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions Runtime/LLMUnitySetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static void makeExecutable(string path)
public static async Task DownloadFile(
string fileUrl, string savePath, bool executable = false,
TaskCallback<string> callback = null, Callback<float> progresscallback = null,
int chunkSize = 1024 * 1024)
long chunkSize = 10 * 1024 * 1024)
{
// download a file to the specified path
if (File.Exists(savePath))
Expand All @@ -100,34 +100,37 @@ public static async Task DownloadFile(
throw new System.Exception("Failed to get file size. Error: " + www.error);

long fileSize = long.Parse(www.GetResponseHeader("Content-Length"));

AssetDatabase.StartAssetEditing();
Directory.CreateDirectory(Path.GetDirectoryName(savePath));
using (FileStream fs = new FileStream(savePath, FileMode.Create, FileAccess.Write))
{
for (long i = 0; i < fileSize; i += chunkSize)
long chunks = (long) Mathf.Ceil((float)fileSize / chunkSize);
for (long i = 0; i < chunks; i++)
{
long startByte = i;
long endByte = (long)Mathf.Min(i + chunkSize - 1, fileSize - 1);
long startByte = i * chunkSize;
long endByte = startByte + chunkSize - 1;
if (endByte > fileSize - 1) endByte = fileSize - 1;

www = UnityWebRequest.Get(fileUrl);
www.SetRequestHeader("Range", "bytes=" + startByte + "-" + endByte);
using (UnityWebRequest wwwChunk = UnityWebRequest.Get(fileUrl))
{
wwwChunk.SetRequestHeader("Range", "bytes=" + startByte + "-" + endByte);

asyncOperation = www.SendWebRequest();
asyncOperation = wwwChunk.SendWebRequest();

while (!asyncOperation.isDone)
{
await Task.Delay(100); // Adjust the delay as needed
}
while (!asyncOperation.isDone)
{
await Task.Delay(1000); // Adjust the delay as needed
}

if (www.result != UnityWebRequest.Result.Success)
throw new System.Exception("Download failed. Error: " + www.error);
if (wwwChunk.result != UnityWebRequest.Result.Success)
throw new System.Exception("Download failed. Error: " + wwwChunk.error);

fs.Write(www.downloadHandler.data, 0, www.downloadHandler.data.Length);
fs.Write(wwwChunk.downloadHandler.data, 0, wwwChunk.downloadHandler.data.Length);

int progressPercentage = Mathf.FloorToInt((float)i / fileSize * 100);
if (progressPercentage % 1 == 0)
progresscallback((float)progressPercentage / 100);
int progressPercentage = Mathf.FloorToInt((float) i / chunks * 100);
if (progressPercentage % 1 == 0)
progresscallback((float)progressPercentage / 100);
}
}
}

Expand Down

0 comments on commit 18dd89a

Please sign in to comment.