UpnpPhoto GetPhoto(FSpotPhoto photo, Container parent)
        {
            UpnpPhoto upnp_photo = null;

            if (!photos_cache.ContainsKey(photo.Id))
            {
                var resource_options = new ResourceOptions {
                    ProtocolInfo = new ProtocolInfo(Protocols.HttpGet, MimeTypeHelper.GetMimeType(photo.DefaultVersion.Uri))
                };

                var resource_uri = new Uri(string.Format("{0}object?id={1}", prefix, upnp_photo.Id));

                var photo_options = new PhotoOptions {
                    Title       = photo.Name,
                    Rating      = photo.Rating.ToString(),
                    Description = photo.Description,
                    Resources   = new [] { new Resource(resource_uri, resource_options) }
                };

                upnp_photo = new UpnpPhoto((id++).ToString(), parent.Id, photo_options);

                photos_cache.Add(photo.Id, upnp_photo);
            }
            else
            {
                upnp_photo = photos_cache [photo.Id];
            }

            return(upnp_photo);
        }
        void ServePhoto(HttpListenerResponse response, string query)
        {
            var id      = query.Substring(4);
            var photoId = photos_cache.Where((kv) => kv.Value.Id == id).FirstOrDefault().Key;

            var photo = db.Photos.Get(photoId);

            using (response) {
                if (photo == null)
                {
                    response.StatusCode = 404;
                    return;
                }

                try {
                    using (var reader = System.IO.File.OpenRead(photo.DefaultVersion.Uri.AbsolutePath)) {
                        response.ContentType     = MimeTypeHelper.GetMimeType(photo.DefaultVersion.Uri);
                        response.ContentLength64 = reader.Length;
                        using (var stream = response.OutputStream) {
                            using (var writer = new System.IO.BinaryWriter(stream)) {
                                var buffer = new byte[8192];
                                int read;
                                do
                                {
                                    read = reader.Read(buffer, 0, buffer.Length);
                                    writer.Write(buffer, 0, read);
                                } while (started && read > 0);
                            }
                        }
                    }
                } catch {
                }
            }
        }