Bloom.Book.BookStorage.FindBookHtmlInFolder C# (CSharp) Method

FindBookHtmlInFolder() public static method

public static FindBookHtmlInFolder ( string folderPath ) : string
folderPath string
return string
        public static string FindBookHtmlInFolder(string folderPath)
        {
            string p = Path.Combine(folderPath, Path.GetFileName(folderPath) + ".htm");
            if (RobustFile.Exists(p))
                return p;
            p = Path.Combine(folderPath, Path.GetFileName(folderPath) + ".html");
            if (File.Exists(p))
                return p;

            if (!Directory.Exists(folderPath)) //bl-291 (user had 4 month-old version, so the bug may well be long gone)
            {
                //in version 1.012 I got this because I had tried to delete the folder on disk that had a book
                //open in Bloom.
                SIL.Reporting.ErrorReport.NotifyUserOfProblem("There's a problem; Bloom can't save this book. Did you perhaps delete or rename the folder that this book is (was) in?");
                throw new ApplicationException(string.Format("In FindBookHtmlInFolder('{0}'), the folder does not exist. (ref bl-291)", folderPath));
            }

            //ok, so maybe they changed the name of the folder and not the htm. Can we find a *single* html doc?
            // BL-3572 when the only file in the directory is "BigBook.html", it matches both filters in Windows (tho' not in Linux?)
            // so Union works better here. (And we'll change the name of the book too.)
            var candidates = new List<string>(Directory.GetFiles(folderPath, "*.htm").Union(Directory.GetFiles(folderPath, "*.html")));
            candidates.RemoveAll((name) => name.ToLowerInvariant().Contains("configuration"));
            if (candidates.Count == 1)
                return candidates[0];

            //template
            p = Path.Combine(folderPath, "templatePages.htm");
            if (RobustFile.Exists(p))
                return p;
            p = Path.Combine(folderPath, "templatePages.html");
            if (File.Exists(p))
                return p;

            return string.Empty;
        }

Usage Example

コード例 #1
0
ファイル: BookServer.cs プロジェクト: sujeffreyl/BloomDesktop
        public Book CreateFromSourceBook(string sourceBookFolder, string containingDestinationFolder)
        {
            string pathToFolderOfNewBook = null;

            Logger.WriteMinorEvent("Starting CreateFromSourceBook({0})", sourceBookFolder);
            try
            {
                var starter = _bookStarterFactory();
                pathToFolderOfNewBook = starter.CreateBookOnDiskFromTemplate(sourceBookFolder, containingDestinationFolder);
                if (Configurator.IsConfigurable(pathToFolderOfNewBook))
                {
                    var c = _configuratorFactory(containingDestinationFolder);
                    if (DialogResult.Cancel == c.ShowConfigurationDialog(pathToFolderOfNewBook))
                    {
                        SIL.IO.RobustIO.DeleteDirectory(pathToFolderOfNewBook, true);
                        return(null);                        // the template had a configuration page and they clicked "cancel"
                    }
                    c.ConfigureBook(BookStorage.FindBookHtmlInFolder(pathToFolderOfNewBook));
                }

                var newBookInfo = new BookInfo(pathToFolderOfNewBook, true);                // _bookInfos.Find(b => b.FolderPath == pathToFolderOfNewBook);
                if (newBookInfo is ErrorBookInfo)
                {
                    throw ((ErrorBookInfo)newBookInfo).Exception;
                }

                Book newBook = GetBookFromBookInfo(newBookInfo);

                //Hack: this is a bit of a hack, to handle problems where we make the book with the suggested initial name, but the title is still something else
                var name = Path.GetFileName(newBookInfo.FolderPath);                 // this way, we get "my book 1", "my book 2", etc.
                newBook.SetTitle(name);

                Logger.WriteMinorEvent("Finished CreateFromnewBook({0})", newBook.FolderPath);
                Logger.WriteEvent("CreateFromSourceBook({0})", newBook.FolderPath);
                return(newBook);
            }
            catch (Exception)
            {
                Logger.WriteEvent("Cleaning up after error CreateFromSourceBook({0})", pathToFolderOfNewBook);
                //clean up this ill-fated book folder up
                if (!string.IsNullOrEmpty(pathToFolderOfNewBook) && Directory.Exists(pathToFolderOfNewBook))
                {
                    SIL.IO.RobustIO.DeleteDirectory(pathToFolderOfNewBook, true);
                }
                throw;
            }
        }
All Usage Examples Of Bloom.Book.BookStorage::FindBookHtmlInFolder