| | | 1 | | using Anichron.Core.Data.Repository; |
| | | 2 | | using Anichron.Core.Domain; |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | |
| | | 5 | | namespace Anichron.API.Endpoints; |
| | | 6 | | |
| | | 7 | | internal static class AssetDtoMapper |
| | | 8 | | { |
| | | 9 | | internal static AssetDto ToDto(AssetView view, AssetBurstDto? burst) |
| | 182 | 10 | | { |
| | 182 | 11 | | var swapsAxes = view.Orientation is { } orientation && ExifOrientation.SwapsAxes(orientation); |
| | 182 | 12 | | var liveVideoUrl = view is { PairedMovHasVideo: true, PairedMovId: { } movId, PairedMovContentHash: { } movHash |
| | 182 | 13 | | ? ProxyKinds.UrlOf(movId, ProxyType.WebVideo, movHash) |
| | 182 | 14 | | : null; |
| | | 15 | | |
| | 182 | 16 | | return new AssetDto( |
| | 182 | 17 | | view.Id, |
| | 182 | 18 | | Published(view.MediaType, liveVideoUrl is not null).ToString(), |
| | 182 | 19 | | view.Year, |
| | 182 | 20 | | view.Month, |
| | 182 | 21 | | view.Day, |
| | 182 | 22 | | swapsAxes ? view.Height : view.Width, |
| | 182 | 23 | | swapsAxes ? view.Width : view.Height, |
| | 182 | 24 | | view.MediaType == MediaType.Video ? view.DurationInSeconds : null, |
| | 182 | 25 | | new AssetProxyUrls( |
| | 182 | 26 | | view.HasThumbnail ? ProxyKinds.UrlOf(view.Id, ProxyType.Thumbnail, view.ContentHash) : null, |
| | 182 | 27 | | view.HasPreview ? ProxyKinds.UrlOf(view.Id, ProxyType.FullPreview, view.ContentHash) : null, |
| | 182 | 28 | | view.HasCompactPreview ? ProxyKinds.UrlOf(view.Id, ProxyType.CompactPreview, view.ContentHash) : null, |
| | 182 | 29 | | view.HasVideo ? ProxyKinds.UrlOf(view.Id, ProxyType.WebVideo, view.ContentHash) : null), |
| | 182 | 30 | | view.Blurhash, |
| | 182 | 31 | | liveVideoUrl, |
| | 182 | 32 | | ToDto(new InteractionState(view.IsStarred, view.IsLiked, view.HiddenAt is not null)), |
| | 182 | 33 | | burst); |
| | 182 | 34 | | } |
| | | 35 | | |
| | | 36 | | internal static AssetInteractionDto ToDto(InteractionState state) |
| | 190 | 37 | | => new(state.Starred, state.Liked, state.Hidden, FlashbackWeight.For(state.Liked)); |
| | | 38 | | |
| | | 39 | | private static PublishedMediaType Published(MediaType type, bool playsLive) |
| | 182 | 40 | | => type switch |
| | 182 | 41 | | { |
| | 169 | 42 | | MediaType.Image => PublishedMediaType.Image, |
| | 7 | 43 | | MediaType.Video => PublishedMediaType.Video, |
| | 6 | 44 | | MediaType.LivePhoto => playsLive ? PublishedMediaType.LivePhoto : PublishedMediaType.Image, |
| | 0 | 45 | | _ => throw new UnreachableException($"MediaType {type} has no published value."), |
| | 182 | 46 | | }; |
| | | 47 | | } |