From 162934d62e463f20f9875e889b8e33aad588ef02 Mon Sep 17 00:00:00 2001 From: Marco Hajek Date: Fri, 29 Sep 2023 21:56:37 +0200 Subject: [PATCH] [delete-map] adds deletion of map file --- src/EvoSC.Common/Services/MapService.cs | 20 +++++++++++++++++++ .../Services/MapServiceTests.cs | 4 +++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/EvoSC.Common/Services/MapService.cs b/src/EvoSC.Common/Services/MapService.cs index 34dc5c5a9..7d9ca2bf8 100644 --- a/src/EvoSC.Common/Services/MapService.cs +++ b/src/EvoSC.Common/Services/MapService.cs @@ -93,6 +93,26 @@ public async Task> AddMapsAsync(List mapStreams) public async Task RemoveMapAsync(long mapId) { await _mapRepository.RemoveMapAsync(mapId); + var map = await this.GetMapByIdAsync(mapId); + + if (map == null) return; + + var filePath = Path.Combine(_config.Path.Maps, "EvoSC", map.FilePath); + if (!File.Exists(filePath)) + { + _logger.LogWarning("Tried to delete map which doesn't exists on the filesystem: {filePath}", filePath); + return; + } + + try + { + File.Delete(filePath); + } + catch (Exception e) + { + _logger.LogError(e, "An exception occurred while trying to delete a map file."); + throw; + } } public async Task AddCurrentMapListAsync() diff --git a/tests/EvoSC.Common.Tests/Services/MapServiceTests.cs b/tests/EvoSC.Common.Tests/Services/MapServiceTests.cs index 06ba598b3..aca8740b6 100644 --- a/tests/EvoSC.Common.Tests/Services/MapServiceTests.cs +++ b/tests/EvoSC.Common.Tests/Services/MapServiceTests.cs @@ -332,10 +332,12 @@ public async Task Remove_Map_Removes_Map() Id = 123, ExternalId = "1337", Name = "snippens dream", - Uid = "Uid" + Uid = "Uid", + FilePath = "FilePath" }; _mapRepository.Setup(m => m.GetMapByIdAsync(It.IsAny())) .Returns(Task.FromResult((IMap?)map)); + _config.SetupGet(config => config.Path.Maps).Returns("MapPath"); await _mapService.RemoveMapAsync(123);