示例#1
0
        protected override async Task <bool> AfterSaveAsync(Post <TUserPk> item, PropertyChange[]?changes = null,
                                                            Post <TUserPk>?oldItem = null, IBioRepositoryOperationContext?operationContext = null)
        {
            var version = new PostVersion <TUserPk> {
                Id = Guid.NewGuid(), ContentId = item.Id
            };

            version.SetContent(item);
            if (operationContext is IUserBioRepositoryOperationContext <TUserPk> userBioRepositoryOperationContext)
            {
                version.ChangeAuthorId = userBioRepositoryOperationContext.User.Id;
            }

            DbContext.Add(version);
            await DbContext.SaveChangesAsync();

            return(await base.AfterSaveAsync(item, changes, oldItem, operationContext));
        }
        public async Task ImportAsync(Guid siteId, Export data)
        {
            _logger.LogCritical("Begin import");
            await PrintStatsAsync();

            var transaction = await _dbContext.Database.BeginTransactionAsync();

            var site = await _dbContext.Sites.FirstOrDefaultAsync(s => s.Id == siteId);

            if (site == null)
            {
                throw new Exception($"Site with id {siteId.ToString()} not found");
            }

            _tags = await _dbContext.Tags.ToListAsync();

            _filesUploader.BeginBatch();
            _propertiesProvider.BeginBatch();
            _propertiesProvider.DisableChecks();

            try
            {
                _logger.LogInformation($"Developers: {data.Developers.Count.ToString()}");

                _redirects = new Dictionary <string, string>();

                _developersMap = new Dictionary <int, Developer>();
                await ImportDevelopersAsync(data, site);

                _logger.LogInformation($"Games: {data.Games.Count.ToString()}");
                _gamesMap = new Dictionary <int, Game>();
                await ImportGamesAsync(data, site);

                _logger.LogInformation($"Topics: {data.Topics.Count.ToString()}");
                _topicsMap = new Dictionary <int, Topic>();
                await ImportTopicsAsync(data, site);

                var posts   = new List <Post <string> >();
                var newsMap = new Dictionary <NewsExport, Post <string> >();
                if (_options.ImportNews)
                {
                    _logger.LogWarning("News");
                    await ImportNewsAsync(data, site, posts, newsMap);
                }

                //articles
                if (_options.ImportArticles)
                {
                    _logger.LogWarning("Articles");
                    await ImportArticlesAsync(data, site, posts);
                }

                // files
                if (_options.ImportFiles)
                {
                    _logger.LogWarning("Files");
                    ImportFiles(data, site, posts);
                }

                // pictures
                if (_options.ImportGallery)
                {
                    _logger.LogWarning("Gallery");
                    await ImportGalleryAsync(data, site, posts);
                }

                if (posts.Any())
                {
                    var urls         = posts.Select(p => p.Url).ToArray();
                    var existedPosts = await _dbContext.Set <Post <string> >().Where(p => urls.Contains(p.Url))
                                       .ToListAsync();

                    var blocks = await BlocksHelper.GetBlocksAsync(existedPosts.ToArray(), _dbContext);

                    foreach (var existedPost in existedPosts)
                    {
                        existedPost.Blocks = blocks[existedPost.Id];
                    }

                    var toRemove = new List <ContentBlock>();
                    foreach (var post in posts.OrderBy(p => p.DateAdded))
                    {
                        var version = new PostVersion <string> {
                            Id = Guid.NewGuid()
                        };
                        var existedPost = existedPosts.FirstOrDefault(p => p.Url == post.Url);
                        if (existedPost != null)
                        {
                            toRemove.AddRange(existedPost.Blocks);
                            existedPost.Blocks = new List <ContentBlock>();
                            foreach (ContentBlock block in post.Blocks)
                            {
                                block.ContentId = existedPost.Id;
                                existedPost.Blocks.Add(block);
                                _dbContext.Blocks.Add(block);
                            }

                            existedPost.Title      = post.Title;
                            existedPost.SectionIds = post.SectionIds;
                            existedPost.SiteIds    = post.SiteIds;
                            _dbContext.Update(existedPost);
                            _dbContext.Add(existedPost.Blocks);
                            version.ContentId = existedPost.Id;
                            version.SetContent(existedPost);
                        }
                        else
                        {
                            await _dbContext.AddAsync(post);

                            version.ContentId = post.Id;
                            version.SetContent(post);
                        }

                        version.ChangeAuthorId = post.AuthorId;
                        await _dbContext.AddAsync(version);
                    }

                    if (toRemove.Any())
                    {
                        _dbContext.Blocks.RemoveRange(toRemove);
                    }

                    _logger.LogWarning("Properties");
                    foreach (var(news, post) in newsMap)
                    {
                        if (news.TwitterId > 0)
                        {
                            _dbContext.Add(new TwitterPublishRecord
                            {
                                Type      = post.GetKey(),
                                ContentId = post.Id,
                                TweetId   = news.TwitterId.Value,
                                SiteIds   = post.SiteIds
                            });
                        }

                        if (!string.IsNullOrEmpty(news.FacebookId))
                        {
                            _dbContext.Add(new FacebookPublishRecord
                            {
                                Type      = post.GetKey(),
                                ContentId = post.Id,
                                PostId    = news.FacebookId,
                                SiteIds   = post.SiteIds
                            });
                        }

                        if (news.ForumTopicId > 0 && news.ForumPostId > 0)
                        {
                            _dbContext.Add(new IPBPublishRecord
                            {
                                Type      = post.GetKey(),
                                ContentId = post.Id,
                                TopicId   = news.ForumTopicId.Value,
                                PostId    = news.ForumPostId.Value,
                                SiteIds   = post.SiteIds
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                Rollback(transaction);
                return;
            }

            await _dbContext.SaveChangesAsync();

            await PrintStatsAsync();

            transaction.Commit();

            // export redirects
            var redirects = string.Join("\n", _redirects.Select(p => $"{p.Key}          {p.Value};"));
            var nginxMap  = "map $request_uri $redirect_uri {\n" + redirects + "\n}";

            File.WriteAllText($"{_options.OutputPath}/redirects.conf", nginxMap);
            _logger.LogCritical("Done!");
        }