Пример #1
0
        public void DeleteUnusedBlogPosts()
        {
            var logosPostsPath = Path.Combine(_env.WebRootPath, "Blog", "Logos");

            var logos = Directory.GetFiles(logosPostsPath);

            var logosInfo = _repo
                            .GetLogosInfo()
                            .Select(logoinfo => new { LogoId = logoinfo.Id.ToString(), LogoExtenssion = logoinfo.Extension })
                            .ToList();

            logos.ForEach(logoPath =>
            {
                var extension = logoPath
                                .Split("\\")
                                .LastOrDefault()
                                .Split(".")
                                .LastOrDefault();

                var fileNameWithExtension = logoPath
                                            .Split("\\")
                                            .LastOrDefault();

                var fileName = fileNameWithExtension.Substring(0, fileNameWithExtension.Length - (extension.Length + 1));

                var logoInfoMatched = logosInfo
                                      .Where(info => (extension == info.LogoExtenssion || extension == "tmstmp") &&
                                             info.LogoId == fileName)
                                      .LastOrDefault() != null;

                if (!logoInfoMatched)
                {
                    File.Delete(logoPath);
                    _logger.LogInformation($"File {fileName}.{extension} was deleted");
                }
            });
        }
 public IEnumerable <LogoInfo> GetLogosInfo() => _repository.GetLogosInfo();