Skip to content

Commit 885e251

Browse files
committed
Bugfix
1 parent c276c92 commit 885e251

File tree

3 files changed

+72
-42
lines changed

3 files changed

+72
-42
lines changed

GofileDownloader/App.cs

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,69 +55,95 @@ private static async Task DownloadSingleUrlOption()
5555
{
5656
try
5757
{
58-
// Get URL from user and api response
59-
GofileDataModel gofileData = JsonSerializer.Deserialize<GofileDataModel>(
60-
await GetResponse(ConsoleHelper.PromptUserForUrl()));
58+
// Get URL from user and check if working
59+
string url = ConsoleHelper.PromptUserForUrl();
60+
string response = await GetResponse(url);
61+
GofileDataModel gofileData = JsonSerializer.Deserialize<GofileDataModel>(response);
6162

6263
// Add files to list
6364
List<GofileDataModel.Child> fileList = [];
6465
foreach (var item in gofileData.Data.Children.Values)
6566
{
6667
fileList.Add(item);
6768
}
68-
69-
await DownloadFiles(fileList);
7069

70+
await DownloadFiles(fileList);
7171
}
7272
catch (InvalidInputException ex)
7373
{
7474
ConsoleHelper.WriteError(ex.Message);
75+
return;
76+
}
77+
catch (InvalidTokenException ex)
78+
{
79+
ConsoleHelper.WriteError(ex.Message);
80+
return;
7581
}
7682
}
7783
private static async Task DownloadMultipleUrlOption()
7884
{
79-
// Load Urls from file if theres any
80-
List<string>? urls = UrlHelper.LoadUrlsFromFile();
81-
if (urls == null)
85+
try
8286
{
83-
ConsoleHelper.WriteError("There are no links in your urls.txt file");
84-
return;
85-
}
87+
// Load Urls from file if theres any
88+
List<string>? urls = UrlHelper.LoadUrlsFromFile();
89+
if (urls == null)
90+
{
91+
ConsoleHelper.WriteError("There are no links in your urls.txt file");
92+
return;
93+
}
8694

87-
// Validate loaded Urls
88-
List<string> validUrls = [];
89-
foreach (var url in urls)
90-
{
91-
bool isValid = Regex.Match(url, Constants.Regex.GOFILE_REGEX).Success;
92-
if (isValid)
95+
// Validate loaded Urls
96+
List<string> validUrls = [];
97+
foreach (var url in urls)
9398
{
94-
validUrls.Add(url);
99+
bool isValid = Regex.Match(url, Constants.Regex.GOFILE_REGEX).Success;
100+
if (isValid)
101+
{
102+
validUrls.Add(url);
103+
}
95104
}
96-
}
97105

98-
// Ask user if wants to continue if any of the links were bad
99-
if (urls.Count != validUrls.Count && !AnsiConsole.Confirm($"[red]Only {validUrls.Count} out of {urls.Count} are valid URL's from your urls.txt. Do you wish to continue?[/]"))
100-
{
101-
return;
102-
}
106+
if (validUrls.Count <= 0)
107+
{
108+
ConsoleHelper.WriteError("There are no valid urls in your urls.txt");
109+
return;
110+
}
103111

112+
// Ask user if wants to continue if any of the links were bad
113+
if (urls.Count != validUrls.Count && !AnsiConsole.Confirm($"[red]Only {validUrls.Count} out of {urls.Count} are valid URL's from your urls.txt. Do you wish to continue?[/]"))
114+
{
115+
return;
116+
}
104117

105-
List<GofileDataModel.Child> fileList = [];
106-
foreach (var url in validUrls)
107-
{
108-
// Get API responses from urls
109-
GofileDataModel gofileData = JsonSerializer.Deserialize<GofileDataModel>(
110-
await GetResponse(url));
111118

112-
// Add file download URLs from response
113-
foreach (var item in gofileData.Data.Children.Values)
119+
List<GofileDataModel.Child> fileList = [];
120+
foreach (var url in validUrls)
114121
{
115-
fileList.Add(item);
122+
// Get API responses from urls
123+
GofileDataModel gofileData = JsonSerializer.Deserialize<GofileDataModel>(
124+
await GetResponse(url));
125+
126+
// Add file download URLs from response
127+
foreach (var item in gofileData.Data.Children.Values)
128+
{
129+
fileList.Add(item);
130+
}
131+
116132
}
117133

134+
await DownloadFiles(fileList);
118135
}
119-
120-
await DownloadFiles(fileList);
136+
catch (InvalidInputException ex)
137+
{
138+
ConsoleHelper.WriteError(ex.Message);
139+
return;
140+
}
141+
catch (InvalidTokenException ex)
142+
{
143+
ConsoleHelper.WriteError(ex.Message);
144+
return;
145+
}
146+
121147
}
122148
private static void ChangeToken()
123149
{
@@ -137,17 +163,19 @@ await AnsiConsole.Progress()
137163
.HideCompleted(false)
138164
.Columns(new ProgressColumn[]
139165
{
140-
new TaskDescriptionColumn { Alignment = Justify.Right},
166+
new TaskDescriptionColumn { Alignment = Justify.Left},
141167
new ProgressBarColumn(),
142168
new PercentageColumn(),
143169
new RemainingTimeColumn(),
144-
new SpinnerColumn(),
145170
})
146171
.StartAsync(async ctx =>
147172
{
173+
var maxLength = fileList.Max(file => file.Name.Length);
174+
148175
var tasks = fileList.Select(file =>
149176
{
150-
var progressTask = ctx.AddTask($"[yellow]{file.Name}[/]", autoStart: false);
177+
var paddedName = file.Name.PadRight(maxLength);
178+
var progressTask = ctx.AddTask($"[yellow]{paddedName}[/]", autoStart: false);
151179

152180
return Task.Run(async () =>
153181
{
@@ -178,10 +206,9 @@ private static async Task<string> GetResponse(string url)
178206
{
179207
return await APIHelper.GetDataAsync(url, _config.Token);
180208
}
181-
catch (InvalidTokenException ex)
209+
catch (InvalidTokenException)
182210
{
183-
ConsoleHelper.WriteError(ex.Message);
184-
return string.Empty;
211+
throw;
185212
}
186213
}
187214
}

GofileDownloader/Helpers/ConfigHelper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using GofileDownloader.Models;
22
using GofileDownloader.Utils;
3+
using Spectre.Console;
34
using System.Runtime.Serialization;
45
using System.Text.Json;
56

@@ -51,6 +52,8 @@ private static bool IsConfigFileExists()
5152
public static void GenerateConfigFile()
5253
{
5354
Config config = new() { Token = ConsoleHelper.PromptUserForToken() };
55+
AnsiConsole.Clear();
56+
5457
string json = JsonSerializer.Serialize(config, jsonSerOptions);
5558
File.WriteAllText(Constants.Paths.CONFIG_PATH, json);
5659
}

GofileDownloader/Helpers/UrlHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private static bool IsUrlFileExists()
2929

3030
private static void GenerateUrlFile()
3131
{
32-
File.Create(Constants.Paths.URLS_PATH);
32+
using FileStream fs = File.Create(Constants.Paths.URLS_PATH);
3333
}
3434
}
3535
}

0 commit comments

Comments
 (0)