/// <summary> /// Returns the paths of all the files present in the folder /// Note - upload size must be less than MaxUploadSize /// </summary> /// <param name="folderPath">Folder Path</param> /// <param name="uploadSize">Passint the size of all file in Folder as out param</param> /// <returns>List of all the file paths present in given folder</returns> List <string> GetUploadFileList(string folderPath, out long uploadSize) { List <string> filePaths = new List <string>(); Stack <string> dirs = new Stack <string>(); long uploadLength = 0; if (FileBrowserHelpers.DirectoryExists(folderPath)) { dirs.Push(folderPath); } while (dirs.Count > 0) { string currentDir = dirs.Pop(); FileSystemEntry[] allEntries = FileBrowserHelpers.GetEntriesInDirectory(currentDir); foreach (FileSystemEntry entry in allEntries) { if (entry.IsDirectory) { dirs.Push(entry.Path); } else { filePaths.Add(entry.Path); uploadLength += FileBrowserHelpers.GetFilesize(entry.Path); Debug.Log("File : " + entry.Path); } } } uploadSize = uploadLength; return(filePaths); }
/// <summary> /// Tells whether the user selected folder/file is valid i.e contains supported file format and checks file size. /// </summary> /// <param name="selectedPath">Path of file/folder user wants to upload</param> /// <param name="errorMessage">If any error pass error message</param> /// <returns>True if file/folder is valid else False</returns> public bool IsValidUpload(string selectedPath, out string errorMessage) { if (FileBrowserHelpers.DirectoryExists(selectedPath)) { FileSystemEntry[] allFiles = FileBrowserHelpers.GetEntriesInDirectory(selectedPath); foreach (FileSystemEntry entry in allFiles) { if (!entry.IsDirectory) { string uploadFileExtension = entry.Extension.ToLower(); if (uploadFileExtension == TypeGLTF || uploadFileExtension == TypeGLB) { errorMessage = null; return(true); } } } errorMessage = "glTF/glb type file not found in selected folder."; return(false); } else { string uploadFileName = FileBrowserHelpers.GetFilename(selectedPath).ToLower(); if (uploadFileName.EndsWith(TypeGLTF) || uploadFileName.EndsWith(TypeGLB)) { if (FileBrowserHelpers.GetFilesize(selectedPath) < MaxUploadFileSize) { errorMessage = null; return(true); } else { errorMessage = "File size is too large. It must be less than 50MB."; return(false); } } errorMessage = "File selected is not glTF or glb."; return(false); } }
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); } } }