Example #1
0
        public void Load(Stream input)
        {
            using (var br = new BinaryReaderX(input, true))
            {
                _absoluteBlockPosition = 0;
                _chunkHeader           = br.ReadType <GIMChunk>();

                while (input.Position < _absoluteBlockPosition + _chunkHeader.BlockSize && input.Position < input.Length)
                {
                    var blockID = (Blocks)br.PeekChar();

                    switch (blockID)
                    {
                    case Blocks.Picture:
                        var pb = new PictureBlock();
                        pb.Load(input);
                        PictureBlocks.Add(pb);
                        break;

                    case Blocks.FileInfo:
                        var fi = new FileInfoBlock();
                        fi.Load(input);
                        FileInfoBlocks.Add(fi);
                        break;
                    }
                }
            }
        }
        private async Task ImportGalleryAsync(Export data, Site site, List <Post <string> > posts)
        {
            foreach (var cat in data.GalleryCats)
            {
                var textParts = new List <string>();
                var current   = cat;
                while (current != null)
                {
                    textParts.Add(current.Title);
                    if (current.CatId > 0)
                    {
                        current = data.GalleryCats.FirstOrDefault(c => c.Id == current.CatId);
                    }
                    else
                    {
                        current = null;
                    }
                }

                textParts.Reverse();
                var tags = new List <Tag>();
                foreach (var text in textParts)
                {
                    var tag = GetTag(text);
                    tags.Add(tag);
                }

                var pics = data.GalleryPics.Where(p => p.CatId == cat.Id);
                foreach (var picsGroup in pics.GroupBy(p => p.Date.Date))
                {
                    var url  = $"gallery_{picsGroup.First().Id.ToString()}";
                    var post = new Post <string>
                    {
                        Id            = Guid.NewGuid(),
                        Url           = url,
                        Title         = cat.Title,
                        DateAdded     = picsGroup.First().Date,
                        DateUpdated   = picsGroup.First().Date,
                        DatePublished = picsGroup.First().Date,
                        IsPublished   = true,
                        AuthorId      = picsGroup.First().AuthorId.ToString(),
                        Blocks        = new List <ContentBlock>()
                    };

                    foreach (var galleryPicExport in picsGroup)
                    {
                        var pictures = new List <StorageItem>();
                        foreach (var picFile in galleryPicExport.Files)
                        {
                            var pic = await _filesUploader.UploadFromUrlAsync(picFile.Url,
                                                                              $"posts/{post.DateAdded.Year.ToString()}/{post.DateAdded.Month.ToString()}",
                                                                              picFile.FileName);

                            if (pic != null)
                            {
                                pictures.Add(pic);
                            }
                        }

                        if (pictures.Count == 1)
                        {
                            var block = new PictureBlock
                            {
                                Id = Guid.NewGuid(), Position = 1, Data = new PictureBlockData()
                            };
                            var picFile = pictures[0];
                            var pic     = await _filesUploader.UploadFromUrlAsync(picFile.PublicUri.ToString(),
                                                                                  $"posts/{post.DateAdded.Year.ToString()}/{post.DateAdded.Month.ToString()}",
                                                                                  picFile.FileName);

                            if (pic != null)
                            {
                                block.Data.Picture = pic;
                                post.Blocks.Add(block);
                            }
                        }
                        else
                        {
                            var block = new GalleryBlock
                            {
                                Id = Guid.NewGuid(), Position = 1, Data = new GalleryBlockData()
                            };

                            block.Data.Pictures = pictures.ToArray();
                            post.Blocks.Add(block);
                        }

                        if (!string.IsNullOrEmpty(galleryPicExport.Desc))
                        {
                            post.Blocks.Add(new TextBlock
                            {
                                Id       = Guid.NewGuid(),
                                Position = 0,
                                Data     = new TextBlockData {
                                    Text = galleryPicExport.Desc
                                }
                            });
                        }
                    }

                    post.TagIds = tags.Select(t => t.Id).ToArray();

                    if (cat.GameId > 0)
                    {
                        var game = data.Games.FirstOrDefault(g => g.Id == cat.GameId);
                        if (game != null)
                        {
                            post.SectionIds = new[] { _gamesMap[game.Id].Id };
                            post.SiteIds    = _gamesMap[game.Id].SiteIds;
                        }
                    }

                    if (cat.DeveloperId > 0)
                    {
                        var developer = data.Developers.FirstOrDefault(g => g.Id == cat.DeveloperId);
                        if (developer != null)
                        {
                            post.SectionIds = new[] { _developersMap[developer.Id].Id };
                            post.SiteIds    = _developersMap[developer.Id].SiteIds;
                        }
                    }

                    if (cat.TopicId > 0)
                    {
                        var topic = data.Topics.FirstOrDefault(g => g.Id == cat.TopicId);
                        if (topic != null)
                        {
                            post.SectionIds = new[] { _topicsMap[topic.Id].Id };
                            post.SiteIds    = _topicsMap[topic.Id].SiteIds;
                        }
                    }

                    posts.Add(post);
                    RegisterRedirect(cat.FullUrl, _linkGenerator.GeneratePublicUrl(post, site).ToString());
                }
            }
        }