|
| 1 | +using MediaBrowser.Controller.Dlna; |
| 2 | +using ServiceStack; |
| 3 | +using ServiceStack.Web; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace MediaBrowser.Api.Dlna |
| 9 | +{ |
| 10 | + [Route("/Dlna/{UuId}/description.xml", "GET", Summary = "Gets dlna server info")] |
| 11 | + [Route("/Dlna/{UuId}/description", "GET", Summary = "Gets dlna server info")] |
| 12 | + public class GetDescriptionXml |
| 13 | + { |
| 14 | + [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")] |
| 15 | + public string UuId { get; set; } |
| 16 | + } |
| 17 | + |
| 18 | + [Route("/Dlna/{UuId}/contentdirectory.xml", "GET", Summary = "Gets dlna content directory xml")] |
| 19 | + [Route("/Dlna/{UuId}/contentdirectory", "GET", Summary = "Gets the content directory xml")] |
| 20 | + public class GetContentDirectory |
| 21 | + { |
| 22 | + [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")] |
| 23 | + public string UuId { get; set; } |
| 24 | + } |
| 25 | + |
| 26 | + [Route("/Dlna/{UuId}/control", "POST", Summary = "Processes a control request")] |
| 27 | + public class ProcessControlRequest : IRequiresRequestStream |
| 28 | + { |
| 29 | + [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")] |
| 30 | + public string UuId { get; set; } |
| 31 | + |
| 32 | + public Stream RequestStream { get; set; } |
| 33 | + } |
| 34 | + |
| 35 | + public class DlnaServerService : BaseApiService |
| 36 | + { |
| 37 | + private readonly IDlnaManager _dlnaManager; |
| 38 | + |
| 39 | + public DlnaServerService(IDlnaManager dlnaManager) |
| 40 | + { |
| 41 | + _dlnaManager = dlnaManager; |
| 42 | + } |
| 43 | + |
| 44 | + public object Get(GetDescriptionXml request) |
| 45 | + { |
| 46 | + var xml = _dlnaManager.GetServerDescriptionXml(GetRequestHeaders(), request.UuId); |
| 47 | + |
| 48 | + return ResultFactory.GetResult(xml, "text/xml"); |
| 49 | + } |
| 50 | + |
| 51 | + public object Get(GetContentDirectory request) |
| 52 | + { |
| 53 | + var xml = _dlnaManager.GetContentDirectoryXml(GetRequestHeaders()); |
| 54 | + |
| 55 | + return ResultFactory.GetResult(xml, "text/xml"); |
| 56 | + } |
| 57 | + |
| 58 | + public object Post(ProcessControlRequest request) |
| 59 | + { |
| 60 | + var response = PostAsync(request).Result; |
| 61 | + |
| 62 | + return ResultFactory.GetResult(response.Xml, "text/xml"); |
| 63 | + } |
| 64 | + |
| 65 | + private async Task<ControlResponse> PostAsync(ProcessControlRequest request) |
| 66 | + { |
| 67 | + using (var reader = new StreamReader(request.RequestStream)) |
| 68 | + { |
| 69 | + return _dlnaManager.ProcessControlRequest(new ControlRequest |
| 70 | + { |
| 71 | + Headers = GetRequestHeaders(), |
| 72 | + InputXml = await reader.ReadToEndAsync().ConfigureAwait(false) |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private IDictionary<string, string> GetRequestHeaders() |
| 78 | + { |
| 79 | + var headers = new Dictionary<string, string>(); |
| 80 | + |
| 81 | + foreach (var key in Request.Headers.AllKeys) |
| 82 | + { |
| 83 | + headers[key] = Request.Headers[key]; |
| 84 | + } |
| 85 | + |
| 86 | + return headers; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments