Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Dec 27, 2024
1 parent 887cb02 commit 5a31d6e
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions JL.Core/Dicts/DictUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ internal static async Task<bool> DownloadDict(string dictPath, Uri dictDownloadU
{
using HttpRequestMessage request = new(HttpMethod.Get, dictDownloadUri);

string fullPath = Path.GetFullPath(dictPath, Utils.ApplicationPath);
if (File.Exists(fullPath))
string fullDictPath = Path.GetFullPath(dictPath, Utils.ApplicationPath);
if (File.Exists(fullDictPath))
{
request.Headers.IfModifiedSince = File.GetLastWriteTime(fullPath);
request.Headers.IfModifiedSince = File.GetLastWriteTime(fullDictPath);
}

if (!noPrompt)
Expand All @@ -37,11 +37,14 @@ internal static async Task<bool> DownloadDict(string dictPath, Uri dictDownloadU
if (response.IsSuccessStatusCode)
{
Stream responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
string tempDictPath = GetTempFilePath(fullDictPath);
await using (responseStream.ConfigureAwait(false))
{
await DecompressGzipStream(responseStream, fullPath).ConfigureAwait(false);
await DecompressGzipStream(responseStream, tempDictPath).ConfigureAwait(false);
}

File.Move(tempDictPath, fullDictPath, true);

if (!noPrompt)
{
Utils.Frontend.ShowOkDialog($"{dictName} has been downloaded successfully.", "Info");
Expand Down Expand Up @@ -72,6 +75,12 @@ internal static async Task<bool> DownloadDict(string dictPath, Uri dictDownloadU
{
Utils.Frontend.ShowOkDialog($"Unexpected error while downloading {dictName}.", "Info");
Utils.Logger.Error(ex, "Unexpected error while downloading {DictName}", dictName);

string tempDictPath = GetTempFilePath(Path.GetFullPath(dictPath, Utils.ApplicationPath));
if (File.Exists(tempDictPath))
{
File.Delete(tempDictPath);
}
}

return false;
Expand Down Expand Up @@ -296,4 +305,9 @@ internal static Task AutoUpdateBuiltInDicts()
return Task.CompletedTask;
}

private static string GetTempFilePath(string filePath)
{
return $"{filePath}.tmp";
}

}

0 comments on commit 5a31d6e

Please sign in to comment.