Bloom.Book.BookInfo.Save C# (CSharp) Method

Save() public method

public Save ( ) : void
return void
        public void Save()
        {
            // https://jira.sil.org/browse/BL-354 "The requested operation cannot be performed on a file with a user-mapped section open"
            var count = 0;

            do
            {
                try
                {
                    RobustFile.WriteAllText(MetaDataPath, MetaData.Json);
                    return;
                }
                catch (IOException e)
                {
                    Thread.Sleep(500);
                    count++;

                    // stop trying after 5 attempts to save the file.
                    if (count > 4)
                    {
                        Debug.Fail("Reproduction of BL-354 that we have taken steps to avoid");

                        var msg = LocalizationManager.GetDynamicString("Bloom", "BookEditor.ErrorSavingPage", "Bloom wasn't able to save the changes to the page.");
                        ErrorReport.NotifyUserOfProblem(e, msg);
                    }
                }

            } while (count < 5);
        }

Usage Example

Example #1
0
        public void RoundTrips_AllowUploading()
        {
            var jsonPath = Path.Combine(_folder.Path, BookInfo.MetaDataFileName);
            File.WriteAllText(jsonPath, @"{'allowUploadingToBloomLibrary':'false'}");
            var bi = new BookInfo(_folder.Path, true);
            Assert.False(bi.AllowUploading, "CHECK YOUR FixBloomMetaInfo ENV variable! Initial Read Failed to get false. Contents: " + File.ReadAllText(jsonPath));
            bi.Save();
            var bi2 = new BookInfo(_folder.Path, true);
            Assert.False(bi2.AllowUploading, "Read after Save() Failed  to get false. Contents: " + File.ReadAllText(jsonPath));

            File.WriteAllText(jsonPath, @"{'allowUploadingToBloomLibrary':'true'}");
            var bi3 = new BookInfo(_folder.Path, true);
            Assert.That(bi3.AllowUploading,  "Initial Read Failed to get true. Contents: " + File.ReadAllText(jsonPath));
            bi3.Save();
            var bi4 = new BookInfo(_folder.Path, true);
            Assert.That(File.ReadAllText(jsonPath).Contains("allowUploadingToBloomLibrary"), "The file doesn't contain 'allowUploadingToBloomLibrary'");
            Assert.That(bi4.AllowUploading, "Read after Save() Failed  to get true. Contents: " + File.ReadAllText(jsonPath));
        }