File size: 4,490 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.IO;
using System.Threading.Tasks;

namespace VersOne.Epub
{
    /// <summary>
    /// The base class for a content file reference located outside of the EPUB archive (e.g., an HTML file or an image).
    /// Unlike <see cref="EpubRemoteContentFile" />, the classes derived from this base class contain only a reference to the file but don't contain its content.
    /// </summary>
    public abstract class EpubRemoteContentFileRef : EpubContentFileRef
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EpubRemoteContentFileRef" /> class with a specified content file key, a content type of the file reference,
        /// a MIME type of the file's content, and content downloader options.
        /// </summary>
        /// <param name="metadata">Metadata of this content file reference.</param>
        /// <param name="epubContentLoader">A reference to the EPUB content loader which provides methods to download the content of this file.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="metadata" /> parameter is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="epubContentLoader" /> parameter is <c>null</c>.</exception>
        protected EpubRemoteContentFileRef(EpubContentFileRefMetadata metadata, IEpubContentLoader epubContentLoader)
            : base(metadata, epubContentLoader)
        {
        }

        /// <summary>
        /// Gets the absolute URI of the remote content file (as it is specified in the EPUB manifest).
        /// </summary>
        public string Url => Key;

        /// <summary>
        /// Gets the location of the content file which is always <see cref="EpubContentLocation.REMOTE" /> for remote content file references.
        /// </summary>
        public override EpubContentLocation ContentLocation => EpubContentLocation.REMOTE;

        /// <summary>
        /// Downloads the whole content of the referenced file and returns it as a byte array.
        /// </summary>
        /// <returns>Content of the referenced file.</returns>
        public byte[] DownloadContentAsBytes()
        {
            return EpubContentLoader.LoadContentAsBytes(Metadata);
        }

        /// <summary>
        /// Asynchronously downloads the whole content of the referenced file and returns it as a byte array.
        /// </summary>
        /// <returns>A task that represents the asynchronous download operation. The value of the TResult parameter contains the content of the referenced file.</returns>
        public Task<byte[]> DownloadContentAsBytesAsync()
        {
            return EpubContentLoader.LoadContentAsBytesAsync(Metadata);
        }

        /// <summary>
        /// Downloads the whole content of the referenced file and returns it as a string.
        /// </summary>
        /// <returns>Content of the referenced file.</returns>
        public string DownloadContentAsText()
        {
            return EpubContentLoader.LoadContentAsText(Metadata);
        }

        /// <summary>
        /// Asynchronously downloads the whole content of the referenced file and returns it as a string.
        /// </summary>
        /// <returns>A task that represents the asynchronous download operation. The value of the TResult parameter contains the content of the referenced file.</returns>
        public Task<string> DownloadContentAsTextAsync()
        {
            return EpubContentLoader.LoadContentAsTextAsync(Metadata);
        }

        /// <summary>
        /// Starts the download of the referenced file and returns a <see cref="Stream" /> to access its content.
        /// </summary>
        /// <returns>A <see cref="Stream" /> to access the referenced file's content.</returns>
        public Stream GetDownloadingContentStream()
        {
            return EpubContentLoader.GetContentStream(Metadata);
        }

        /// <summary>
        /// Starts the download of the referenced file and returns a <see cref="Stream" /> to access its content.
        /// </summary>
        /// <returns>
        /// A task that represents the asynchronous download operation.
        /// The value of the TResult parameter contains a <see cref="Stream" /> to access the referenced file's content.
        /// </returns>
        public Task<Stream> GetDownloadingContentStreamAsync()
        {
            return EpubContentLoader.GetContentStreamAsync(Metadata);
        }
    }
}