Skip to content

Commit 18d8027

Browse files
committed
Fix Some Bugs
1 parent 56e2438 commit 18d8027

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

dev/ViewModels/Qari/DownloadQariViewModel.cs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.ObjectModel;
2-
2+
using System.Text.RegularExpressions;
3+
using ColorCode.Compilation.Languages;
34
using Downloader;
4-
55
using Newtonsoft.Json;
66

77
namespace AlAnvar.ViewModels;
@@ -50,7 +50,7 @@ private async void OnPageLoaded()
5050
await Task.Run(async () =>
5151
{
5252
using var db = new AlAnvarDBContext();
53-
var data = await db.Audios.OrderBy(x=>x.Name).ToListAsync();
53+
var data = await db.Audios.OrderBy(x => x.Name).ToListAsync();
5454
QuranAudios = new(data);
5555
dispatcherQueue.TryEnqueue(() =>
5656
{
@@ -137,8 +137,18 @@ private async void DownloadQariAsync()
137137
}
138138
else
139139
{
140-
var audioUrl = Path.Combine(audioItem.Url, id);
141-
var audioFilePath = Path.Combine(dirPath, id);
140+
var audioUrlPath = id;
141+
if (audioItem.Url.EndsWith("/") && audioUrlPath.StartsWith("/"))
142+
{
143+
audioUrlPath = audioUrlPath.Remove(0, 1);
144+
}
145+
else if (!audioItem.Url.EndsWith("/") && !audioUrlPath.StartsWith("/"))
146+
{
147+
audioUrlPath = audioUrlPath.Insert(0, "/");
148+
}
149+
150+
var finalAudioUrlPath = Path.Combine(audioItem.Url, audioUrlPath);
151+
var audioFilePath = Path.Combine(dirPath, finalAudioUrlPath);
142152
if (File.Exists(audioFilePath))
143153
{
144154
_downloadedtItemCount += 1;
@@ -149,7 +159,7 @@ private async void DownloadQariAsync()
149159
downloadService = new DownloadService();
150160
downloadService.DownloadProgressChanged += Downloader_DownloadProgressChanged;
151161
downloadService.DownloadFileCompleted += Downloader_DownloadFileCompleted;
152-
await downloadService.DownloadFileTaskAsync(audioUrl, new DirectoryInfo(dirPath));
162+
await downloadService.DownloadFileTaskAsync(finalAudioUrlPath, new DirectoryInfo(dirPath));
153163
}
154164
}
155165
}
@@ -191,18 +201,28 @@ private void Downloader_DownloadProgressChanged(object sender, DownloadProgressC
191201
private List<string> GetAudioIds(string fileName)
192202
{
193203
List<string> quranAudioIds = new List<string>();
194-
using var streamReader = File.OpenText(fileName);
195-
string line = String.Empty;
196-
while ((line = streamReader.ReadLine()) != null)
204+
205+
string content;
206+
using (StreamReader sr = new StreamReader(fileName))
197207
{
198-
if (line.Contains("href"))
199-
{
200-
var audioId = line.Replace("<a href=\"", "");
201-
var lastIndex = audioId.IndexOf("\">");
202-
audioId = audioId.Substring(0, lastIndex);
203-
quranAudioIds.Add(audioId);
204-
}
208+
content = sr.ReadToEnd();
205209
}
210+
211+
if (string.IsNullOrEmpty(content))
212+
{
213+
return null;
214+
}
215+
216+
string pattern = "href=\"(.*?\\.mp3)\"";
217+
Regex regex = new Regex(pattern);
218+
219+
foreach (Match match in regex.Matches(content))
220+
{
221+
string mp3Link = match.Groups[1].Value;
222+
mp3Link = Path.GetFileName(mp3Link);
223+
quranAudioIds.Add(mp3Link);
224+
}
225+
206226
return quranAudioIds;
207227
}
208228

0 commit comments

Comments
 (0)