public static async Task <IBook> GetBookFromFile(Windows.Storage.IStorageFile file) { if (file == null) { return(null); } else if (Path.GetExtension(file.Path).ToLower() == ".pdf") { var book = new Books.Pdf.PdfBook(); try { await book.Load(file); } catch { return(null); } if (book.PageCount <= 0) { return(null); } return(book); } else if (new string[] { ".zip", ".cbz" }.Contains(Path.GetExtension(file.Path).ToLower())) { var book = new Books.Cbz.CbzBook(); try { await book.LoadAsync(WindowsRuntimeStreamExtensions.AsStream(await file.OpenReadAsync())); } catch { return(null); } if (book.PageCount <= 0) { return(null); } return(book); } else if (new string[] { ".rar", ".cbr", ".7z", ".cb7" }.Contains(Path.GetExtension(file.Path).ToLower())) { var book = new Books.Compressed.CompressedBook(); try { await book.LoadAsync(WindowsRuntimeStreamExtensions.AsStream(await file.OpenReadAsync())); } catch { return(null); } if (book.PageCount <= 0) { return(null); } return(book); } return(null); }
private async Task Run() { _cancellationToken = new CancellationTokenSource(); var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, _uploadUrl); request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _storageFolder.Account.Token); var httpClient = new System.Net.Http.HttpClient(); var response = await httpClient.SendAsync(request); if (response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.OK) { throw new InvalidOperationException("Session of upload did not create"); } DeserializedUploadSession deserializedUploadSession = new DeserializedUploadSession(); using (System.IO.Stream stream = await response.Content.ReadAsStreamAsync()) { DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUploadSession.GetType()); deserializedUploadSession = ser.ReadObject(stream) as DeserializedUploadSession; if (deserializedUploadSession?.NextExpectedRanges == null) { throw new NullReferenceException("Couldn't deserialized the data"); } } var fileStream = await _loсalFile.OpenReadAsync(); uint bytesCount = 13 * 320 * 1024; Byte[] bytes = new byte[bytesCount]; Regex regex = new Regex("[0-9]*"); var result = regex.Match(deserializedUploadSession.NextExpectedRanges[0]); _previewUploadBytes = UInt32.Parse(result.Value); fileStream.Seek(_previewUploadBytes); ulong offset = _previewUploadBytes; string token = _storageFolder.Account.Token; do { request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Put, _uploadUrl); request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); var reader = await fileStream.ReadAsync(bytes.AsBuffer(), bytesCount, InputStreamOptions.None); request.Content = new ByteArrayContent(reader.ToArray()); request.Content.Headers.ContentRange = new ContentRangeHeaderValue((long)offset, (long)offset + reader.Length - 1, (long)TotalBytesToTransfer); response = await httpClient.SendAsync(request); offset += reader.Length; } while (offset != TotalBytesToTransfer && !_cancellationToken.IsCancellationRequested); if (_cancellationToken.IsCancellationRequested) { StorageApplicationPermissions.FutureAccessList.Remove(_localFileToken); return; } DeserializedItem deserializedItem = new DeserializedItem(); using (System.IO.Stream stream = await response.Content.ReadAsStreamAsync()) { DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedItem.GetType()); deserializedItem = ser.ReadObject(stream) as DeserializedItem; if (deserializedItem?.File == null) { throw new NullReferenceException("Couldn't deserialized the data"); } } RemoteFile = new FileStorageFile(new FileBuilder(deserializedItem)) { Account = _storageFolder.Account }; StorageApplicationPermissions.FutureAccessList.Remove(_localFileToken); }
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); } }