2012-07-20 02:22:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2012-08-11 18:07:07 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2012-07-14 20:45:11 +00:00
|
|
|
|
|
2012-07-21 18:39:47 +00:00
|
|
|
|
namespace MediaBrowser.Common.Net.Handlers
|
2012-07-14 20:45:11 +00:00
|
|
|
|
{
|
2012-07-20 02:22:44 +00:00
|
|
|
|
public abstract class BaseEmbeddedResourceHandler : BaseHandler
|
2012-07-14 20:45:11 +00:00
|
|
|
|
{
|
2012-07-20 02:22:44 +00:00
|
|
|
|
public BaseEmbeddedResourceHandler(string resourcePath)
|
|
|
|
|
: base()
|
2012-07-14 20:45:11 +00:00
|
|
|
|
{
|
|
|
|
|
ResourcePath = resourcePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected string ResourcePath { get; set; }
|
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
public override Task<string> GetContentType()
|
2012-07-14 20:45:11 +00:00
|
|
|
|
{
|
2012-08-19 20:38:31 +00:00
|
|
|
|
return Task.Run(() =>
|
2012-07-14 20:45:11 +00:00
|
|
|
|
{
|
|
|
|
|
string extension = Path.GetExtension(ResourcePath);
|
|
|
|
|
|
|
|
|
|
if (extension.EndsWith("jpeg", StringComparison.OrdinalIgnoreCase) || extension.EndsWith("jpg", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "image/jpeg";
|
|
|
|
|
}
|
|
|
|
|
else if (extension.EndsWith("png", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "image/png";
|
|
|
|
|
}
|
|
|
|
|
else if (extension.EndsWith("ico", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "image/ico";
|
|
|
|
|
}
|
|
|
|
|
else if (extension.EndsWith("js", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "application/x-javascript";
|
|
|
|
|
}
|
|
|
|
|
else if (extension.EndsWith("css", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "text/css";
|
|
|
|
|
}
|
|
|
|
|
else if (extension.EndsWith("html", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "text/html; charset=utf-8";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "text/plain; charset=utf-8";
|
2012-08-19 20:38:31 +00:00
|
|
|
|
});
|
2012-07-14 20:45:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 18:07:07 +00:00
|
|
|
|
protected override Task WriteResponseToOutputStream(Stream stream)
|
2012-07-14 20:45:11 +00:00
|
|
|
|
{
|
2012-08-11 18:07:07 +00:00
|
|
|
|
return GetEmbeddedResourceStream().CopyToAsync(stream);
|
2012-07-14 20:45:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract Stream GetEmbeddedResourceStream();
|
|
|
|
|
}
|
|
|
|
|
}
|