File size: 2,872 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 |
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using VersOne.Epub.Environment;
using VersOne.Epub.Options;
using VersOne.Epub.Utils;
namespace VersOne.Epub.Internal
{
internal class BookRefReader
{
private readonly IEnvironmentDependencies environmentDependencies;
private readonly EpubReaderOptions epubReaderOptions;
public BookRefReader(IEnvironmentDependencies environmentDependencies, EpubReaderOptions? epubReaderOptions = null)
{
this.environmentDependencies = environmentDependencies ?? throw new ArgumentNullException(nameof(environmentDependencies));
this.epubReaderOptions = epubReaderOptions ?? new EpubReaderOptions();
}
public EpubBookRef OpenBook(string filePath)
{
return OpenBookAsync(filePath).ExecuteAndUnwrapAggregateException();
}
public EpubBookRef OpenBook(Stream stream)
{
return OpenBookAsync(stream).ExecuteAndUnwrapAggregateException();
}
public Task<EpubBookRef> OpenBookAsync(string filePath)
{
if (!environmentDependencies.FileSystem.FileExists(filePath))
{
throw new FileNotFoundException("Specified EPUB file not found.", filePath);
}
return OpenBookAsync(GetZipFile(filePath), filePath);
}
public Task<EpubBookRef> OpenBookAsync(Stream stream)
{
return OpenBookAsync(GetZipFile(stream), null);
}
private async Task<EpubBookRef> OpenBookAsync(IZipFile epubFile, string? filePath)
{
SchemaReader schemaReader = new(epubReaderOptions);
EpubSchema schema = await schemaReader.ReadSchemaAsync(epubFile).ConfigureAwait(false);
string title = schema.Package.Metadata.Titles.FirstOrDefault()?.Title ?? String.Empty;
List<string> authorList = schema.Package.Metadata.Creators.Select(creator => creator.Creator).ToList();
string author = String.Join(", ", authorList);
string? description = schema.Package.Metadata.Descriptions.FirstOrDefault()?.Description;
ContentReader contentReader = new(environmentDependencies, epubReaderOptions);
EpubContentRef content = await Task.Run(() => contentReader.ParseContentMap(schema, epubFile)).ConfigureAwait(false);
return new(epubFile, filePath, title, author, authorList, description, schema, content, epubReaderOptions);
}
private IZipFile GetZipFile(string filePath)
{
return environmentDependencies.FileSystem.OpenZipFile(filePath);
}
private IZipFile GetZipFile(Stream stream)
{
return environmentDependencies.FileSystem.OpenZipFile(stream);
}
}
}
|