Exemplo n.º 1
0
        /// <summary>
        /// Tests create view against two options with compare results
        /// </summary>
        private void TestCompareOptions(TestFile testFile, RenderOptions options1, RenderOptions options2)
        {
            var viewOptions1 = new ViewOptions
            {
                FileInfo      = testFile.ToFileInfo(),
                RenderOptions = options1
            };
            var viewResult1 = ViewApi.CreateView(new CreateViewRequest(viewOptions1));
            var response1   = FileApi.DownloadFile(new DownloadFileRequest {
                path = viewResult1.Pages[0].Path
            });

            Assert.Greater(response1.Length, 0);

            Cleanup();

            var viewOptions2 = new ViewOptions
            {
                FileInfo      = testFile.ToFileInfo(),
                RenderOptions = options2
            };
            var viewResult2 = ViewApi.CreateView(new CreateViewRequest(viewOptions2));
            var response2   = FileApi.DownloadFile(new DownloadFileRequest {
                path = viewResult2.Pages[0].Path
            });

            Assert.Greater(response2.Length, 0);

            Assert.AreNotEqual(response1.Length, response2.Length);
        }
        public void TestDownloadFile()
        {
            // Arrange
            var testFile = TestFiles.Docx;
            var request = new DownloadFileRequest { path = testFile.FullName };

            // Act & Assert
            var response = FileApi.DownloadFile(request);
            Assert.Greater(response.Length, 0);
        }
        static void TranslateDocument(Configuration conf)
        {
            // request parameters for translation
            string     name      = "test.docx";
            string     folder    = "";
            string     pair      = "en-fr";
            string     format    = "docx";
            string     outformat = "";
            string     storage   = "First Storage";
            string     saveFile  = "translated_d.docx";
            string     savePath  = "";
            bool       masters   = false;
            List <int> elements  = new List <int>();

            // local paths to upload and download files
            string uploadPath   = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName + "/" + name;
            string downloadPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName + "/" + saveFile;

            TranslationApi api     = new TranslationApi(conf);
            FileApi        fileApi = new FileApi(conf);


            Stream stream = File.Open(uploadPath, FileMode.Open);

            UploadFileRequest uploadRequest = new UploadFileRequest {
                File = stream, path = name, storageName = storage
            };
            FilesUploadResult uploadResult = fileApi.UploadFile(uploadRequest);

            Console.WriteLine("Files uploaded: " + uploadResult.Uploaded.Count);

            TranslateDocumentRequest request  = api.CreateDocumentRequest(name, folder, pair, format, outformat, storage, saveFile, savePath, masters, elements);
            TranslationResponse      response = api.RunTranslationTask(request);

            Console.WriteLine(response.Message);
            foreach (var key in response.Details.Keys)
            {
                Console.WriteLine(key + ": " + response.Details[key]);
            }

            DownloadFileRequest downloadRequest = new DownloadFileRequest {
                storageName = storage, path = saveFile
            };
            Stream result = fileApi.DownloadFile(downloadRequest);

            Console.WriteLine("Translated file downloaded");

            using (FileStream file = new FileStream(downloadPath, FileMode.Create, FileAccess.Write))
            {
                result.CopyTo(file);
            }
            Console.WriteLine("Translated file saved");
        }
