Exemple #1
0
        public HttpResponse HandleRequest(HttpRequest request)
        {
            string  str      = request.Path.Substring(this.Prefix.Length - 1);
            Content itemById = this.dataLibrary.GetItemById(new Guid(str), this.dataLibrary.RootFolder) as Content;

            if (itemById == null)
            {
                return(new HttpResponse(request, HttpCode.NotFound, "text/html", ""));
            }
            string       mimeByFileType = MimeTypeSolver.GetMimeByFileType(Path.GetExtension(itemById.Path));
            HttpResponse httpResponse   = new HttpResponse(request, HttpCode.Ok, mimeByFileType, new FileStream(itemById.Path, FileMode.Open, FileAccess.Read));

            httpResponse.Headers["Accept-Ranges"]         = "bytes";
            httpResponse.Headers["transferMode.dlna.org"] = "Streaming";
            if (request.Headers.ContainsKey("getcontentFeatures.dlna.org"))
            {
                if (!mimeByFileType.StartsWith("image"))
                {
                    httpResponse.Headers["contentFeatures.dlna.org"] = string.Format("{0};DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS={1}", DLNAMaps.GetPNByMime(mimeByFileType), DLNAMaps.DefaultStreaming);
                }
                else
                {
                    httpResponse.Headers["contentFeatures.dlna.org"] = string.Format("{0};DLNA.ORG_OP=00;DLNA.ORG_CI=0;DLNA.ORG_FLAGS={1}", DLNAMaps.GetPNByMime(mimeByFileType), DLNAMaps.DefaultInteractive);
                }
            }
            return(httpResponse);
        }
        private XElement CreateItem(ItemBase resource)
        {
            Content content = resource as Content;
            XName   xName   = this.didl + "item";

            object[] xAttribute = new object[] { new XAttribute("restricted", (object)1), new XAttribute("id", (object)resource.ID), new XAttribute("parentID", (object)content.Parent) };
            XElement xElement   = new XElement(xName, xAttribute);

            xElement.Add(this.CreateObjectClass(resource));
            xElement.Add(new XElement(this.dc + "title", resource.Name));
            XName    xName1 = this.dc + "date";
            DateTime date   = content.Date;

            xElement.Add(new XElement(xName1, date.ToString("o")));
            Track track = content as Track;

            if (track != null)
            {
                xElement.Add(new XElement(this.upnp + "genre", track.Genre.Name));
                xElement.Add(new XElement(this.upnp + "artist", track.Artist.Name));
                xElement.Add(new XElement(this.upnp + "album", track.Album.Name));
                if (track.Preview != null && File.Exists(track.Preview.AbsolutePath))
                {
                    XName    xName2   = this.upnp + "albumArtURI";
                    object[] objArray = new object[] { new XAttribute(this.dlna + "profileID", "JPEG_TN"), string.Format("{0}/cover/{1}", this.httpServer.ServerUrl, resource.ID) };
                    xElement.Add(new XElement(xName2, objArray));
                }
            }
            string mimeByFileType = MimeTypeSolver.GetMimeByFileType(Path.GetExtension(content.Path));
            XName  xName3         = this.didl + "res";

            object[] xAttribute1 = new object[] { new XAttribute("size", (object)(new FileInfo(content.Path)).Length), new XAttribute("protocolInfo", string.Format("http-get:*:{1}:{0};DLNA.ORG_OP=01;DLNA.ORG_OP=01;DLNA.ORG_FLAGS={2}", DLNAMaps.GetPNByMime(mimeByFileType), mimeByFileType, DLNAMaps.DefaultStreaming)), string.Format("{0}/file/{1}", this.httpServer.ServerUrl, resource.ID) };
            xElement.Add(new XElement(xName3, xAttribute1));
            return(xElement);
        }
Exemple #3
0
        public HttpResponse HandleRequest(HttpRequest request)
        {
            string str = request.Path.Substring(this.Prefix.Length - 1);

            char[]  chrArray = new char[] { '.' };
            string  str1     = str.Split(chrArray)[0];
            Content content  = this.sharedFiles.First <Content>((Content f) => f.ID == new Guid(str1));

            if (content == null)
            {
                return(new HttpResponse(request, HttpCode.NotFound, "text/html", ""));
            }
            string mimeByFileType = MimeTypeSolver.GetMimeByFileType(Path.GetExtension(content.Path));
            Stream fileStream     = new FileStream(content.Path, FileMode.Open, FileAccess.Read);

            if (mimeByFileType.StartsWith("image"))
            {
                using (Image image = Image.FromStream(fileStream))
                {
                    if (image.Width > 15360 || image.Height > 8640)
                    {
                        fileStream = new MemoryStream();
                        double num = Math.Min(15360 / (double)image.Width, 8640 / (double)image.Height);
                        (new Bitmap(image, new Size((int)((double)image.Width * num), (int)((double)image.Height * num)))).Save(fileStream, image.RawFormat);
                    }
                }
            }
            HttpResponse httpResponse = new HttpResponse(request, HttpCode.Ok, mimeByFileType, fileStream);

            httpResponse.Headers["Accept-Ranges"] = "bytes";
            return(httpResponse);
        }