Ejemplo n.º 1
0
    /// <summary>
    /// Uploads a single file to the server. Not used in case of upload to AmazonS3
    /// Note - As the backend server supports only upload to AmazonS3 this is never called
    /// </summary>
    /// <param name="uploadFilePath">Path of file to be uploaded</param>
    /// <param name="deleteTempFile">Path of temporary Zip file</param>
    /// <returns></returns>
    private IEnumerator UploadFileToServer(string uploadFilePath, bool deleteTempFile)
    {
        List <IMultipartFormSection> formData = new List <IMultipartFormSection>();
        string fileName = FileBrowserHelpers.GetFilename(uploadFilePath);

        byte[] fileData = FileBrowserHelpers.ReadBytesFromFile(uploadFilePath);
        formData.Add(new MultipartFormFileSection("Object", fileData, fileName, "application/octet-stream"));

        using (UnityWebRequest uwr = UnityWebRequest.Post(amazonS3ServerBackend + "/fileupload", formData))
        {
            uwr.SendWebRequest();

            MessageFields uploadMsgFields = DisplayUploadProgressMessage();
            StartCoroutine(CheckUploadConnection(uwr));
            while (!uwr.isDone)
            {
                string progress = "Progress : " + Mathf.Round(uwr.uploadProgress * 100).ToString() + "%";
                uploadMsgFields.MessageDetails("Upload Progress ...", progress);
                if (uwr.isNetworkError || uwr.isHttpError)
                {
                    yield break;
                }
                yield return(null);
            }

            if (uwr.result != UnityWebRequest.Result.Success)
            {
                DestroyUploadProgressMessage(uploadMsgFields);
                if (deleteTempFile && FileBrowserHelpers.FileExists(uploadFilePath))
                {
                    FileBrowserHelpers.DeleteFile(uploadFilePath);
                    Debug.Log("Temp File deleted after Error");
                }
                DisplayUploadErrorMessage(uwr.error);
            }
            else
            {
                DestroyUploadProgressMessage(uploadMsgFields);
                if (deleteTempFile && FileBrowserHelpers.FileExists(uploadFilePath))
                {
                    FileBrowserHelpers.DeleteFile(uploadFilePath);
                    Debug.Log("Temp File deleted");
                }
            }
        }
    }
Ejemplo n.º 2
0
        public void RemoveFile(FileData file)
        {
            FileBrowserHelpers.DeleteFile(file.Path);

            if (file.CoverImagePath != string.Empty)
            {
                try
                {
                    FileBrowserHelpers.DeleteFile(file.CoverImagePath);
                }
                catch
                {
                    Debug.Log("Image not destroyed.");
                }
            }

            FilesData.Files.Remove(file);

            SaveFilesData();

            filesListChanged?.Invoke();
        }
Ejemplo n.º 3
0
        public Sprite GetPage(PdfiumViewer.PdfDocument doc, int pageNum)
        {
            if (pageNum >= doc.PageCount)
            {
                return(null);
            }

            var tempPath = Application.persistentDataPath + _targetFolder + "_tempImage_.png";

            if (FileBrowserHelpers.FileExists(tempPath))
            {
                FileBrowserHelpers.DeleteFile(tempPath);
            }

            var image = doc.Render(pageNum, 72, 72, false);

            image.Save(tempPath, System.Drawing.Imaging.ImageFormat.Png);

            var sprite = LoadSprite(tempPath);

            return(sprite);
        }
Ejemplo n.º 4
0
    IEnumerator ShowLoadDialogCoroutine()
    {
        // Show a load file dialog and wait for a response from user
        // Load file/folder: both, Allow multiple selection: true
        // Initial path: default (Documents), Initial filename: empty
        // Title: "Load File", Submit button text: "Load"
        yield return(FileBrowser.WaitForLoadDialog(FileBrowser.PickMode.FilesAndFolders, false, null, null, "Load Files and Folders", "Load"));

        // Dialog is closed
        // Print whether the user has selected some files/folders or cancelled the operation (FileBrowser.Success)
        Debug.Log(FileBrowser.Success);

        if (FileBrowser.Success)
        {
            // Print paths of the selected files (FileBrowser.Result) (null, if FileBrowser.Success is false)
            // for( int i = 0; i < FileBrowser.Result.Length; i++ )
            //  Debug.Log( FileBrowser.Result[i] );

            // Read the bytes of the first file via FileBrowserHelpers
            // Contrary to File.ReadAllBytes, this function works on Android 10+, as well
            byte[] bytes = FileBrowserHelpers.ReadBytesFromFile(FileBrowser.Result[0]);
            Debug.Log(bytes);

            // Or, copy the first file to persistentDataPath
            // string destinationPath = Path.Combine( Application.persistentDataPath, FileBrowserHelpers.GetFilename( FileBrowser.Result[0] ) );

            string destinationPath = Path.Combine(Application.persistentDataPath, "Data.csv");
            Debug.Log(destinationPath);

            if (FileBrowserHelpers.FileExists(destinationPath))
            {
                FileBrowserHelpers.DeleteFile(destinationPath);
            }
            FileBrowserHelpers.CopyFile(FileBrowser.Result[0], destinationPath);
            name = FileBrowserHelpers.GetFilename(FileBrowser.Result[0]);
            Debug.Log(name);
            FileSelectText(name);
        }
    }
Ejemplo n.º 5
0
        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);
                }
            }
        }