Ejemplo n.º 1
0
        public void AddDirectoryContents_GivenSubfolder_WritesContentAtExpectedPath()
        {
            ///////////
            // Setup //
            ///////////
            string fileContents = "Hello.";

            // When disposing, deletes the temporary folder including its contents.
            using (var testFolder = new TemporaryFolder(kTestFolderName))
            {
                var subFolder    = new TemporaryFolder(testFolder, "subFolder");
                var testFilePath = Path.Combine(subFolder.Path, Path.ChangeExtension(Path.GetRandomFileName(), ".txt"));
                RobustFile.WriteAllText(testFilePath, fileContents);

                // This is not under the test folder. It makes creating the archive less thorny that way. make sure it gets cleaned up.
                using (var archiveFile = TempFile.WithExtension(".tar"))
                {
                    ///////////////////////
                    // System under test //
                    ///////////////////////
                    var archive = new BloomTarArchive(archiveFile.Path);
                    archive.AddDirectoryContents(testFolder.Path);
                    archive.Save();

                    //////////////////
                    // Verification //
                    //////////////////
                    var        inStream          = RobustFile.OpenRead(archiveFile.Path);
                    TarArchive archiveForReading = TarArchive.CreateInputTarArchive(inStream, Encoding.UTF8);
                    using (var extractedFolder = new TemporaryFolder(testFolder, "extractedTarContents"))
                    {
                        archiveForReading.ExtractContents(extractedFolder.Path, false);

                        string expectedFilePath = Path.Combine(extractedFolder.Path, "subFolder", Path.GetFileName(testFilePath));
                        FileAssert.Exists(expectedFilePath);
                        FileAssert.AreEqual(expectedFilePath, testFilePath, "Incorrect file contents.");

                        archiveForReading.Close();
                    }
                }

                /////////////
                // Cleanup //
                /////////////

                // Nothing else necessary, disposing the TemporaryFolder cleans up all its subcontents.
            }
        }
Ejemplo n.º 2
0
        // Precondition: bulkSaveSettings must be non-null
        public void PublishAllBooks(BulkBloomPubPublishSettings bulkSaveSettings)
        {
            BrowserProgressDialog.DoWorkWithProgressDialog(_webSocketServer, "Bulk Save BloomPubs",
                                                           (progress, worker) =>
            {
                var dest = new TemporaryFolder("BloomPubs");
                progress.MessageWithoutLocalizing($"Creating files in {dest.FolderPath}...");

                var filenameWithoutExtension = _collectionModel.CollectionSettings.DefaultBookshelf.SanitizeFilename(' ', true);
                ;
                if (bulkSaveSettings.makeBookshelfFile)
                {
                    // see https://docs.google.com/document/d/1UUvwxJ32W2X5CRgq-TS-1HmPj7gCKH9Y9bxZKbmpdAI

                    progress.MessageWithoutLocalizing($"Creating bloomshelf file...");
                    System.Diagnostics.Debug.Assert(!bulkSaveSettings.bookshelfColor.Contains("\n") && !bulkSaveSettings.bookshelfColor.Contains("\r"), "(BL-10190 Repro) Invalid bookshelfColor setting (contains newline). Please investigate!");
                    var colorString = getBloomReaderColorString(bulkSaveSettings.bookshelfColor);
                    System.Diagnostics.Debug.Assert(!colorString.Contains("\n") && !colorString.Contains("\r"), "(BL-10190 Repro) Invalid computed colorString value (contains newline). Please investigate!");

                    // OK I know this looks lame but trust me, using jsconvert to make that trivial label array is way too verbose.
                    var template =
                        "{ 'label': [{ 'en': 'bookshelf-name'}], 'id': 'id-of-the-bookshelf', 'color': 'hex-color-value'}";
                    var json = template.Replace('\'', '"')
                               .Replace("bookshelf-name", bulkSaveSettings.bookshelfLabel.Replace('"', '\''))
                               .Replace("id-of-the-bookshelf", _collectionModel.CollectionSettings.DefaultBookshelf)
                               .Replace("hex-color-value", colorString);
                    var filename       = $"{filenameWithoutExtension}.bloomshelf";
                    var bloomShelfPath = Path.Combine(dest.FolderPath, filename);
                    RobustFile.WriteAllText(bloomShelfPath, json, Encoding.UTF8);
                }

                foreach (var bookInfo in _collectionModel.TheOneEditableCollection.GetBookInfos())
                {
                    if (worker.CancellationPending)
                    {
                        progress.MessageWithoutLocalizing("Cancelled.");
                        return(true);
                    }
                    progress.MessageWithoutLocalizing($"Making BloomPUB for {bookInfo.QuickTitleUserDisplay}...",
                                                      ProgressKind.Heading);

                    var settings             = AndroidPublishSettings.GetPublishSettingsForBook(_bookServer, bookInfo);
                    settings.DistributionTag = bulkSaveSettings.distributionTag;
                    if (bulkSaveSettings.makeBookshelfFile)
                    {
                        settings.BookshelfTag = _collectionModel.CollectionSettings.DefaultBookshelf;
                    }
                    BloomPubMaker.CreateBloomPub(bookInfo, settings, dest.FolderPath, _bookServer, progress);
                }

                if (bulkSaveSettings.makeBloomBundle)
                {
                    var bloomBundlePath = Path.Combine(dest.FolderPath, $"{filenameWithoutExtension}.bloombundle");

                    var bloomBundleFile = new BloomTarArchive(bloomBundlePath);
                    bloomBundleFile.AddDirectoryContents(dest.FolderPath, new string[] { ".bloombundle" });
                    bloomBundleFile.Save();
                }

                progress.MessageWithoutLocalizing("Done.", ProgressKind.Heading);
                Process.SafeStart(dest.FolderPath);
                // true means wait for the user, don't close automatically
                return(true);
            });
        }