Exemplo n.º 1
0
    public static async Task <IBook> GetBookZip(Stream stream)
    {
        var book = new CbzBook();

        try
        {
            await book.LoadAsync(stream);
        }
        catch
        {
            return(null);
        }
        if (book.PageCount <= 0)
        {
            return(null);
        }
        return(book);
    }
Exemplo n.º 2
0
        public static async Task <(IBook Book, bool IsEpub)> GetBookFromFile(Windows.Storage.IStorageFile file)
        {
            if (file == null)
            {
                return(null, false);
            }
            else if (Path.GetExtension(file.Path).ToLower() == ".pdf")
            {
                goto Pdf;
            }
            else if (new string[] { ".zip", ".cbz" }.Contains(Path.GetExtension(file.Path).ToLower()))
            {
                goto Zip;
            }
            else if (new string[] { ".rar", ".cbr", ".7z", ".cb7" }.Contains(Path.GetExtension(file.Path).ToLower()))
            {
                goto SharpCompress;
            }
            else if (Path.GetExtension(file.Path).ToLower() == ".epub")
            {
                goto Epub;
            }

            var stream = await file.OpenStreamForReadAsync();

            var buffer = new byte[64];

            stream.Read(buffer, 0, stream.Length < 64 ? (int)stream.Length : 64);
            stream.Close();

            if (buffer.Take(5).SequenceEqual(new byte[] { 0x25, 0x50, 0x44, 0x46, 0x2d }))
            {
                //pdf
                goto Pdf;
            }
            else if (buffer.Take(6).SequenceEqual(new byte[] { 0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C }))
            {
                //7zip
                goto SharpCompress;
            }
            else if (buffer.Take(4).SequenceEqual(new byte[] { 0x50, 0x4B, 0x03, 0x04 }) ||
                     buffer.Take(4).SequenceEqual(new byte[] { 0x50, 0x4B, 0x05, 0x06 }) ||
                     buffer.Take(4).SequenceEqual(new byte[] { 0x50, 0x4B, 0x07, 0x08 })
                     )
            {
                //zip
                if (buffer.Skip(0x1e).Take(28).SequenceEqual(Encoding.ASCII.GetBytes("mimetypeapplication/epub+zip")))
                {
                    //epub
                    goto Epub;
                }
                goto Zip;
            }
            else if (buffer.Take(7).SequenceEqual(new byte[] { 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00 }))
            {
                //rar
                goto SharpCompress;
            }
            else if (buffer.Take(8).SequenceEqual(new byte[] { 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x01, 0x00 }))
            {
                //rar5
                goto SharpCompress;
            }

            return(null, false);

            Pdf :;
            {
                var book = new PdfBook();
                try
                {
                    await book.Load(file, async (a) =>
                    {
                        var dialog = new Views.PasswordRequestContentDialog();
                        var result = await dialog.ShowAsync();
                        if (result == Windows.UI.Xaml.Controls.ContentDialogResult.Primary)
                        {
                            return(dialog.Password, dialog.Remember);
                        }
                        else
                        {
                            throw new Exception();
                        }
                    });
                }
                catch { return(null, false); }
                if (book.PageCount <= 0)
                {
                    return(null, false);
                }
                return(book, false);
            }
            Zip :;
            {
                var book = new CbzBook();
                try
                {
                    await book.LoadAsync((await file.OpenReadAsync()).AsStream());
                }
                catch
                {
                    return(null, false);
                }
                if (book.PageCount <= 0)
                {
                    return(null, false);
                }
                return(book, false);
            }
            SharpCompress :;
            {
                var book = new CompressedBook();
                try
                {
                    await book.LoadAsync((await file.OpenReadAsync()).AsStream());
                }
                catch
                {
                    return(null, false);
                }
                if (book.PageCount <= 0)
                {
                    return(null, false);
                }
                return(book, false);
            }
            Epub :;
            {
                return(null, true);
            }
        }