diff --git a/MareSynchronos/Managers/CachedPlayer.cs b/MareSynchronos/Managers/CachedPlayer.cs index fd230a7..e09d3a3 100644 --- a/MareSynchronos/Managers/CachedPlayer.cs +++ b/MareSynchronos/Managers/CachedPlayer.cs @@ -66,7 +66,7 @@ public class CachedPlayer Logger.Debug("Received data for " + this); Logger.Debug("Checking for files to download for player " + PlayerName); - Logger.Debug("Hash for data is " + characterData.GetHashCode()); + Logger.Debug("Hash for data is " + characterData.GetHashCode() + ", current cache hash is " + _cachedData.GetHashCode()); if (characterData.GetHashCode() == _cachedData.GetHashCode()) return; @@ -434,9 +434,11 @@ public class CachedPlayer finally { _cachedData = new(); + var tempPlayerName = PlayerName; PlayerName = string.Empty; PlayerCharacter = IntPtr.Zero; IsVisible = false; + Logger.Debug("Disposing " + tempPlayerName + " complete"); } } diff --git a/MareSynchronos/WebAPI/ApIController.Functions.Files.cs b/MareSynchronos/WebAPI/ApIController.Functions.Files.cs index d376c92..0909305 100644 --- a/MareSynchronos/WebAPI/ApIController.Functions.Files.cs +++ b/MareSynchronos/WebAPI/ApIController.Functions.Files.cs @@ -98,7 +98,7 @@ public partial class ApiController } } - private async Task DownloadFileHttpClient(DownloadFileTransfer fileTransfer, IProgress progress, CancellationToken ct) + private async Task DownloadFileHttpClient(DownloadFileTransfer fileTransfer, string tempPath, IProgress progress, CancellationToken ct) { var requestId = await GetQueueRequest(fileTransfer, ct).ConfigureAwait(false); @@ -124,12 +124,9 @@ public partial class ApiController } } - var fileName = ""; try { - fileName = Path.GetTempFileName(); - - var fileStream = File.Create(fileName); + var fileStream = File.Create(tempPath); await using (fileStream.ConfigureAwait(false)) { var bufferSize = response.Content.Headers.ContentLength > 1024 * 1024 ? 4096 : 1024; @@ -145,8 +142,7 @@ public partial class ApiController progress.Report(bytesRead); } - Logger.Debug($"{requestUrl} downloaded to {fileName}"); - return fileName; + Logger.Debug($"{requestUrl} downloaded to {tempPath}"); } } catch (Exception ex) @@ -154,8 +150,8 @@ public partial class ApiController Logger.Warn($"Error during file download of {requestUrl}", ex); try { - if (!fileName.IsNullOrEmpty()) - File.Delete(fileName); + if (!tempPath.IsNullOrEmpty()) + File.Delete(tempPath); } catch { } throw; @@ -246,18 +242,27 @@ public partial class ApiController file.Transferred += bytesDownloaded; }); - var tempFile = await DownloadFileHttpClient(file, progress, token).ConfigureAwait(false); - if (token.IsCancellationRequested) + var tempPath = Path.Combine(_pluginConfiguration.CacheFolder, file.Hash + ".tmp"); + try { - File.Delete(tempFile); - Logger.Debug("Detetokened cancellation, removing " + currentDownloadId); + await DownloadFileHttpClient(file, tempPath, progress, token).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + File.Delete(tempPath); + Logger.Debug("Detected cancellation, removing " + currentDownloadId); CancelDownload(currentDownloadId); return; } + catch (Exception ex) + { + Logger.Error("Error during download of " + file.Hash, ex); + return; + } - var tempFileData = await File.ReadAllBytesAsync(tempFile, token).ConfigureAwait(false); + var tempFileData = await File.ReadAllBytesAsync(tempPath, token).ConfigureAwait(false); var extratokenedFile = LZ4Codec.Unwrap(tempFileData); - File.Delete(tempFile); + File.Delete(tempPath); var filePath = Path.Combine(_pluginConfiguration.CacheFolder, file.Hash); await File.WriteAllBytesAsync(filePath, extratokenedFile, token).ConfigureAwait(false); var fi = new FileInfo(filePath);