<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Blogifier.Core.Extensions;
using Microsoft.AspNetCore.Http;
using Blogifier.Core.Common;

namespace Blogifier.Core.Middleware
{
	public class EmbeddedResources
	{
		readonly RequestDelegate _next;
		List<string> _resources;
		Assembly _assembly;

		public EmbeddedResources(RequestDelegate next)
		{
			_next = next;
			_resources = new List<string>();
			_assembly = typeof(EmbeddedResources).GetTypeInfo().Assembly;

			foreach (var name in _assembly.GetManifestResourceNames())
			{
				if (name.Contains("Blogifier.Core.Embedded") && Include(name))
				{
					_resources.Add(name);
				}
			}
		}

		public async Task Invoke(HttpContext context)
		{
			var path = context.Request.Path.ToString().ToLower().Replace("/", ".");

			if (path.Contains(".embedded.", StringComparison.OrdinalIgnoreCase))
			{
				try
				{
					var resource = _resources.Where(r => r.Contains(path, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

					if (!string.IsNullOrEmpty(resource))
					{
						var stream = _assembly.GetManifestResourceStream(resource);

                        // if (Common.ApplicationSettings.OSDescription.Contains("Linux", StringComparison.OrdinalIgnoreCase))
                        if (ApplicationSettings.AddContentTypeHeaders)
						{
							context.Response.Headers.Add("Content-Length", stream.Length.ToString());
							context.Response.Headers.Remove("Content-Type");

							if (resource.EndsWith(".css", StringComparison.OrdinalIgnoreCase))
							{
								context.Response.Headers.Add("Content-Type", "text/css");
							}
							if (resource.EndsWith(".js", StringComparison.OrdinalIgnoreCase))
							{
								context.Response.Headers.Add("Content-Type", "application/javascript");
							}
							if (resource.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase))
							{
								context.Response.Headers.Add("Content-Type", "image/jpeg");
							}
							if (resource.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
							{
								context.Response.Headers.Add("Content-Type", "image/png");
							}
						}
						await stream.CopyToAsync(context.Response.Body);
					}
				}
				catch(Exception ex)
				{
					throw new Exception("Exception invoking embedded resources: " + ex.Message);
				}
			}
			await _next.Invoke(context);
		}

		bool Include(string resource)
		{
			if (resource.EndsWith(".css", StringComparison.OrdinalIgnoreCase) ||
				resource.EndsWith(".js", StringComparison.OrdinalIgnoreCase) ||
				resource.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
				resource.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
				resource.EndsWith(".woff", StringComparison.OrdinalIgnoreCase) ||
				resource.EndsWith(".ttf", StringComparison.OrdinalIgnoreCase)
				)
				return true;

			return false;
		}
	}
}<commit_msg>font added to content types<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Blogifier.Core.Extensions;
using Microsoft.AspNetCore.Http;
using Blogifier.Core.Common;

namespace Blogifier.Core.Middleware
{
	public class EmbeddedResources
	{
		readonly RequestDelegate _next;
		List<string> _resources;
		Assembly _assembly;

		public EmbeddedResources(RequestDelegate next)
		{
			_next = next;
			_resources = new List<string>();
			_assembly = typeof(EmbeddedResources).GetTypeInfo().Assembly;

			foreach (var name in _assembly.GetManifestResourceNames())
			{
				if (name.Contains("Blogifier.Core.Embedded") && Include(name))
				{
					_resources.Add(name);
				}
			}
		}

		public async Task Invoke(HttpContext context)
		{
			var path = context.Request.Path.ToString().ToLower().Replace("/", ".");

			if (path.Contains(".embedded.", StringComparison.OrdinalIgnoreCase))
			{
				try
				{
					var resource = _resources.Where(r => r.Contains(path, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

					if (!string.IsNullOrEmpty(resource))
					{
						var stream = _assembly.GetManifestResourceStream(resource);

                        // if (Common.ApplicationSettings.OSDescription.Contains("Linux", StringComparison.OrdinalIgnoreCase))
                        if (ApplicationSettings.AddContentTypeHeaders)
						{
							context.Response.Headers.Add("Content-Length", stream.Length.ToString());
							context.Response.Headers.Remove("Content-Type");

							if (resource.EndsWith(".css", StringComparison.OrdinalIgnoreCase))
							{
								context.Response.Headers.Add("Content-Type", "text/css");
							}
							if (resource.EndsWith(".js", StringComparison.OrdinalIgnoreCase))
							{
								context.Response.Headers.Add("Content-Type", "application/javascript");
							}
							if (resource.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase))
							{
								context.Response.Headers.Add("Content-Type", "image/jpeg");
							}
							if (resource.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
							{
								context.Response.Headers.Add("Content-Type", "image/png");
							}
						}
						await stream.CopyToAsync(context.Response.Body);
					}
				}
				catch(Exception ex)
				{
					throw new Exception("Exception invoking embedded resources: " + ex.Message);
				}
			}
			await _next.Invoke(context);
		}

		bool Include(string resource)
		{
			if (resource.EndsWith(".css", StringComparison.OrdinalIgnoreCase) ||
				resource.EndsWith(".js", StringComparison.OrdinalIgnoreCase) ||
				resource.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
				resource.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
				resource.EndsWith(".woff", StringComparison.OrdinalIgnoreCase) ||
                resource.EndsWith(".woff2", StringComparison.OrdinalIgnoreCase) ||
                resource.EndsWith(".ttf", StringComparison.OrdinalIgnoreCase)
				)
				return true;

			return false;
		}
	}
}
{'fraction_non_alphanumeric': 0.12778056531192508, 'fraction_numerical': 0.00016725204883759825, 'mean_word_length': 3.393828067597355, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7314751', 'n_tokens_mistral': 2160, 'n_tokens_neox': 1678, 'n_words': 266}