2012-07-14 20:45:11 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2012-07-21 18:39:47 +00:00
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
2012-07-14 20:45:11 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
{
|
2012-07-20 02:22:44 +00:00
|
|
|
|
class MediaHandler : BaseHandler
|
2012-07-14 20:45:11 +00:00
|
|
|
|
{
|
|
|
|
|
private string _MediaPath = string.Empty;
|
|
|
|
|
private string MediaPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(_MediaPath))
|
|
|
|
|
{
|
|
|
|
|
_MediaPath = GetMediaPath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _MediaPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetMediaPath()
|
|
|
|
|
{
|
|
|
|
|
string path = QueryString["path"] ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BaseItem item = ApiService.GetItemById(QueryString["id"]);
|
|
|
|
|
|
|
|
|
|
return item.Path;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
public override bool GzipResponse
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-14 20:45:11 +00:00
|
|
|
|
public override string ContentType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// http://www.codingcereal.com/2011/10/an-array-of-45-video-mime-types/
|
|
|
|
|
|
|
|
|
|
string extension = Path.GetExtension(MediaPath);
|
|
|
|
|
|
|
|
|
|
if (extension.EndsWith("mkv", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "video/x-matroska";
|
|
|
|
|
}
|
|
|
|
|
else if (extension.EndsWith("avi", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "video/avi";
|
|
|
|
|
}
|
|
|
|
|
else if (extension.EndsWith("wmv", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "video/wmv";
|
|
|
|
|
}
|
|
|
|
|
else if (extension.EndsWith("m4v", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "video/m4v";
|
|
|
|
|
}
|
|
|
|
|
else if (extension.EndsWith("flv", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "video/flv";
|
|
|
|
|
}
|
|
|
|
|
else if (extension.EndsWith("mov", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "video/quicktime";
|
|
|
|
|
}
|
|
|
|
|
else if (extension.EndsWith("mp4", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "video/mp4";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "video/x-matroska";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
protected override void WriteResponseToOutputStream(Stream stream)
|
2012-07-14 20:45:11 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (Stream input = File.OpenRead(MediaPath))
|
|
|
|
|
{
|
|
|
|
|
input.CopyTo(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|