Skip to content

Commit

Permalink
- Fix GoFile.io URLs file upload retry
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaBear84 committed Jul 13, 2020
1 parent 884e85e commit 0f70f9e
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions OpenDirectoryDownloader/FileUpload/GoFileIo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
using OpenDirectoryDownloader.Models;
using System;
using System.IO;
using System.Net.Http;
Expand All @@ -14,30 +15,30 @@ public class GoFileIo

public static async Task<GoFilesFile> UploadFile(HttpClient httpClient, string path)
{
string jsonServer = await httpClient.GetStringAsync("https://apiv2.gofile.io/getServer");
int retries = 0;
int maxRetries = 5;

JObject result = JObject.Parse(jsonServer);

if (result["status"].Value<string>() == "error")
while (retries < maxRetries)
{
throw new Exception("GoFile.io error, probably in maintenance");
}
try
{
string jsonServer = await httpClient.GetStringAsync("https://apiv2.gofile.io/getServer");

string server = result.SelectToken("data.server").Value<string>();
JObject result = JObject.Parse(jsonServer);

using (MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent($"Upload----{Guid.NewGuid()}"))
{
multipartFormDataContent.Add(new StreamContent(new FileStream(path, FileMode.Open)), "filesUploaded", Path.GetFileName(path));
multipartFormDataContent.Add(new StringContent("file"), "category");
multipartFormDataContent.Add(new StringContent("0"), "category");
if (result["status"].Value<string>() == "error")
{
throw new Exception("GoFile.io error, probably in maintenance");
}

int i = 0;
int retries = 5;
string server = result.SelectToken("data.server").Value<string>();

while (i < retries)
{
try
using (MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent($"Upload----{Guid.NewGuid()}"))
{
multipartFormDataContent.Add(new StreamContent(new FileStream(path, FileMode.Open)), "filesUploaded", Path.GetFileName(path));
multipartFormDataContent.Add(new StringContent("file"), "category");
multipartFormDataContent.Add(new StringContent("0"), "category");

using (HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"https://{server}.gofile.io/upload", multipartFormDataContent))
{
if (httpResponseMessage.IsSuccessStatusCode)
Expand All @@ -56,16 +57,16 @@ public static async Task<GoFilesFile> UploadFile(HttpClient httpClient, string p
}
}
}
catch (Exception)
{
retries++;
Logger.Error($"Error uploading file... Retry in 5 seconds!!!");
await Task.Delay(TimeSpan.FromSeconds(5));
}
}

throw new Exception("Error uploading Urls file...");
catch (Exception)
{
retries++;
Logger.Error($"Error uploading file... Retry in 5 seconds!!!");
await Task.Delay(TimeSpan.FromSeconds(5));
}
}

throw new FriendlyException("Error uploading URLs");
}
}

Expand Down

0 comments on commit 0f70f9e

Please sign in to comment.