From 60dd44153b5f2b233dc66032507ee6c9a925ed0e Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Mon, 27 Apr 2026 03:06:02 +1000 Subject: v0.12a --- Services/MediaService.cs | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) (limited to 'Services/MediaService.cs') diff --git a/Services/MediaService.cs b/Services/MediaService.cs index 27c77d6..763b953 100644 --- a/Services/MediaService.cs +++ b/Services/MediaService.cs @@ -24,7 +24,8 @@ public interface IMediaService { DateTime? lastWriteTime = null, DateTime? createTime = null, string? path = null, - PathType? pathType = null); + PathType? pathType = null, + Guid[]? tagIds = null); public void Delete(Guid media); public void Delete(Media media); @@ -117,7 +118,8 @@ public class MediaService : IMediaService { DateTime? lastWriteTime = null, DateTime? createTime = null, string? path = null, - PathType? pathType = null) { + PathType? pathType = null, + Guid[]? tagIds = null) { using var db = dbFactory.CreateDbContext(); using var transaction = db.Database.BeginTransaction(); @@ -145,10 +147,10 @@ public class MediaService : IMediaService { fileData.Seek(0, SeekOrigin.Begin); using var magickImage = new MagickImage(fileData); - var media = db.UploadedFiles - .Include(uf => uf.Media) - .FirstOrDefault(uf => uf.Checksum == hash)? - .Media; + var media = db.Media + .Include(m => m.UploadedFiles) + .Include(m => m.Tags) + .FirstOrDefault(m => m.UploadedFiles.Any(uf => uf.Checksum == hash)); var fileRecord = new UploadedFile() { Filename = fileName, @@ -166,6 +168,24 @@ public class MediaService : IMediaService { PathType = pathType }; + var tags = Array.Empty(); + if(tagIds is not null) { + tagIds = tagIds.Distinct().ToArray(); + + tags = db.TagDefinitions + .Where(td => tagIds.Contains(td.Guid)) + .ToArray(); + + if(tags.Count() < tagIds.Count()) { + var badIds = tagIds + .Where(x => !tags.Select(td => td.Guid).Contains(x)) + .Order(); + + throw new MediaCreateException( + $"Non-existent tags specified: {string.Join(", ", badIds)}"); + } + } + if(media is null) { var ingestTagDef = db.TagDefinitions .First(td => td.Guid == HBContext.IngestTag); @@ -174,9 +194,9 @@ public class MediaService : IMediaService { UploadedFiles = new() { fileRecord }, - Tags = new() { - new() { TagDefinition = ingestTagDef } - } + Tags = tags is null ? [ new() { TagDefinition = ingestTagDef } ] : tags + .Select(td => new Tag() { TagDefinition = td }) + .ToList() }; using var newFile = File.Create(GetPath(media)); @@ -190,12 +210,15 @@ public class MediaService : IMediaService { media.CurrentUploadedFile = fileRecord; db.SaveChanges(); } else { - db.Entry(media).Collection(m => m.UploadedFiles).Load(); var fileHashes = media.UploadedFiles .Select(uf => GetUploadedFileHash(uf)); // Only add the uploaded file record if it contains new information if(!fileHashes.Contains(GetUploadedFileHash(fileRecord))) media.UploadedFiles.Add(fileRecord); + // Add new tags if needed + var missingTags = tags + .Where(td => !media.Tags.Select(t => t.TagDefinition.Guid).Contains(td.Guid)); + media.Tags.AddRange(missingTags.Select(td => new Tag() { TagDefinition = td })); db.Update(media); db.SaveChanges(); } -- cgit v1.3