Exemplo n.º 4
0
        public static void Run()
        {
            try
            {
                // Create necessary API instances
                var editApi = new EditApi(Common.GetConfig());
                var fileApi = new FileApi(Common.GetConfig());

                // The document already uploaded into the storage.
                // Load it into editable state
                var loadOptions = new SpreadsheetLoadOptions
                {
                    FileInfo = new FileInfo
                    {
                        FilePath = "Spreadsheet/four-sheets.xlsx"
                    },
                    OutputPath     = "output",
                    WorksheetIndex = 0
                };

                var loadResult = editApi.Load(new LoadRequest(loadOptions));

                // Download html document
                var stream     = fileApi.DownloadFile(new DownloadFileRequest(loadResult.HtmlPath));
                var htmlString = new StreamReader(stream, Encoding.UTF8).ReadToEnd();

                // Edit something...
                htmlString = htmlString.Replace("This is sample sheet", "This is sample sheep");

                // Upload html back to storage
                fileApi.UploadFile(new UploadFileRequest(loadResult.HtmlPath,
                                                         new MemoryStream(Encoding.UTF8.GetBytes(htmlString))));

                // Save html back to xlsx
                var saveOptions = new SpreadsheetSaveOptions
                {
                    FileInfo      = loadOptions.FileInfo,
                    OutputPath    = "output/edited.xlsx",
                    HtmlPath      = loadResult.HtmlPath,
                    ResourcesPath = loadResult.ResourcesPath
                };

                var saveResult = editApi.Save(new SaveRequest(saveOptions));

                // Done.
                Console.WriteLine("Document edited: " + saveResult.Path);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
        }
        public void StorageDownloadFile()
        {
            // Arrange
            var testFile = TestFiles.PdfStorage.FirstOrDefault(x => x.Name.Equals("01_pages.pdf"));
            var request  = new DownloadFileRequest {
                path = testFile.Path
            };

            // Act & Assert
            var response = FileApi.DownloadFile(request);

            Assert.Greater(response.Length, 0);
        }
        internal void DownloadFile(string path)
        {
            var requestDownload = new DownloadFileRequest {
                path = path
            };
            var    responseDownload = FileApi.DownloadFile(requestDownload);
            string fileName         = Path.GetFileName(path);
            string newFilePath      = Path.Combine(DownloadFolder, fileName);

            using (FileStream file = new FileStream(newFilePath, FileMode.Create))
            {
                responseDownload.CopyTo(file);
            }
        }
Exemplo n.º 7
0
        public void TestCreateViewWithMinimalViewOptions()
        {
            // Arrange
            var viewOptions = new ViewOptions
            {
                FileInfo = TestFiles.PasswordProtectedDocx.ToFileInfo()
            };
            var request = new CreateViewRequest(viewOptions);

            // Act & Assert
            var viewResult = ViewApi.CreateView(request);

            Assert.AreEqual(4, viewResult.Pages.Count);
            Assert.AreEqual(0, viewResult.Attachments.Count);

            // Download result
            var dlRequest = new DownloadFileRequest {
                path = viewResult.Pages[0].Path
            };
            var response = FileApi.DownloadFile(dlRequest);

            Assert.Greater(response.Length, 0);
        }
Exemplo n.º 8
0
        private Response Translate(Workbook sourceWorkbook, string sessionId, string filePath, string outputType, string translateFrom, string translateTo)
        {
            var conf = new Configuration
            {
                ClientId     = "ClientId",
                ClientSecret = "ClientSecret"
            };

            var name = sessionId + "_" + Path.GetFileNameWithoutExtension(filePath) + ".xlsx";

            const string folder = "";

            var pair = $"{translateFrom}-{translateTo}";

            const string format = "xlsx";

            const string outFormat = "xlsx";

            const string storage = "";

            var saveFile = "translated_" + name;

            const string savePath = "";

            const bool masters = false;

            var elements = new List <int>();

            var workingDirectoryPath = AppSettings.OutputDirectory + sessionId + "/" + Path.GetFileName(filePath);

            var fileApi = new FileApi(conf);

            using (var stream = File.Open(workingDirectoryPath, FileMode.Open))
            {
                var uploadRequest = new UploadFileRequest {
                    File = stream, Path = name, StorageName = storage
                };
                fileApi.UploadFile(uploadRequest);
            }

            var translationApi = new TranslationApi(conf);
            var request        = translationApi.CreateDocumentRequest(name, folder, pair, format, outFormat, storage, saveFile, savePath, masters, elements);

            translationApi.RunTranslationTask(request);

            var downloadRequest = new DownloadFileRequest {
                StorageName = storage, Path = saveFile
            };
            var result = fileApi.DownloadFile(downloadRequest);

            Directory.CreateDirectory(AppSettings.OutputDirectory + sessionId);
            var outFileName         = "translated_" + Path.GetFileName(filePath);
            var outputDirectoryPath = AppSettings.OutputDirectory + sessionId + "/" + outFileName;

            using (var file = new FileStream(outputDirectoryPath, FileMode.Create, FileAccess.Write))
            {
                result.CopyTo(file);
            }

            var convertWorkbook   = new Workbook(outputDirectoryPath);
            var convertWorksheets = convertWorkbook.Worksheets;
            var sourceWorksheets  = sourceWorkbook.Worksheets;

            foreach (var convertWorksheet in convertWorksheets)
            {
                var sourceWorksheet = sourceWorksheets[convertWorksheet.Index];
                var convertCells    = convertWorksheet.Cells;
                var sourceCells     = sourceWorksheet.Cells;

                foreach (Cell convertCell in convertCells)
                {
                    if (!convertCell.IsFormula && convertCell.Type == CellValueType.IsString)
                    {
                        sourceCells[convertCell.Row, convertCell.Column].PutValue(convertCell.Value);
                    }
                }
            }

            var filename = Path.GetFileNameWithoutExtension(outFileName) + "." + outputType.ToLower();
            var doc      = new DocumentInfo
            {
                FileName   = filename,
                FolderName = sessionId,
                Workbook   = sourceWorkbook
            };

            SaveDocument(doc, outputDirectoryPath, sessionId, GetSaveFormatType(filename));

            return(new Response
            {
                StatusCode = 200,
                Status = "OK",
                FileName = filename,
                FolderName = sessionId
            });
        }