Skip to content

Commit fc9586f

Browse files
authored
Merge pull request #176 from jitwxs/dev
Supplement For Release v6.0
2 parents 464cc37 + 0c77efd commit fc9586f

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

MusicLyricApp/MainForm.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ private async void BatchSave()
654654
UpdateLrcTextBox(log.ToString());
655655
}
656656

657-
private Task<bool> BatchSaveForUrl(string filePath, ISet<string> success)
657+
private async Task<bool> BatchSaveForUrl(string filePath, ISet<string> success)
658658
{
659659
var csvBean = CsvBean.Deserialization(Console_TextBox.Text);
660660

@@ -674,7 +674,7 @@ private Task<bool> BatchSaveForUrl(string filePath, ISet<string> success)
674674

675675
if (idIndex == -1 || urlIndex == -1)
676676
{
677-
return Task.FromResult(false);
677+
return false;
678678
}
679679

680680
foreach (var line in csvBean.Lines)
@@ -692,19 +692,14 @@ private Task<bool> BatchSaveForUrl(string filePath, ISet<string> success)
692692
}
693693

694694
var path = filePath + '/' + GlobalUtils.GetOutputName(saveVo, _globalSearchInfo.SettingBean.Config.OutputFileNameFormat) + GlobalUtils.GetSuffix(url);
695-
696-
try
695+
696+
if (await HttpUtils.DownloadFile(url, path))
697697
{
698-
HttpUtils.DownloadFile(url, path);
699698
success.Add(id);
700699
}
701-
catch (System.Exception)
702-
{
703-
// ignored
704-
}
705700
}
706701

707-
return Task.FromResult(true);
702+
return true;
708703
}
709704

710705
private async Task WriteToFile(string path, LyricVo lyricVo)

MusicLyricApp/Utils/GlobalUtils.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,27 @@ public static Encoding GetEncoding(OutputEncodingEnum encodingEnum)
324324

325325
public static string GetSuffix(string str)
326326
{
327-
return str.Substring(str.LastIndexOf(".", StringComparison.Ordinal));
327+
var a = str.LastIndexOf("/", StringComparison.Ordinal);
328+
if (a != -1)
329+
{
330+
str = str.Substring(a + 1);
331+
}
332+
333+
var b = str.IndexOf("?", StringComparison.Ordinal);
334+
if (b != -1)
335+
{
336+
str = str.Substring(0, b);
337+
}
338+
339+
var c = str.LastIndexOf(".", StringComparison.Ordinal);
340+
if (c == -1)
341+
{
342+
return "";
343+
}
344+
else
345+
{
346+
return str.Substring(c);
347+
}
328348
}
329349

330350
public static int toInt(string str, int defaultValue)

MusicLyricApp/Utils/HttpUtils.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,25 @@ public static async Task<T> HttpGetAsync<T>(string url, string contentType = "ap
172172
return res.ToEntity<T>();
173173
}
174174

175-
public static async void DownloadFile(string url, string path)
175+
public static async Task<bool> DownloadFile(string url, string path)
176176
{
177-
var client = new HttpClient();
178-
var s = await client.GetStreamAsync(url);
179-
var fs = new FileStream(path, FileMode.OpenOrCreate);
180-
await s.CopyToAsync(fs);
177+
try
178+
{
179+
var client = new HttpClient();
180+
181+
using (var s = await client.GetStreamAsync(url))
182+
using(var fs = new FileStream(path, FileMode.OpenOrCreate))
183+
{
184+
await s.CopyToAsync(fs);
185+
}
186+
187+
return true;
188+
}
189+
catch (System.Exception)
190+
{
191+
// ignore exception
192+
return false;
193+
}
181194
}
182195
}
183196
}

0 commit comments

Comments
 (0)