Пример #1
0
        private async Task <byte[]?> ReadArticleAsset(ArticleMeta meta, string assetKey)
        {
            ArticleAssets?cachedAssets = null;

            if (_cachedAssets.ContainsKey(meta.Id))
            {
                cachedAssets            = _cachedAssets[meta.Id];
                var(_, foundAssetBytes) = cachedAssets.Assets.FirstOrDefault(a => a.AssetKey == assetKey);
                if (foundAssetBytes != default)
                {
                    return(foundAssetBytes);
                }
            }

            var articlePath = $"{_config.BlogContentPath}/{meta.FileKey}/";

            var assetFileNames = Directory.GetFiles(articlePath)
                                 .Select(Path.GetFileName)
                                 .Where(f => string.Equals(f, assetKey, StringComparison.InvariantCultureIgnoreCase))
                                 .ToList();

            if (assetFileNames.Count == 0)
            {
                return(null);
            }

            if (assetFileNames.Count > 1)
            {
                throw new Exception($"Duplicate assets with filename '{assetKey}'");
            }

            if (cachedAssets == null)
            {
                cachedAssets = new ArticleAssets(meta.Id, meta.FileKey, new List <(string, byte[])>());
                _cachedAssets.Add(meta.Id, cachedAssets);
            }

            var assetPath = $"{articlePath}{assetFileNames[0]}";

            Assert.True(File.Exists(assetPath), $"Asset with path and filename '{assetFileNames[0]}' could not be found");

            try
            {
                var file = await File.ReadAllBytesAsync(assetPath);

                cachedAssets.Assets.Add((assetFileNames[0], file));
                return(file);
            }
            catch (Exception e)
            {
                throw new Exception($"Error reading asset with filename '{assetFileNames[0]}'", e);
            }
        }
Пример #2
0
        private async Task <Article> ReadArticleContent(string fileName, ArticleMeta meta)
        {
            var contentPath = $"{_config.BlogContentPath}/{fileName}/{fileName}{ContentFileExtension}";

            Assert.True(File.Exists(contentPath), $"File '{contentPath}' does not exist");

            var rawContent = await File.ReadAllTextAsync(contentPath);

            var article = new Article(meta, rawContent);

            _cachedContent.Add(meta.Id, article);

            return(article);
        }
Пример #3
0
 public Article(ArticleMeta meta, string content)
 {
     Meta    = meta;
     Content = content;
 }