public DlnaProtocolInfo GetProtocolInfo(MediaItem item) { var info = new DlnaProtocolInfo { Protocol = "http-get", Network = "*", MediaType = MediaLibraryHelper.GetOrGuessMimeType(item), AdditionalInfo = new DlnaForthField() }; ConfigureProfile(info.AdditionalInfo, item, info.MediaType); return(info); }
public override bool Process(IHttpRequest request, IHttpResponse response, IHttpSession session) { var uri = request.Uri; // Check the request path to see if it's for us. if (!uri.AbsolutePath.StartsWith(DlnaResourceAccessUtils.RESOURCE_ACCESS_PATH)) { return(false); } // Grab the media item given in the request. Guid mediaItemGuid; if (!DlnaResourceAccessUtils.ParseMediaItem(uri, out mediaItemGuid)) { throw new BadRequestException(string.Format("Illegal request syntax. Correct syntax is '{0}'", DlnaResourceAccessUtils.SYNTAX)); } try { // Attempt to grab the media item from the database. var item = MediaLibraryHelper.GetMediaItem(mediaItemGuid); if (item == null) { throw new BadRequestException(string.Format("Media item '{0}' not found.", mediaItemGuid)); } // Grab the mimetype from the media item and set the Content Type header. var mimeType = MediaLibraryHelper.GetOrGuessMimeType(item); if (mimeType == null) { throw new InternalServerException("Media item has bad mime type, re-import media item"); } response.ContentType = mimeType; // Grab the resource path for the media item. var resourcePathStr = item.Aspects[ProviderResourceAspect.ASPECT_ID].GetAttributeValue( ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH); var resourcePath = ResourcePath.Deserialize(resourcePathStr.ToString()); var ra = GetResourceAccessor(resourcePath); using (var resourceStream = ra.OpenRead()) { // HTTP/1.1 RFC2616 section 14.25 'If-Modified-Since' if (!string.IsNullOrEmpty(request.Headers["If-Modified-Since"])) { DateTime lastRequest = DateTime.Parse(request.Headers["If-Modified-Since"]); if (lastRequest.CompareTo(ra.LastChanged) <= 0) { response.Status = HttpStatusCode.NotModified; } } // HTTP/1.1 RFC2616 section 14.29 'Last-Modified' response.AddHeader("Last-Modified", ra.LastChanged.ToUniversalTime().ToString("r")); // DLNA Requirement: [7.4.26.1-6] // Since the DLNA spec allows contentFeatures.dlna.org with any request, we'll put it in. if (!string.IsNullOrEmpty(request.Headers["getcontentFeatures.dlna.org"])) { if (request.Headers["getcontentFeatures.dlna.org"] != "1") { // DLNA Requirement [7.4.26.5] throw new BadRequestException("Illegal value for getcontentFeatures.dlna.org"); } } var dlnaString = DlnaProtocolInfoFactory.GetProfileInfo(item).ToString(); response.AddHeader("contentFeatures.dlna.org", dlnaString); // DLNA Requirement: [7.4.55-57] // TODO: Bad implementation of requirement if (!string.IsNullOrEmpty(request.Headers["transferMode.dlna.org"])) { if (request.Headers["transferMode.dlna.org"] == "Streaming") { response.AddHeader("transferMode.dlna.org", "Streaming"); } if (request.Headers["transferMode.dlna.org"] == "Interactive") { response.AddHeader("transferMode.dlna.org", "Interactive"); } if (request.Headers["transferMode.dlna.org"] == "Background") { response.AddHeader("transferMode.dlna.org", "Background"); } } string byteRangesSpecifier = request.Headers["Range"]; IList <Range> ranges = ParseRanges(byteRangesSpecifier, resourceStream.Length); bool onlyHeaders = request.Method == Method.Header || response.Status == HttpStatusCode.NotModified; if (ranges != null && ranges.Count == 1) { // We only support one range SendRange(response, resourceStream, ranges[0], onlyHeaders); } else { SendWholeFile(response, resourceStream, onlyHeaders); } } } catch (FileNotFoundException ex) { throw new InternalServerException(string.Format("Failed to proccess media item '{0}'", mediaItemGuid), ex); } return(true); }