mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-14 20:29:07 +00:00
Included epub library in plugin project
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Compression;
|
||||
using System.Threading.Tasks;
|
||||
using VersOne.Epub.Internal;
|
||||
|
||||
namespace VersOne.Epub
|
||||
{
|
||||
public class EpubBookRef : IDisposable
|
||||
{
|
||||
private bool isDisposed;
|
||||
|
||||
public EpubBookRef(ZipArchive epubArchive)
|
||||
{
|
||||
EpubArchive = epubArchive;
|
||||
isDisposed = false;
|
||||
}
|
||||
|
||||
~EpubBookRef()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
public string FilePath { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Author { get; set; }
|
||||
public List<string> AuthorList { get; set; }
|
||||
public EpubSchema Schema { get; set; }
|
||||
public EpubContentRef Content { get; set; }
|
||||
|
||||
internal ZipArchive EpubArchive { get; private set; }
|
||||
|
||||
public byte[] ReadCover()
|
||||
{
|
||||
return ReadCoverAsync().Result;
|
||||
}
|
||||
|
||||
public async Task<byte[]> ReadCoverAsync()
|
||||
{
|
||||
return await BookCoverReader.ReadBookCoverAsync(this).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public List<EpubChapterRef> GetChapters()
|
||||
{
|
||||
return GetChaptersAsync().Result;
|
||||
}
|
||||
|
||||
public async Task<List<EpubChapterRef>> GetChaptersAsync()
|
||||
{
|
||||
return await Task.Run(() => ChapterReader.GetChapters(this)).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
EpubArchive?.Dispose();
|
||||
}
|
||||
isDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VersOne.Epub
|
||||
{
|
||||
public class EpubByteContentFileRef : EpubContentFileRef
|
||||
{
|
||||
public EpubByteContentFileRef(EpubBookRef epubBookRef)
|
||||
: base(epubBookRef)
|
||||
{
|
||||
}
|
||||
|
||||
public byte[] ReadContent()
|
||||
{
|
||||
return ReadContentAsBytes();
|
||||
}
|
||||
|
||||
public Task<byte[]> ReadContentAsync()
|
||||
{
|
||||
return ReadContentAsBytesAsync();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VersOne.Epub
|
||||
{
|
||||
public class EpubChapterRef
|
||||
{
|
||||
private readonly EpubTextContentFileRef epubTextContentFileRef;
|
||||
|
||||
public EpubChapterRef(EpubTextContentFileRef epubTextContentFileRef)
|
||||
{
|
||||
this.epubTextContentFileRef = epubTextContentFileRef;
|
||||
}
|
||||
|
||||
public string Title { get; set; }
|
||||
public string ContentFileName { get; set; }
|
||||
public string Anchor { get; set; }
|
||||
public List<EpubChapterRef> SubChapters { get; set; }
|
||||
public EpubChapterRef Parent { get; set; }
|
||||
|
||||
public string ReadHtmlContent()
|
||||
{
|
||||
return ReadHtmlContentAsync().Result;
|
||||
}
|
||||
|
||||
public Task<string> ReadHtmlContentAsync()
|
||||
{
|
||||
return epubTextContentFileRef.ReadContentAsTextAsync();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return String.Format("Title: {0}, Subchapter count: {1}", Title, SubChapters.Count);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Threading.Tasks;
|
||||
using VersOne.Epub.Internal;
|
||||
|
||||
namespace VersOne.Epub
|
||||
{
|
||||
public abstract class EpubContentFileRef
|
||||
{
|
||||
private readonly EpubBookRef epubBookRef;
|
||||
|
||||
public EpubContentFileRef(EpubBookRef epubBookRef)
|
||||
{
|
||||
this.epubBookRef = epubBookRef;
|
||||
}
|
||||
|
||||
public string FileName { get; set; }
|
||||
public EpubContentType ContentType { get; set; }
|
||||
public string ContentMimeType { get; set; }
|
||||
|
||||
public byte[] ReadContentAsBytes()
|
||||
{
|
||||
return ReadContentAsBytesAsync().Result;
|
||||
}
|
||||
|
||||
public async Task<byte[]> ReadContentAsBytesAsync()
|
||||
{
|
||||
ZipArchiveEntry contentFileEntry = GetContentFileEntry();
|
||||
byte[] content = new byte[(int)contentFileEntry.Length];
|
||||
using (Stream contentStream = OpenContentStream(contentFileEntry))
|
||||
using (MemoryStream memoryStream = new MemoryStream(content))
|
||||
{
|
||||
await contentStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
public string ReadContentAsText()
|
||||
{
|
||||
return ReadContentAsTextAsync().Result;
|
||||
}
|
||||
|
||||
public async Task<string> ReadContentAsTextAsync()
|
||||
{
|
||||
using (Stream contentStream = GetContentStream())
|
||||
using (StreamReader streamReader = new StreamReader(contentStream))
|
||||
{
|
||||
return await streamReader.ReadToEndAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public Stream GetContentStream()
|
||||
{
|
||||
return OpenContentStream(GetContentFileEntry());
|
||||
}
|
||||
|
||||
private ZipArchiveEntry GetContentFileEntry()
|
||||
{
|
||||
string contentFilePath = ZipPathUtils.Combine(epubBookRef.Schema.ContentDirectoryPath, FileName);
|
||||
ZipArchiveEntry contentFileEntry = epubBookRef.EpubArchive.GetEntry(contentFilePath);
|
||||
if (contentFileEntry == null)
|
||||
{
|
||||
throw new Exception(String.Format("EPUB parsing error: file {0} not found in archive.", contentFilePath));
|
||||
}
|
||||
if (contentFileEntry.Length > Int32.MaxValue)
|
||||
{
|
||||
throw new Exception(String.Format("EPUB parsing error: file {0} is bigger than 2 Gb.", contentFilePath));
|
||||
}
|
||||
return contentFileEntry;
|
||||
}
|
||||
|
||||
private Stream OpenContentStream(ZipArchiveEntry contentFileEntry)
|
||||
{
|
||||
Stream contentStream = contentFileEntry.Open();
|
||||
if (contentStream == null)
|
||||
{
|
||||
throw new Exception(String.Format("Incorrect EPUB file: content file \"{0}\" specified in manifest is not found.", FileName));
|
||||
}
|
||||
return contentStream;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace VersOne.Epub
|
||||
{
|
||||
public class EpubContentRef
|
||||
{
|
||||
public Dictionary<string, EpubTextContentFileRef> Html { get; set; }
|
||||
public Dictionary<string, EpubTextContentFileRef> Css { get; set; }
|
||||
public Dictionary<string, EpubByteContentFileRef> Images { get; set; }
|
||||
public Dictionary<string, EpubByteContentFileRef> Fonts { get; set; }
|
||||
public Dictionary<string, EpubContentFileRef> AllFiles { get; set; }
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VersOne.Epub
|
||||
{
|
||||
public class EpubTextContentFileRef : EpubContentFileRef
|
||||
{
|
||||
public EpubTextContentFileRef(EpubBookRef epubBookRef)
|
||||
: base(epubBookRef)
|
||||
{
|
||||
}
|
||||
|
||||
public string ReadContent()
|
||||
{
|
||||
return ReadContentAsText();
|
||||
}
|
||||
|
||||
public Task<string> ReadContentAsync()
|
||||
{
|
||||
return ReadContentAsTextAsync();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user