aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs
blob: 89e13acb38948a2c4a0760e8b6d9a8d83260108b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using ServiceStack.Service;
using ServiceStack.ServiceHost;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

namespace MediaBrowser.Api.Playback
{
    /// <summary>
    /// Class StaticRemoteStreamWriter
    /// </summary>
    public class StaticRemoteStreamWriter : IStreamWriter, IHasOptions
    {
        /// <summary>
        /// The _input stream
        /// </summary>
        private readonly HttpResponseMessage _msg;

        private readonly HttpClient _client;

        /// <summary>
        /// The _options
        /// </summary>
        private readonly IDictionary<string, string> _options = new Dictionary<string, string>();

        /// <summary>
        /// Initializes a new instance of the <see cref="StaticRemoteStreamWriter"/> class.
        /// </summary>
        public StaticRemoteStreamWriter(HttpResponseMessage msg, HttpClient client)
        {
            _msg = msg;
            _client = client;
        }

        /// <summary>
        /// Gets the options.
        /// </summary>
        /// <value>The options.</value>
        public IDictionary<string, string> Options
        {
            get { return _options; }
        }

        /// <summary>
        /// Writes to.
        /// </summary>
        /// <param name="responseStream">The response stream.</param>
        public void WriteTo(Stream responseStream)
        {
            var task = WriteToAsync(responseStream);

            Task.WaitAll(task);
        }

        /// <summary>
        /// Writes to async.
        /// </summary>
        /// <param name="responseStream">The response stream.</param>
        /// <returns>Task.</returns>
        public async Task WriteToAsync(Stream responseStream)
        {
            using (_client)
            {
                using (_msg)
                {
                    using (var input = await _msg.Content.ReadAsStreamAsync().ConfigureAwait(false))
                    {
                        await input.CopyToAsync(responseStream).ConfigureAwait(false);
                    }
                }
            }
        }
    }
}