| | | 1 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Concurrent; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Reflection; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Text.Json; |
| | | 9 | | using Jellyfin.Database.Implementations.Entities; |
| | | 10 | | using Jellyfin.Extensions; |
| | | 11 | | using Jellyfin.Extensions.Json; |
| | | 12 | | using MediaBrowser.Common; |
| | | 13 | | using MediaBrowser.Controller; |
| | | 14 | | using MediaBrowser.Controller.Entities; |
| | | 15 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 16 | | using MediaBrowser.Controller.Entities.TV; |
| | | 17 | | using MediaBrowser.Controller.LiveTv; |
| | | 18 | | using MediaBrowser.Model.Entities; |
| | | 19 | | using MediaBrowser.Model.LiveTv; |
| | | 20 | | using Microsoft.Extensions.Logging; |
| | | 21 | | using BaseItemDto = MediaBrowser.Controller.Entities.BaseItem; |
| | | 22 | | using BaseItemEntity = Jellyfin.Database.Implementations.Entities.BaseItemEntity; |
| | | 23 | | |
| | | 24 | | namespace Jellyfin.Server.Implementations.Item; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Handles mapping between BaseItemEntity (database) and BaseItemDto (domain) objects. |
| | | 28 | | /// </summary> |
| | | 29 | | internal static class BaseItemMapper |
| | | 30 | | { |
| | | 31 | | /// <summary> |
| | | 32 | | /// This holds all the types in the running assemblies |
| | | 33 | | /// so that we can de-serialize properly when we don't have strong types. |
| | | 34 | | /// </summary> |
| | 2 | 35 | | private static readonly ConcurrentDictionary<string, Type?> _typeMap = new ConcurrentDictionary<string, Type?>(); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Maps a Entity to the DTO. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="entity">The entity.</param> |
| | | 41 | | /// <param name="dto">The dto base instance.</param> |
| | | 42 | | /// <param name="appHost">The Application server Host.</param> |
| | | 43 | | /// <returns>The dto to map.</returns> |
| | | 44 | | public static BaseItemDto Map(BaseItemEntity entity, BaseItemDto dto, IServerApplicationHost? appHost) |
| | | 45 | | { |
| | 73 | 46 | | dto.Id = entity.Id; |
| | 73 | 47 | | dto.ParentId = entity.ParentId.GetValueOrDefault(); |
| | 73 | 48 | | dto.Path = appHost?.ExpandVirtualPath(entity.Path) ?? entity.Path; |
| | 73 | 49 | | dto.EndDate = entity.EndDate; |
| | 73 | 50 | | dto.CommunityRating = entity.CommunityRating; |
| | 73 | 51 | | dto.CustomRating = entity.CustomRating; |
| | 73 | 52 | | dto.IndexNumber = entity.IndexNumber; |
| | 73 | 53 | | dto.IsLocked = entity.IsLocked; |
| | 73 | 54 | | dto.Name = entity.Name; |
| | 73 | 55 | | dto.OfficialRating = entity.OfficialRating; |
| | 73 | 56 | | dto.Overview = entity.Overview; |
| | 73 | 57 | | dto.ParentIndexNumber = entity.ParentIndexNumber; |
| | 73 | 58 | | dto.PremiereDate = entity.PremiereDate; |
| | 73 | 59 | | dto.ProductionYear = entity.ProductionYear; |
| | 73 | 60 | | dto.SortName = entity.SortName; |
| | 73 | 61 | | dto.ForcedSortName = entity.ForcedSortName; |
| | 73 | 62 | | dto.RunTimeTicks = entity.RunTimeTicks; |
| | 73 | 63 | | dto.PreferredMetadataLanguage = entity.PreferredMetadataLanguage; |
| | 73 | 64 | | dto.PreferredMetadataCountryCode = entity.PreferredMetadataCountryCode; |
| | 73 | 65 | | dto.IsInMixedFolder = entity.IsInMixedFolder; |
| | 73 | 66 | | dto.InheritedParentalRatingValue = entity.InheritedParentalRatingValue; |
| | 73 | 67 | | dto.InheritedParentalRatingSubValue = entity.InheritedParentalRatingSubValue; |
| | 73 | 68 | | dto.CriticRating = entity.CriticRating; |
| | 73 | 69 | | dto.PresentationUniqueKey = entity.PresentationUniqueKey; |
| | 73 | 70 | | dto.OriginalTitle = entity.OriginalTitle; |
| | 73 | 71 | | dto.Album = entity.Album; |
| | 73 | 72 | | dto.LUFS = entity.LUFS; |
| | 73 | 73 | | dto.NormalizationGain = entity.NormalizationGain; |
| | 73 | 74 | | dto.IsVirtualItem = entity.IsVirtualItem; |
| | 73 | 75 | | dto.ExternalSeriesId = entity.ExternalSeriesId; |
| | 73 | 76 | | dto.Tagline = entity.Tagline; |
| | 73 | 77 | | dto.TotalBitrate = entity.TotalBitrate; |
| | 73 | 78 | | dto.ExternalId = entity.ExternalId; |
| | 73 | 79 | | dto.Size = entity.Size; |
| | 73 | 80 | | dto.Genres = string.IsNullOrWhiteSpace(entity.Genres) ? [] : entity.Genres.Split('|'); |
| | 73 | 81 | | dto.DateCreated = entity.DateCreated ?? DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); |
| | 73 | 82 | | dto.DateModified = entity.DateModified ?? DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); |
| | 73 | 83 | | dto.ChannelId = entity.ChannelId ?? Guid.Empty; |
| | 73 | 84 | | dto.DateLastRefreshed = entity.DateLastRefreshed ?? DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); |
| | 73 | 85 | | dto.DateLastSaved = entity.DateLastSaved ?? DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); |
| | 73 | 86 | | dto.OwnerId = entity.OwnerId ?? Guid.Empty; |
| | 73 | 87 | | dto.Width = entity.Width.GetValueOrDefault(); |
| | 73 | 88 | | dto.Height = entity.Height.GetValueOrDefault(); |
| | 73 | 89 | | dto.UserData = entity.UserData; |
| | | 90 | | |
| | 73 | 91 | | if (entity.Provider is not null) |
| | | 92 | | { |
| | 72 | 93 | | dto.ProviderIds = entity.Provider.ToDictionary(e => e.ProviderId, e => e.ProviderValue); |
| | | 94 | | } |
| | | 95 | | |
| | 73 | 96 | | if (entity.ExtraType is not null) |
| | | 97 | | { |
| | 0 | 98 | | dto.ExtraType = (ExtraType)entity.ExtraType; |
| | | 99 | | } |
| | | 100 | | |
| | 73 | 101 | | if (entity.LockedFields is not null) |
| | | 102 | | { |
| | 72 | 103 | | dto.LockedFields = entity.LockedFields?.Select(e => (MetadataField)e.Id).ToArray() ?? []; |
| | | 104 | | } |
| | | 105 | | |
| | 73 | 106 | | if (entity.Audio is not null) |
| | | 107 | | { |
| | 0 | 108 | | dto.Audio = (ProgramAudio)entity.Audio; |
| | | 109 | | } |
| | | 110 | | |
| | 73 | 111 | | dto.ProductionLocations = entity.ProductionLocations?.Split('|', StringSplitOptions.RemoveEmptyEntries) ?? []; |
| | 73 | 112 | | dto.Studios = entity.Studios?.Split('|') ?? []; |
| | 73 | 113 | | dto.Tags = string.IsNullOrWhiteSpace(entity.Tags) ? [] : entity.Tags.Split('|'); |
| | | 114 | | |
| | 73 | 115 | | if (dto is IHasProgramAttributes hasProgramAttributes) |
| | | 116 | | { |
| | 0 | 117 | | hasProgramAttributes.IsMovie = entity.IsMovie; |
| | 0 | 118 | | hasProgramAttributes.IsSeries = entity.IsSeries; |
| | 0 | 119 | | hasProgramAttributes.EpisodeTitle = entity.EpisodeTitle; |
| | 0 | 120 | | hasProgramAttributes.IsRepeat = entity.IsRepeat; |
| | | 121 | | } |
| | | 122 | | |
| | 73 | 123 | | if (dto is LiveTvChannel liveTvChannel) |
| | | 124 | | { |
| | 0 | 125 | | liveTvChannel.ServiceName = entity.ExternalServiceId; |
| | | 126 | | } |
| | | 127 | | |
| | 73 | 128 | | if (dto is Trailer trailer) |
| | | 129 | | { |
| | 0 | 130 | | trailer.TrailerTypes = entity.TrailerTypes?.Select(e => (TrailerType)e.Id).ToArray() ?? []; |
| | | 131 | | } |
| | | 132 | | |
| | 73 | 133 | | if (dto is Video video) |
| | | 134 | | { |
| | 1 | 135 | | video.PrimaryVersionId = entity.PrimaryVersionId; |
| | | 136 | | } |
| | | 137 | | |
| | 73 | 138 | | if (dto is IHasSeries hasSeriesName) |
| | | 139 | | { |
| | 0 | 140 | | hasSeriesName.SeriesName = entity.SeriesName; |
| | 0 | 141 | | hasSeriesName.SeriesId = entity.SeriesId.GetValueOrDefault(); |
| | 0 | 142 | | hasSeriesName.SeriesPresentationUniqueKey = entity.SeriesPresentationUniqueKey; |
| | | 143 | | } |
| | | 144 | | |
| | 73 | 145 | | if (dto is Episode episode) |
| | | 146 | | { |
| | 0 | 147 | | episode.SeasonName = entity.SeasonName; |
| | 0 | 148 | | episode.SeasonId = entity.SeasonId.GetValueOrDefault(); |
| | | 149 | | } |
| | | 150 | | |
| | 73 | 151 | | if (dto is IHasArtist hasArtists) |
| | | 152 | | { |
| | 0 | 153 | | hasArtists.Artists = entity.Artists?.Split('|', StringSplitOptions.RemoveEmptyEntries) ?? []; |
| | | 154 | | } |
| | | 155 | | |
| | 73 | 156 | | if (dto is IHasAlbumArtist hasAlbumArtists) |
| | | 157 | | { |
| | 0 | 158 | | hasAlbumArtists.AlbumArtists = entity.AlbumArtists?.Split('|', StringSplitOptions.RemoveEmptyEntries) ?? []; |
| | | 159 | | } |
| | | 160 | | |
| | 73 | 161 | | if (dto is LiveTvProgram program) |
| | | 162 | | { |
| | 0 | 163 | | program.ShowId = entity.ShowId; |
| | | 164 | | } |
| | | 165 | | |
| | 73 | 166 | | if (entity.Images is not null) |
| | | 167 | | { |
| | 72 | 168 | | dto.ImageInfos = entity.Images.Select(e => MapImageFromEntity(e, appHost)).ToArray(); |
| | | 169 | | } |
| | | 170 | | |
| | 73 | 171 | | if (dto is IHasStartDate hasStartDate) |
| | | 172 | | { |
| | 0 | 173 | | hasStartDate.StartDate = entity.StartDate.GetValueOrDefault(); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | // Fields that are present in the DB but are never actually used |
| | | 177 | | // dto.UnratedType = entity.UnratedType; |
| | | 178 | | // dto.TopParentId = entity.TopParentId; |
| | | 179 | | // dto.CleanName = entity.CleanName; |
| | | 180 | | // dto.UserDataKey = entity.UserDataKey; |
| | | 181 | | |
| | 73 | 182 | | if (dto is Folder folder) |
| | | 183 | | { |
| | 72 | 184 | | folder.DateLastMediaAdded = entity.DateLastMediaAdded ?? DateTime.SpecifyKind(DateTime.MinValue, DateTimeKin |
| | 72 | 185 | | if (entity.LinkedChildEntities is not null && entity.LinkedChildEntities.Count > 0) |
| | | 186 | | { |
| | 0 | 187 | | folder.LinkedChildren = entity.LinkedChildEntities |
| | 0 | 188 | | .OrderBy(e => e.SortOrder) |
| | 0 | 189 | | .Select(e => new LinkedChild |
| | 0 | 190 | | { |
| | 0 | 191 | | ItemId = e.ChildId, |
| | 0 | 192 | | Type = (MediaBrowser.Controller.Entities.LinkedChildType)e.ChildType |
| | 0 | 193 | | }) |
| | 0 | 194 | | .ToArray(); |
| | | 195 | | } |
| | | 196 | | } |
| | | 197 | | |
| | 73 | 198 | | return dto; |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Maps a DTO to a database entity. |
| | | 203 | | /// </summary> |
| | | 204 | | /// <param name="dto">The DTO.</param> |
| | | 205 | | /// <param name="appHost">The application host for path resolution.</param> |
| | | 206 | | /// <returns>The database entity.</returns> |
| | | 207 | | public static BaseItemEntity Map(BaseItemDto dto, IServerApplicationHost appHost) |
| | | 208 | | { |
| | 123 | 209 | | var dtoType = dto.GetType(); |
| | 123 | 210 | | var entity = new BaseItemEntity() |
| | 123 | 211 | | { |
| | 123 | 212 | | Type = dtoType.ToString(), |
| | 123 | 213 | | Id = dto.Id |
| | 123 | 214 | | }; |
| | | 215 | | |
| | 123 | 216 | | if (TypeRequiresDeserialization(dtoType)) |
| | | 217 | | { |
| | 102 | 218 | | entity.Data = JsonSerializer.Serialize(dto, dtoType, JsonDefaults.Options); |
| | | 219 | | } |
| | | 220 | | |
| | 123 | 221 | | entity.ParentId = !dto.ParentId.IsEmpty() ? dto.ParentId : null; |
| | 123 | 222 | | entity.Path = GetPathToSave(dto.Path, appHost); |
| | 123 | 223 | | entity.EndDate = dto.EndDate; |
| | 123 | 224 | | entity.CommunityRating = dto.CommunityRating; |
| | 123 | 225 | | entity.CustomRating = dto.CustomRating; |
| | 123 | 226 | | entity.IndexNumber = dto.IndexNumber; |
| | 123 | 227 | | entity.IsLocked = dto.IsLocked; |
| | 123 | 228 | | entity.Name = dto.Name; |
| | 123 | 229 | | entity.CleanName = dto.Name.GetCleanValue(); |
| | 123 | 230 | | entity.OfficialRating = dto.OfficialRating; |
| | 123 | 231 | | entity.Overview = dto.Overview; |
| | 123 | 232 | | entity.ParentIndexNumber = dto.ParentIndexNumber; |
| | 123 | 233 | | entity.PremiereDate = dto.PremiereDate; |
| | 123 | 234 | | entity.ProductionYear = dto.ProductionYear; |
| | 123 | 235 | | entity.SortName = dto.SortName; |
| | 123 | 236 | | entity.ForcedSortName = dto.ForcedSortName; |
| | 123 | 237 | | entity.RunTimeTicks = dto.RunTimeTicks; |
| | 123 | 238 | | entity.PreferredMetadataLanguage = dto.PreferredMetadataLanguage; |
| | 123 | 239 | | entity.PreferredMetadataCountryCode = dto.PreferredMetadataCountryCode; |
| | 123 | 240 | | entity.IsInMixedFolder = dto.IsInMixedFolder; |
| | 123 | 241 | | entity.InheritedParentalRatingValue = dto.InheritedParentalRatingValue; |
| | 123 | 242 | | entity.InheritedParentalRatingSubValue = dto.InheritedParentalRatingSubValue; |
| | 123 | 243 | | entity.CriticRating = dto.CriticRating; |
| | 123 | 244 | | entity.PresentationUniqueKey = dto.PresentationUniqueKey; |
| | 123 | 245 | | entity.OriginalTitle = dto.OriginalTitle; |
| | 123 | 246 | | entity.Album = dto.Album; |
| | 123 | 247 | | entity.LUFS = dto.LUFS; |
| | 123 | 248 | | entity.NormalizationGain = dto.NormalizationGain; |
| | 123 | 249 | | entity.IsVirtualItem = dto.IsVirtualItem; |
| | 123 | 250 | | entity.ExternalSeriesId = dto.ExternalSeriesId; |
| | 123 | 251 | | entity.Tagline = dto.Tagline; |
| | 123 | 252 | | entity.TotalBitrate = dto.TotalBitrate; |
| | 123 | 253 | | entity.ExternalId = dto.ExternalId; |
| | 123 | 254 | | entity.Size = dto.Size; |
| | 123 | 255 | | entity.Genres = string.Join('|', dto.Genres.Distinct(StringComparer.OrdinalIgnoreCase)); |
| | 123 | 256 | | entity.DateCreated = dto.DateCreated == DateTime.MinValue ? null : dto.DateCreated; |
| | 123 | 257 | | entity.DateModified = dto.DateModified == DateTime.MinValue ? null : dto.DateModified; |
| | 123 | 258 | | entity.ChannelId = dto.ChannelId; |
| | 123 | 259 | | entity.DateLastRefreshed = dto.DateLastRefreshed == DateTime.MinValue ? null : dto.DateLastRefreshed; |
| | 123 | 260 | | entity.DateLastSaved = dto.DateLastSaved == DateTime.MinValue ? null : dto.DateLastSaved; |
| | 123 | 261 | | entity.OwnerId = dto.OwnerId == Guid.Empty ? null : dto.OwnerId; |
| | 123 | 262 | | entity.Width = dto.Width; |
| | 123 | 263 | | entity.Height = dto.Height; |
| | 123 | 264 | | entity.Provider = dto.ProviderIds.Select(e => new BaseItemProvider() |
| | 123 | 265 | | { |
| | 123 | 266 | | Item = entity, |
| | 123 | 267 | | ProviderId = e.Key, |
| | 123 | 268 | | ProviderValue = e.Value |
| | 123 | 269 | | }).ToList(); |
| | | 270 | | |
| | 123 | 271 | | if (dto.Audio.HasValue) |
| | | 272 | | { |
| | 0 | 273 | | entity.Audio = (ProgramAudioEntity)dto.Audio; |
| | | 274 | | } |
| | | 275 | | |
| | 123 | 276 | | if (dto.ExtraType.HasValue) |
| | | 277 | | { |
| | 0 | 278 | | entity.ExtraType = (BaseItemExtraType)dto.ExtraType; |
| | | 279 | | } |
| | | 280 | | |
| | 123 | 281 | | entity.ProductionLocations = dto.ProductionLocations is not null ? string.Join('|', dto.ProductionLocations.Wher |
| | 123 | 282 | | entity.Studios = dto.Studios is not null ? string.Join('|', dto.Studios.Distinct(StringComparer.OrdinalIgnoreCas |
| | 123 | 283 | | entity.Tags = dto.Tags is not null ? string.Join('|', dto.Tags.Distinct(StringComparer.OrdinalIgnoreCase)) : nul |
| | 123 | 284 | | entity.LockedFields = dto.LockedFields is not null ? dto.LockedFields |
| | 123 | 285 | | .Select(e => new BaseItemMetadataField() |
| | 123 | 286 | | { |
| | 123 | 287 | | Id = (int)e, |
| | 123 | 288 | | Item = entity, |
| | 123 | 289 | | ItemId = entity.Id |
| | 123 | 290 | | }) |
| | 123 | 291 | | .ToArray() : null; |
| | | 292 | | |
| | 123 | 293 | | if (dto is IHasProgramAttributes hasProgramAttributes) |
| | | 294 | | { |
| | 0 | 295 | | entity.IsMovie = hasProgramAttributes.IsMovie; |
| | 0 | 296 | | entity.IsSeries = hasProgramAttributes.IsSeries; |
| | 0 | 297 | | entity.EpisodeTitle = hasProgramAttributes.EpisodeTitle; |
| | 0 | 298 | | entity.IsRepeat = hasProgramAttributes.IsRepeat; |
| | | 299 | | } |
| | | 300 | | |
| | 123 | 301 | | if (dto is LiveTvChannel liveTvChannel) |
| | | 302 | | { |
| | 0 | 303 | | entity.ExternalServiceId = liveTvChannel.ServiceName; |
| | | 304 | | } |
| | | 305 | | |
| | 123 | 306 | | if (dto is Video video) |
| | | 307 | | { |
| | 0 | 308 | | entity.PrimaryVersionId = video.PrimaryVersionId; |
| | | 309 | | } |
| | | 310 | | |
| | 123 | 311 | | if (dto is IHasSeries hasSeriesName) |
| | | 312 | | { |
| | 0 | 313 | | entity.SeriesName = hasSeriesName.SeriesName; |
| | 0 | 314 | | entity.SeriesId = hasSeriesName.SeriesId; |
| | 0 | 315 | | entity.SeriesPresentationUniqueKey = hasSeriesName.SeriesPresentationUniqueKey; |
| | | 316 | | } |
| | | 317 | | |
| | 123 | 318 | | if (dto is Episode episode) |
| | | 319 | | { |
| | 0 | 320 | | entity.SeasonName = episode.SeasonName; |
| | 0 | 321 | | entity.SeasonId = episode.SeasonId; |
| | | 322 | | } |
| | | 323 | | |
| | 123 | 324 | | if (dto is IHasArtist hasArtists) |
| | | 325 | | { |
| | 0 | 326 | | entity.Artists = hasArtists.Artists is not null ? string.Join('|', hasArtists.Artists.Distinct(StringCompare |
| | | 327 | | } |
| | | 328 | | |
| | 123 | 329 | | if (dto is IHasAlbumArtist hasAlbumArtists) |
| | | 330 | | { |
| | 0 | 331 | | entity.AlbumArtists = hasAlbumArtists.AlbumArtists is not null ? string.Join('|', hasAlbumArtists.AlbumArtis |
| | | 332 | | } |
| | | 333 | | |
| | 123 | 334 | | if (dto is LiveTvProgram program) |
| | | 335 | | { |
| | 0 | 336 | | entity.ShowId = program.ShowId; |
| | | 337 | | } |
| | | 338 | | |
| | 123 | 339 | | if (dto.ImageInfos is not null) |
| | | 340 | | { |
| | 123 | 341 | | entity.Images = dto.ImageInfos.Select(f => MapImageToEntity(dto.Id, f)).ToArray(); |
| | | 342 | | } |
| | | 343 | | |
| | 123 | 344 | | if (dto is Trailer trailer) |
| | | 345 | | { |
| | 0 | 346 | | entity.TrailerTypes = trailer.TrailerTypes?.Select(e => new BaseItemTrailerType() |
| | 0 | 347 | | { |
| | 0 | 348 | | Id = (int)e, |
| | 0 | 349 | | Item = entity, |
| | 0 | 350 | | ItemId = entity.Id |
| | 0 | 351 | | }).ToArray() ?? []; |
| | | 352 | | } |
| | | 353 | | |
| | 123 | 354 | | entity.MediaType = dto.MediaType.ToString(); |
| | 123 | 355 | | if (dto is IHasStartDate hasStartDate) |
| | | 356 | | { |
| | 0 | 357 | | entity.StartDate = hasStartDate.StartDate; |
| | | 358 | | } |
| | | 359 | | |
| | 123 | 360 | | entity.UnratedType = dto.GetBlockUnratedType().ToString(); |
| | | 361 | | |
| | | 362 | | // Fields that are present in the DB but are never actually used |
| | | 363 | | // dto.UserDataKey = entity.UserDataKey; |
| | | 364 | | |
| | 123 | 365 | | if (dto is Folder folder) |
| | | 366 | | { |
| | 123 | 367 | | entity.DateLastMediaAdded = folder.DateLastMediaAdded == DateTime.MinValue ? null : folder.DateLastMediaAdde |
| | 123 | 368 | | entity.IsFolder = folder.IsFolder; |
| | | 369 | | } |
| | | 370 | | |
| | 123 | 371 | | return entity; |
| | | 372 | | } |
| | | 373 | | |
| | | 374 | | /// <summary> |
| | | 375 | | /// Maps a database image entity to a domain image info. |
| | | 376 | | /// </summary> |
| | | 377 | | /// <param name="e">The database image entity.</param> |
| | | 378 | | /// <param name="appHost">The application host.</param> |
| | | 379 | | /// <returns>The mapped image info.</returns> |
| | | 380 | | public static ItemImageInfo MapImageFromEntity(BaseItemImageInfo e, IServerApplicationHost? appHost) |
| | | 381 | | { |
| | 0 | 382 | | return new ItemImageInfo() |
| | 0 | 383 | | { |
| | 0 | 384 | | Path = appHost?.ExpandVirtualPath(e.Path) ?? e.Path, |
| | 0 | 385 | | BlurHash = e.Blurhash is null ? null : Encoding.UTF8.GetString(e.Blurhash), |
| | 0 | 386 | | DateModified = e.DateModified ?? DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc), |
| | 0 | 387 | | Height = e.Height, |
| | 0 | 388 | | Width = e.Width, |
| | 0 | 389 | | Type = (ImageType)e.ImageType |
| | 0 | 390 | | }; |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | /// <summary> |
| | | 394 | | /// Maps a domain image info to a database image entity. |
| | | 395 | | /// </summary> |
| | | 396 | | /// <param name="baseItemId">The parent item ID.</param> |
| | | 397 | | /// <param name="e">The image info to map.</param> |
| | | 398 | | /// <returns>The mapped database entity.</returns> |
| | | 399 | | public static BaseItemImageInfo MapImageToEntity(Guid baseItemId, ItemImageInfo e) |
| | | 400 | | { |
| | 0 | 401 | | return new BaseItemImageInfo() |
| | 0 | 402 | | { |
| | 0 | 403 | | ItemId = baseItemId, |
| | 0 | 404 | | Id = Guid.NewGuid(), |
| | 0 | 405 | | Path = e.Path, |
| | 0 | 406 | | Blurhash = e.BlurHash is null ? null : Encoding.UTF8.GetBytes(e.BlurHash), |
| | 0 | 407 | | DateModified = e.DateModified, |
| | 0 | 408 | | Height = e.Height, |
| | 0 | 409 | | Width = e.Width, |
| | 0 | 410 | | ImageType = (ImageInfoImageType)e.Type, |
| | 0 | 411 | | Item = null! |
| | 0 | 412 | | }; |
| | | 413 | | } |
| | | 414 | | |
| | | 415 | | /// <summary> |
| | | 416 | | /// Gets the type from a type name string. |
| | | 417 | | /// </summary> |
| | | 418 | | /// <param name="typeName">The type name.</param> |
| | | 419 | | /// <returns>The resolved type, or null.</returns> |
| | | 420 | | public static Type? GetType(string typeName) |
| | | 421 | | { |
| | 147 | 422 | | ArgumentException.ThrowIfNullOrEmpty(typeName); |
| | | 423 | | |
| | 147 | 424 | | return _typeMap.GetOrAdd(typeName, k => AppDomain.CurrentDomain.GetAssemblies() |
| | 147 | 425 | | .Select(a => a.GetType(k)) |
| | 147 | 426 | | .FirstOrDefault(t => t is not null)); |
| | | 427 | | } |
| | | 428 | | |
| | | 429 | | /// <summary> |
| | | 430 | | /// Checks whether a type requires JSON deserialization. |
| | | 431 | | /// </summary> |
| | | 432 | | /// <param name="type">The type to check.</param> |
| | | 433 | | /// <returns>True if the type requires deserialization.</returns> |
| | | 434 | | public static bool TypeRequiresDeserialization(Type type) |
| | | 435 | | { |
| | 196 | 436 | | return type.GetCustomAttribute<RequiresSourceSerialisationAttribute>() == null; |
| | | 437 | | } |
| | | 438 | | |
| | | 439 | | /// <summary> |
| | | 440 | | /// Deserializes a BaseItemEntity and sets all properties. |
| | | 441 | | /// </summary> |
| | | 442 | | /// <param name="baseItemEntity">The DB entity.</param> |
| | | 443 | | /// <param name="logger">Logger.</param> |
| | | 444 | | /// <param name="appHost">The application server Host.</param> |
| | | 445 | | /// <param name="skipDeserialization">If only mapping should be processed.</param> |
| | | 446 | | /// <returns>A mapped BaseItem, or null if the item type is unknown.</returns> |
| | | 447 | | public static BaseItemDto? DeserializeBaseItem(BaseItemEntity baseItemEntity, ILogger logger, IServerApplicationHost |
| | | 448 | | { |
| | 75 | 449 | | var type = GetType(baseItemEntity.Type); |
| | 75 | 450 | | if (type is null) |
| | | 451 | | { |
| | 2 | 452 | | logger.LogWarning( |
| | 2 | 453 | | "Skipping item {ItemId} with unknown type '{ItemType}'. This may indicate a removed plugin or database c |
| | 2 | 454 | | baseItemEntity.Id, |
| | 2 | 455 | | baseItemEntity.Type); |
| | 2 | 456 | | return null; |
| | | 457 | | } |
| | | 458 | | |
| | 73 | 459 | | BaseItemDto? dto = null; |
| | 73 | 460 | | if (TypeRequiresDeserialization(type) && baseItemEntity.Data is not null && !skipDeserialization) |
| | | 461 | | { |
| | | 462 | | try |
| | | 463 | | { |
| | 12 | 464 | | dto = JsonSerializer.Deserialize(baseItemEntity.Data, type, JsonDefaults.Options) as BaseItemDto; |
| | 12 | 465 | | } |
| | 0 | 466 | | catch (JsonException ex) |
| | | 467 | | { |
| | 0 | 468 | | logger.LogError(ex, "Error deserializing item with JSON: {Data}", baseItemEntity.Data); |
| | 0 | 469 | | } |
| | | 470 | | } |
| | | 471 | | |
| | 73 | 472 | | if (dto is null) |
| | | 473 | | { |
| | 61 | 474 | | dto = Activator.CreateInstance(type) as BaseItemDto ?? throw new InvalidOperationException("Cannot deseriali |
| | | 475 | | } |
| | | 476 | | |
| | 73 | 477 | | return Map(baseItemEntity, dto, appHost); |
| | | 478 | | } |
| | | 479 | | |
| | | 480 | | private static string? GetPathToSave(string path, IServerApplicationHost appHost) |
| | | 481 | | { |
| | 123 | 482 | | if (path is null) |
| | | 483 | | { |
| | 10 | 484 | | return null; |
| | | 485 | | } |
| | | 486 | | |
| | 113 | 487 | | return appHost.ReverseVirtualPath(path); |
| | | 488 | | } |
| | | 489 | | } |