Skip to content

Commit

Permalink
Merge pull request #176 from jitwxs/dev
Browse files Browse the repository at this point in the history
Supplement For Release v6.0
  • Loading branch information
jitwxs committed May 14, 2023
2 parents 464cc37 + 0c77efd commit fc9586f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
15 changes: 5 additions & 10 deletions MusicLyricApp/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ private async void BatchSave()
UpdateLrcTextBox(log.ToString());
}

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

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

if (idIndex == -1 || urlIndex == -1)
{
return Task.FromResult(false);
return false;
}

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

var path = filePath + '/' + GlobalUtils.GetOutputName(saveVo, _globalSearchInfo.SettingBean.Config.OutputFileNameFormat) + GlobalUtils.GetSuffix(url);
try

if (await HttpUtils.DownloadFile(url, path))
{
HttpUtils.DownloadFile(url, path);
success.Add(id);
}
catch (System.Exception)
{
// ignored
}
}

return Task.FromResult(true);
return true;
}

private async Task WriteToFile(string path, LyricVo lyricVo)
Expand Down
22 changes: 21 additions & 1 deletion MusicLyricApp/Utils/GlobalUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,27 @@ public static Encoding GetEncoding(OutputEncodingEnum encodingEnum)

public static string GetSuffix(string str)
{
return str.Substring(str.LastIndexOf(".", StringComparison.Ordinal));
var a = str.LastIndexOf("/", StringComparison.Ordinal);
if (a != -1)
{
str = str.Substring(a + 1);
}

var b = str.IndexOf("?", StringComparison.Ordinal);
if (b != -1)
{
str = str.Substring(0, b);
}

var c = str.LastIndexOf(".", StringComparison.Ordinal);
if (c == -1)
{
return "";
}
else
{
return str.Substring(c);
}
}

public static int toInt(string str, int defaultValue)
Expand Down
23 changes: 18 additions & 5 deletions MusicLyricApp/Utils/HttpUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,25 @@ public static async Task<T> HttpGetAsync<T>(string url, string contentType = "ap
return res.ToEntity<T>();
}

public static async void DownloadFile(string url, string path)
public static async Task<bool> DownloadFile(string url, string path)
{
var client = new HttpClient();
var s = await client.GetStreamAsync(url);
var fs = new FileStream(path, FileMode.OpenOrCreate);
await s.CopyToAsync(fs);
try
{
var client = new HttpClient();

using (var s = await client.GetStreamAsync(url))
using(var fs = new FileStream(path, FileMode.OpenOrCreate))
{
await s.CopyToAsync(fs);
}

return true;
}
catch (System.Exception)
{
// ignore exception
return false;
}
}
}
}

0 comments on commit fc9586f

Please sign in to comment.