| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Drawing; |
| | | 3 | | using SVETA.Api.Data.Domain; |
| | | 4 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 5 | | |
| | | 6 | | namespace WinSolutions.Sveta.Server.Data.DataModel.Extensions |
| | | 7 | | { |
| | | 8 | | public static class PhotosExtension |
| | | 9 | | { |
| | 1 | 10 | | static char[] trimChars = new char[] { '/', '\\' }; |
| | | 11 | | public static void SetPhotoUrl(this List<Photo> photos, ImagesSettings imageSettings) |
| | 11 | 12 | | { |
| | 11 | 13 | | if (photos == null) |
| | 0 | 14 | | photos = new List<Photo>(); |
| | 11 | 15 | | if (photos.Count == 0) |
| | 11 | 16 | | { |
| | 11 | 17 | | photos.Add(new Photo().SetDefault(imageSettings)); |
| | 11 | 18 | | return; |
| | | 19 | | } |
| | | 20 | | string CreateUrl(string path) |
| | 0 | 21 | | { |
| | 0 | 22 | | if (path == default) |
| | 0 | 23 | | return imageSettings.PlaceholderImageUrl.AddRootUrl(imageSettings.ImageUrl); |
| | | 24 | | else |
| | 0 | 25 | | return path.AddRootUrl(imageSettings.ImageUrl); |
| | 0 | 26 | | } |
| | 0 | 27 | | foreach (var photo in photos) |
| | 0 | 28 | | { |
| | 0 | 29 | | photo.PreviewUrl = CreateUrl(photo.PreviewUrl); |
| | 0 | 30 | | photo.FullSizeUrl = CreateUrl(photo.FullSizeUrl); |
| | 0 | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | return; |
| | 11 | 34 | | } |
| | | 35 | | |
| | | 36 | | public static Photo SetDefault(this Photo photo, ImagesSettings imageSettings) |
| | 11 | 37 | | { |
| | 11 | 38 | | return new Photo |
| | 11 | 39 | | { |
| | 11 | 40 | | FullSizeHeight = imageSettings.ImageFullsizeHeight, |
| | 11 | 41 | | FullSizeWidth = imageSettings.ImageFullsizeWidth, |
| | 11 | 42 | | FullSizeUrl = imageSettings.PlaceholderImageUrl.AddRootUrl(imageSettings.ImageUrl), |
| | 11 | 43 | | PreviewHeight = imageSettings.ImagePreveiwHeight, |
| | 11 | 44 | | PreviewWidth = imageSettings.ImagePreveiwWidth, |
| | 11 | 45 | | PreviewUrl = imageSettings.PlaceholderImageUrl.AddRootUrl(imageSettings.ImageUrl) |
| | 11 | 46 | | }; |
| | 11 | 47 | | } |
| | | 48 | | |
| | | 49 | | public static string AddRootUrl(this string path, string root) => |
| | 22 | 50 | | root.TrimEnd(trimChars) + '/' + path.TrimStart(trimChars); |
| | | 51 | | |
| | | 52 | | } |
| | | 53 | | } |