| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.IO.Compression; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text; |
| | | 7 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 8 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 9 | | using SkiaSharp; |
| | | 10 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 11 | | |
| | | 12 | | namespace WinSolutions.Sveta.Server.Data.DataLoading.Loaders |
| | | 13 | | { |
| | | 14 | | public class ImagePackageLoader : IDataLoader |
| | | 15 | | { |
| | 0 | 16 | | public IPhotoService PhotoService { get; set; } |
| | | 17 | | |
| | 0 | 18 | | public Action<BaseRecord> BeforeAddRecordAction {get;set;} |
| | | 19 | | |
| | | 20 | | public void Commit(SvetaDbContext context, long uploadId) |
| | 0 | 21 | | { |
| | 0 | 22 | | throw new NotImplementedException(); |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | public void GetTemplate(SvetaDbContext context, Stream stream, bool csvFormat = true) |
| | 0 | 26 | | { |
| | 0 | 27 | | throw new NotImplementedException(); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public LoadingResult Load(SvetaDbContext context, Stream stream) |
| | 0 | 31 | | { |
| | 0 | 32 | | LoadingResult res = new LoadingResult |
| | 0 | 33 | | { |
| | 0 | 34 | | Errors = new List<ParsingError>() |
| | 0 | 35 | | }; |
| | | 36 | | |
| | 0 | 37 | | var photos = new List<Photo>(); |
| | | 38 | | |
| | 0 | 39 | | int x = 0; |
| | 0 | 40 | | Upload upload = new Upload(); |
| | | 41 | | int newPrevWidth, newPrevHeight; |
| | 0 | 42 | | using (var zip = new ZipArchive(stream)) |
| | 0 | 43 | | { |
| | 0 | 44 | | foreach (var e in zip.Entries) |
| | 0 | 45 | | { |
| | 0 | 46 | | if (string.IsNullOrEmpty(e.Name)) continue; |
| | | 47 | | |
| | 0 | 48 | | x ++; |
| | 0 | 49 | | var photo = new Photo |
| | 0 | 50 | | { |
| | 0 | 51 | | FullSizeUrl = e.Name, |
| | 0 | 52 | | PreviewUrl = e.Name |
| | 0 | 53 | | }; |
| | | 54 | | |
| | | 55 | | try |
| | 0 | 56 | | { |
| | 0 | 57 | | BeforeAddRecordAction?.Invoke(photo); |
| | | 58 | | |
| | 0 | 59 | | e.ExtractToFile(photo.FullSizePath, true); |
| | | 60 | | |
| | 0 | 61 | | using (var streamImg = File.OpenRead(photo.FullSizePath)) |
| | 0 | 62 | | using (var inputStream = new SKManagedStream(streamImg)) |
| | 0 | 63 | | using (var original = SKBitmap.Decode(inputStream)) |
| | 0 | 64 | | { |
| | 0 | 65 | | if (original.Width > original.Height) //сохраняем пропорции |
| | 0 | 66 | | { |
| | 0 | 67 | | newPrevWidth = photo.PreviewWidth; |
| | 0 | 68 | | newPrevHeight = original.Height * photo.PreviewWidth / original.Width; |
| | 0 | 69 | | } |
| | | 70 | | else |
| | 0 | 71 | | { |
| | 0 | 72 | | newPrevWidth = original.Width * photo.PreviewHeight / original.Height; |
| | 0 | 73 | | newPrevHeight = photo.PreviewHeight; |
| | 0 | 74 | | } |
| | 0 | 75 | | using (var resized = original.Resize(new SKImageInfo(newPrevWidth, newPrevHeight), SKFilterQ |
| | 0 | 76 | | using (var image = SKImage.FromBitmap(resized)) |
| | 0 | 77 | | using (var output = File.OpenWrite(photo.PreviewPath)) |
| | 0 | 78 | | image.Encode(SKEncodedImageFormat.Jpeg, 75).SaveTo(output); |
| | | 79 | | |
| | 0 | 80 | | photo.FullSizeHeight = original.Height; |
| | 0 | 81 | | photo.FullSizeWidth = original.Width; |
| | 0 | 82 | | photo.PreviewHeight = newPrevHeight; |
| | 0 | 83 | | photo.PreviewWidth = newPrevWidth; |
| | 0 | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | photos.Add(photo); |
| | 0 | 87 | | res.AffectedCount ++; |
| | 0 | 88 | | } |
| | 0 | 89 | | catch(Exception ex) |
| | 0 | 90 | | { |
| | 0 | 91 | | (res.Errors as List<ParsingError>).Add(new ParsingError |
| | 0 | 92 | | { |
| | 0 | 93 | | LineNumber = x, |
| | 0 | 94 | | Line = new List<string> { e.Name }, |
| | 0 | 95 | | Exception = new Exception($"'{e.Name}' -> '{photo.FullSizePath}' | {ex.Message}") |
| | 0 | 96 | | }); |
| | 0 | 97 | | } |
| | 0 | 98 | | } |
| | 0 | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | var dbPhotos = PhotoService.FindPhotos(photos.Select(x => x.FullSizeUrl.TrimStart('/').ToLower()).ToList()); |
| | 0 | 102 | | dbPhotos.AddRange(PhotoService.FindPhotos(photos.Select(x => x.FullSizeUrl.ToLower()).ToList())); |
| | 0 | 103 | | photos.ForEach(x => |
| | 0 | 104 | | { |
| | 0 | 105 | | var dbPhts = dbPhotos.Where(p => p.FullSizeUrl.TrimStart('/').ToLower() == x.FullSizeUrl.TrimStart('/'). |
| | 0 | 106 | | if (dbPhts.Any()) |
| | 0 | 107 | | { |
| | 0 | 108 | | dbPhts.ForEach(p => |
| | 0 | 109 | | { |
| | 0 | 110 | | p.FullSizeHeight = x.FullSizeHeight; |
| | 0 | 111 | | p.FullSizeWidth = x.FullSizeWidth; |
| | 0 | 112 | | p.PreviewUrl = x.PreviewUrl; |
| | 0 | 113 | | p.PreviewHeight = x.PreviewHeight; |
| | 0 | 114 | | p.PreviewWidth = x.PreviewWidth; |
| | 0 | 115 | | }); |
| | 0 | 116 | | } |
| | 0 | 117 | | else |
| | 0 | 118 | | { |
| | 0 | 119 | | (res.Errors as List<ParsingError>).Add(new ParsingError |
| | 0 | 120 | | { |
| | 0 | 121 | | LineNumber = -1, |
| | 0 | 122 | | Line = new List<string> { x.FullSizeUrl.TrimStart('/') }, |
| | 0 | 123 | | Exception = new Exception($"Фотография не найдена в БД") |
| | 0 | 124 | | }); |
| | 0 | 125 | | } |
| | 0 | 126 | | }); |
| | | 127 | | |
| | 0 | 128 | | if(res.Errors.Any()) |
| | 0 | 129 | | { |
| | 0 | 130 | | (res.Errors as List<ParsingError>).Insert(0, new ParsingError |
| | 0 | 131 | | { |
| | 0 | 132 | | LineNumber = -1, |
| | 0 | 133 | | Line = new List<string> { "Файл" }, |
| | 0 | 134 | | Exception = new Exception("Ошибка") |
| | 0 | 135 | | }); |
| | 0 | 136 | | upload.Status = res.AffectedCount > 0 ? UploadStatus.PartlyLoaded : UploadStatus.Error; |
| | 0 | 137 | | } |
| | | 138 | | else |
| | 0 | 139 | | { |
| | 0 | 140 | | upload.Status = UploadStatus.FullLoaded; |
| | 0 | 141 | | } |
| | 0 | 142 | | upload.ResultFile = res.GetResultFile(); |
| | | 143 | | |
| | 0 | 144 | | context.Uploads.Add(upload); |
| | 0 | 145 | | context.SaveChanges(); |
| | | 146 | | |
| | 0 | 147 | | res.UploadId = upload.Id; |
| | | 148 | | |
| | 0 | 149 | | return res; |
| | 0 | 150 | | } |
| | | 151 | | |
| | | 152 | | public void Rollback(SvetaDbContext context, long uploadId) |
| | 0 | 153 | | { |
| | 0 | 154 | | throw new NotImplementedException(); |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | } |