Skip to content

Commit c9150af

Browse files
committed
Fix Audiobook bugs
1 parent 2918ac1 commit c9150af

File tree

3 files changed

+61
-16
lines changed

3 files changed

+61
-16
lines changed

OpenMediaServer.Test/Services/DiscoveryAudiobookServiceShould.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public DiscoveryAudiobookServiceShould()
4343
[Theory]
4444
[InlineData("/media/Audiobooks/Quality Land/QualityLand 2.0 Kikis Geheimnis/QualityLand 2.0 Kikis Geheimnis.m4b", "QualityLand 2.0 Kikis Geheimnis", "/media/Audiobooks/Quality Land/QualityLand 2.0 Kikis Geheimnis")]
4545
[InlineData("/media/Audiobooks/Das Känguru-Manifest/Das Känguru-Manifest.m4b", "Das Känguru-Manifest", "/media/Audiobooks/Das Känguru-Manifest")]
46+
[InlineData("/media/Audiobooks/The Witcher/Ödets Svärd.mp3", "Ödets Svärd", "/media/Audiobooks/The Witcher")]
4647
public async Task Create_FirstItemBook(string path, string title, string? folderPath)
4748
{
4849
// Arrange
@@ -67,5 +68,33 @@ public async Task Create_FirstItemBook(string path, string title, string? folder
6768
versions.First().Id.ShouldNotBe(Guid.Empty);
6869
versions.First().Path.ShouldBe(path);
6970
resultItem.FolderPath.ShouldBe(folderPath);
71+
}
72+
73+
[Theory]
74+
[InlineData("/media/Audiobooks/Asterix bei den Schweizern/Track 1.wav", "Asterix bei den Schweizern", "/media/Audiobooks/Asterix bei den Schweizern/")]
75+
public async Task MultiPart(string path, string title, string? folderPath)
76+
{
77+
// Arrange
78+
var paths = new List<string>
79+
{
80+
path
81+
};
82+
83+
// Act
84+
await _inventoryBookService.CreateAudiobook(path);
85+
var resultJson = _storageRepository.WrittenObjects.First();
86+
var resultItem = JsonSerializer.Deserialize<InventoryItem>(resultJson, Globals.JsonOptions);
87+
var versions = await _versionService.List();
88+
89+
// Assert
90+
resultItem.Id.ShouldNotBe(Guid.Empty);
91+
resultItem.Title.ShouldBe(title);
92+
resultItem.Category.ShouldBe("Audiobook");
93+
resultItem.MetadataId.ShouldNotBe(Guid.Empty);
94+
versions.ShouldNotBeNull();
95+
versions.Count().ShouldBe(1);
96+
versions.First().Id.ShouldNotBe(Guid.Empty);
97+
versions.First().Path.ShouldBe(folderPath);
98+
resultItem.FolderPath.ShouldBe(folderPath);
7099
}
71100
}

OpenMediaServer/Services/Discovery/ContentDiscoveryService.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public class ContentDiscoveryService(
1515
IAddonService addonService,
1616
IFileInfoService fileInfo,
1717
IDiscoveryAudiobookService audiobookDiscoveryService,
18-
IVersionService versionService) : IContentDiscoveryService
18+
IVersionService versionService,
19+
IPartService partService) : IContentDiscoveryService
1920
{
2021
private FileSystemWatcher? _watcher;
2122
private readonly SemaphoreSlim _semaphore = new(1, 1);
@@ -308,9 +309,32 @@ private async Task HandleDelete(IEnumerable<string> paths, IEnumerable<Inventory
308309
{
309310
if (!paths.Contains(version.Path))
310311
{
311-
await versionService.Delete(version.Id);
312-
313-
await fileInfo.DeleteFileInfo(version.FileInfoId);
312+
var parts = await partService.ListItems(i => i.InventoryItemVersionId == version.Id);
313+
314+
if (parts != null)
315+
{
316+
foreach (var part in parts)
317+
{
318+
if (!paths.Contains(part.Path))
319+
{
320+
await partService.DeletePart(part.Id);
321+
await fileInfo.DeleteFileInfo(part.FileInfoId);
322+
}
323+
}
324+
325+
parts = await partService.ListItems(i => i.InventoryItemVersionId == version.Id);
326+
327+
if (!parts.Any())
328+
{
329+
await versionService.Delete(version.Id);
330+
}
331+
}
332+
else
333+
{
334+
await versionService.Delete(version.Id);
335+
336+
await fileInfo.DeleteFileInfo(version.FileInfoId);
337+
}
314338
}
315339
}
316340

OpenMediaServer/Services/Discovery/DiscoveryAudiobookService.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,8 @@ public async Task CreateAudiobook(string path)
6060
}
6161

6262
var books = await inventoryService.ListItems("Audiobook");
63-
var existingVersion = (await versionService.List(i => i.Path == path) ?? []).FirstOrDefault();
64-
var existingVersionId = existingVersion?.Id;
65-
6663
InventoryItem? existingBook = null;
67-
68-
if (existingVersionId != null)
69-
{
70-
existingBook = await inventoryService.GetItem(existingVersionId);
71-
}
72-
73-
64+
7465
string? folderPath = null;
7566

7667
if (!string.IsNullOrEmpty(folderTitle))
@@ -84,6 +75,8 @@ public async Task CreateAudiobook(string path)
8475

8576
existingBook = books?.Where(i => i.FolderPath == folderPath).FirstOrDefault();
8677
}
78+
79+
var existingVersion = (await versionService.List(i => i.Path == folderPath) ?? []).FirstOrDefault();
8780

8881
if (existingBook != null)
8982
{
@@ -118,7 +111,7 @@ public async Task CreateAudiobook(string path)
118111
var version = new InventoryItemVersion
119112
{
120113
Id = Guid.NewGuid(),
121-
Path = path,
114+
Path = folderPath,
122115
InventoryItemId = existingBook.Id,
123116
};
124117

@@ -211,7 +204,6 @@ public async Task CreateAudiobook(string path)
211204
if (!string.IsNullOrEmpty(folderTitle))
212205
{
213206
folderPath = path.Replace($"/{title}.{extension}", "");
214-
existingBook = books?.Where(i => i.FolderPath == folderPath).FirstOrDefault();
215207
}
216208

217209
if (existingBook != null)

0 commit comments

Comments
 (0)