private IEnumerator ShowLoadDialogCoroutine() { // Show a load file dialog and wait for a response from user // Load file/folder: file, Initial path: default (Documents), Title: "Load File", submit button text: "Load" yield return(FileBrowser.WaitForLoadDialog(FileBrowser.PickMode.Files, false, null, "Load Book", "Load")); if (FileBrowser.Success) { foreach (string path in FileBrowser.Result) { string fileName = FileBrowserHelpers.GetFilename(path); string targetPath = Application.persistentDataPath + _sourceFolder + fileName; FileBrowserHelpers.MoveFile(path, targetPath); ImportFiles(); } } onFB2Loaded?.Invoke(); }
private async void ImportFiles() { string path = Application.persistentDataPath + _sourceFolder; try { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } catch (IOException ex) { Debug.Log(ex.Message); return; } var files = FileBrowserHelpers.GetEntriesInDirectory(path); foreach (var file in files) { string fileName = string.Empty; string filePath = string.Empty; string imagePath = string.Empty; string text = string.Empty; string finalText = string.Empty; byte[] imageData = null; FileData newBook = null; if (!file.IsDirectory) { switch (file.Extension) { case ".fb2": fileName = FileBrowserHelpers.GetFilename(file.Path); text = FileBrowserHelpers.ReadTextFromFile(file.Path); FB2File fb2File = await new FB2Reader().ReadAsync(text); var lines = await _fB2SampleConverter.ConvertAsync(fb2File); finalText = _fB2SampleConverter.GetLinesAsText(); imageData = _fB2SampleConverter.GetCoverImageData(); if (imageData != null) { imagePath = Application.persistentDataPath + _targetFolder + fileName + ".jpg";; try { File.WriteAllBytes(imagePath, imageData); Debug.Log("Image is saved. Path: " + imagePath); } catch { Debug.LogError("Loading image is error!"); } imagePath = imagePath.Replace("/", "\\"); } filePath = (Application.persistentDataPath + _targetFolder + fileName).Replace("/", "\\"); newBook = new FileData(fileName, filePath, imagePath, FileData.FileType.Book); SaveFile(newBook, finalText); FileBrowserHelpers.DeleteFile(file.Path); break; case ".epub": EpubBook epubFile = EpubReader.ReadBook(file.Path); fileName = epubFile.Title + " (" + epubFile.Author + ")"; foreach (EpubTextContentFile textContentFile in epubFile.ReadingOrder) { HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(textContentFile.Content); StringBuilder sb = new StringBuilder(); foreach (HtmlNode node in htmlDocument.DocumentNode.SelectNodes("//text()")) { sb.AppendLine(node.InnerText.Replace("\n", "").Replace("\r", "")); } finalText += sb.ToString(); } imageData = epubFile.CoverImage; var imageName = string.Empty; if (imageData == null) { imageData = epubFile.Content.Images.FirstOrDefault().Value.Content; imageName = epubFile.Content.Images.FirstOrDefault().Key; } else { imageName = epubFile.Content.Cover.FileName; } if (imageData != null) { imagePath = Application.persistentDataPath + _targetFolder + imageName; try { File.WriteAllBytes(imagePath, imageData); Debug.Log("Image is saved. Path: " + imagePath); } catch { Debug.LogError("Loading image is error!"); } imagePath = imagePath.Replace("/", "\\"); } filePath = (Application.persistentDataPath + _targetFolder + fileName).Replace("/", "\\"); newBook = new FileData(fileName, filePath, imagePath, FileData.FileType.Book); SaveFile(newBook, finalText); FileBrowserHelpers.DeleteFile(file.Path); break; case ".pdf": var document = PdfiumViewer.PdfDocument.Load(file.Path); var title = document.GetInformation().Title; if (FilesData.Files.Any(x => x.Name == title)) { return; } fileName = FileBrowserHelpers.GetFilename(file.Path); filePath = Application.persistentDataPath + _targetFolder + fileName; imagePath = Application.persistentDataPath + _targetFolder + fileName.Remove(fileName.Length - 4) + ".png"; var image = document.Render(0, 72, 72, false); image.Save(imagePath, System.Drawing.Imaging.ImageFormat.Png); imagePath = imagePath.Replace("/", "\\"); filePath = filePath.Replace("/", "\\"); document.Dispose(); FileBrowserHelpers.MoveFile(file.Path, filePath); newBook = new FileData(title, filePath, imagePath, FileData.FileType.PdfFile); SaveFile(newBook); break; } } else { FileBrowserHelpers.DeleteDirectory(file.Path); } } }