File size: 5,042 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
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using VersOne.Epub.Environment;
using VersOne.Epub.Options;

namespace VersOne.Epub.Internal
{
    internal class EpubLocalContentLoader : EpubContentLoader
    {
        private readonly ContentReaderOptions contentReaderOptions;
        private readonly IZipFile epubFile;
        private readonly string contentDirectoryPath;
        private readonly Dictionary<string, ReplacementContentFileEntry> replacementContentFileEntries;

        public EpubLocalContentLoader(IEnvironmentDependencies environmentDependencies, IZipFile epubFile, string contentDirectoryPath, ContentReaderOptions? contentReaderOptions = null)
            : base(environmentDependencies)
        {
            this.epubFile = epubFile ?? throw new ArgumentNullException(nameof(epubFile));
            this.contentDirectoryPath = contentDirectoryPath ?? throw new ArgumentNullException(nameof(contentDirectoryPath));
            this.contentReaderOptions = contentReaderOptions ?? new ContentReaderOptions();
            replacementContentFileEntries = new Dictionary<string, ReplacementContentFileEntry>();
        }

        public override async Task<byte[]> LoadContentAsBytesAsync(EpubContentFileRefMetadata contentFileRefMetadata)
        {
            IZipFileEntry contentFileEntry = GetContentFileEntry(contentFileRefMetadata);
            byte[] content = new byte[(int)contentFileEntry.Length];
            using (Stream contentStream = contentFileEntry.Open())
            using (MemoryStream memoryStream = new(content))
            {
                await contentStream.CopyToAsync(memoryStream).ConfigureAwait(false);
            }
            return content;
        }

        public override async Task<string> LoadContentAsTextAsync(EpubContentFileRefMetadata contentFileRefMetadata)
        {
            using Stream contentStream = GetContentFileEntry(contentFileRefMetadata).Open();
            using StreamReader streamReader = new(contentStream);
            return await streamReader.ReadToEndAsync().ConfigureAwait(false);
        }

        public override Task<Stream> GetContentStreamAsync(EpubContentFileRefMetadata contentFileRefMetadata)
        {
            return Task.FromResult(GetContentFileEntry(contentFileRefMetadata).Open());
        }

        private IZipFileEntry GetContentFileEntry(EpubContentFileRefMetadata contentFileRefMetadata)
        {
            if (replacementContentFileEntries.TryGetValue(contentFileRefMetadata.Key, out ReplacementContentFileEntry existingReplacementContentFileEntry))
            {
                return existingReplacementContentFileEntry;
            }
            if (epubFile.IsDisposed)
            {
                throw new ObjectDisposedException(nameof(epubFile), "EPUB file stored within this local content file loader has been disposed.");
            }
            ReplacementContentFileEntry? newReplacementContentFileEntry = null;
            string contentFilePath = ContentPathUtils.Combine(contentDirectoryPath, contentFileRefMetadata.Key);
            IZipFileEntry? contentFileEntry = epubFile.GetEntry(contentFilePath);
            if (contentFileEntry == null)
            {
                newReplacementContentFileEntry = RequestReplacementContentFileEntry(contentFileRefMetadata, contentFilePath);
                contentFileEntry = newReplacementContentFileEntry;
            }
            if (contentFileEntry == null)
            {
                throw new EpubContentException($"EPUB parsing error: file \"{contentFilePath}\" was not found in the EPUB file.", contentFilePath);
            }
            if (contentFileEntry.Length > Int32.MaxValue)
            {
                throw new EpubContentException($"EPUB parsing error: file \"{contentFilePath}\" is larger than 2 GB.", contentFilePath);
            }
            if (newReplacementContentFileEntry != null)
            {
                replacementContentFileEntries.Add(contentFileRefMetadata.Key, newReplacementContentFileEntry);
            }
            return contentFileEntry;
        }

        private ReplacementContentFileEntry? RequestReplacementContentFileEntry(EpubContentFileRefMetadata contentFileRefMetadata, string contentFilePath)
        {
            ContentFileMissingEventArgs contentFileMissingEventArgs =
                new(contentFileRefMetadata.Key, contentFilePath, contentFileRefMetadata.ContentType, contentFileRefMetadata.ContentMimeType);
            contentReaderOptions.RaiseContentFileMissingEvent(contentFileMissingEventArgs);
            if (contentFileMissingEventArgs.ReplacementContentStream != null)
            {
                return new ReplacementContentFileEntry(contentFileMissingEventArgs.ReplacementContentStream);
            }
            else if (contentFileMissingEventArgs.SuppressException)
            {
                return new ReplacementContentFileEntry(new MemoryStream());
            }
            return null;
        }
    }
}