Example #1
0
        /// <summary>
        /// If we are able to directly submit to YouTrack, we do that. But otherwise,
        /// this makes a zip file of everything we want to submit, in order to
        /// give the user a single thing they need to attach and send.
        /// </summary>
        private void MakeEmailableReportFile()
        {
            var filename = ("Report " + DateTime.UtcNow.ToString("u") + ".zip").Replace(':', '.');

            filename = filename.SanitizeFilename('#');
            var zipFile = TempFile.WithFilename(filename);

            _emailableReportFilePath = zipFile.Path;

            var zip = new BloomZipFile(_emailableReportFilePath);

            using (var file = TempFile.WithFilenameInTempFolder("report.txt"))
            {
                using (var stream = RobustFile.CreateText(file.Path))
                {
                    stream.WriteLine(GetFullDescriptionContents(false));

                    if (_includeBook.Visible && _includeBook.Checked)                     // only Visible if Book is not null
                    {
                        stream.WriteLine();
                        stream.WriteLine(
                            "REMEMBER: if the attached zip file appears empty, it may have non-ascii in the file names. Open with 7zip and you should see it.");
                    }
                }
                zip.AddTopLevelFile(file.Path);

                if (_includeBook.Visible && _includeBook.Checked)                 // only Visible if Book is not null
                {
                    zip.AddDirectory(Book.FolderPath);
                    if (WantReaderInfo())
                    {
                        AddReaderInfo(zip);
                    }
                    AddCollectionSettings(zip);
                }
            }
            if (_includeScreenshot.Checked)
            {
                using (var file = TempFile.WithFilenameInTempFolder("screenshot.png"))
                {
                    RobustImageIO.SaveImage(_screenshot, file.Path, ImageFormat.Png);
                    zip.AddTopLevelFile(file.Path);
                }
            }
            if (Logger.Singleton != null)
            {
                try
                {
                    using (var logFile = GetLogFile())
                    {
                        zip.AddTopLevelFile(logFile.Path);
                    }
                }
                catch (Exception)
                {
                    // just ignore
                }
            }
            zip.Save();
        }
Example #2
0
        /// <summary>
        /// When the Book is a Decodable or Leveled Reader, bundle up more information to recreate its state.
        /// </summary>
        /// <remarks>
        /// See http://issues.bloomlibrary.org/youtrack/issue/BL-4240.
        /// </remarks>
        private void AddReaderInfo(BloomZipFile zip)
        {
            // When Book being sent is a Decodable or Leveled Reader, include these as well:
            // ReaderToolsSettings-*.json
            // ReaderToolsWords-*.json
            // And the following Collection folders:
            // Allowed Words
            // Sample Texts
            var collectionFolder = System.IO.Path.GetDirectoryName(Book.FolderPath);

            foreach (var file in Directory.GetFiles(collectionFolder, "ReaderTools*-*.json"))
            {
                zip.AddTopLevelFile(file);
            }
            var allowedWords = Path.Combine(collectionFolder, "Allowed Words");

            if (Directory.Exists(allowedWords))
            {
                zip.AddDirectory(allowedWords);
            }
            var sampleTexts = Path.Combine(collectionFolder, "Sample Texts");

            if (Directory.Exists(sampleTexts))
            {
                zip.AddDirectory(sampleTexts);
            }
        }
 private string CreateBookZipFile(string basename, string userDesc)
 {
     try
     {
         if (_bookZipFileTemp != null)
         {
             _bookZipFileTemp.Dispose();                         // delete any previous report's temp file
         }
         _bookZipFileTemp = TempFile.WithFilenameInTempFolder(basename + ".zip");
         _bookZipFile     = new BloomZipFile(_bookZipFileTemp.Path);
         _bookZipFile.AddDirectory(_bookSelection.CurrentSelection.StoragePageFolder);
         if (WantReaderInfo(true))
         {
             AddReaderInfo();
         }
         AddCollectionSettings();
         _bookZipFile.Save();
         return(_bookZipFileTemp.Path);
     }
     catch (Exception error)
     {
         var msg = "***Error as ProblemReportApi attempted to zip up the book: " + error.Message;
         userDesc += Environment.NewLine + msg;
         Logger.WriteEvent(userDesc);
         DisposeOfZipRemnants(true);
         return(null);
     }
 }
Example #4
0
        /// <summary>
        /// Zip up the book folder, excluding .pdf, .bloombookorder, .map, .bloompack, .db files.
        /// The resulting file should have a .bloomSource extension, but this depends on properly
        /// setting the value of destFileName before calling this method.  TeamCollection still
        /// uses the .bloom extension until we can figure out how to safely migrate teams as a
        /// whole to using .bloomSource.
        /// </summary>
        /// <param name="exception">any exception which occurs when trying to save the file</param>
        /// <returns>true if file was saved successfully; false otherwise</returns>
        /// <remarks>if return value is false, exception is non-null and vice versa</remarks>
        public static bool SaveAsBloomSourceFile(string srcFolderName, string destFileName, out Exception exception)
        {
            exception = null;
            try
            {
                var excludedExtensions = new[] { ".pdf", ".bloombookorder", ".map", ".bloompack", ".db" };

                Logger.WriteEvent("Zipping up {0} ...", destFileName);
                var zipFile = new BloomZipFile(destFileName);
                zipFile.AddDirectoryContents(srcFolderName, excludedExtensions);

                Logger.WriteEvent("Saving {0} ...", destFileName);
                zipFile.Save();

                if (destFileName.EndsWith(".bloom"))
                {
                    Logger.WriteEvent("Finished writing .bloom file.");
                }
                else
                {
                    Logger.WriteEvent("Finished writing .bloomSource file.");
                }
            }
            catch (Exception e)
            {
                exception = e;
                return(false);
            }
            return(true);
        }
        private void DisposeOfZipRemnants(bool includeBook)
        {
            // if an error happens in the zipper, the zip file stays locked, so we just leak it
            if (includeBook)
            {
                _bookZipFileTemp.Detach();
            }

            _bookZipFile = null;
        }
Example #6
0
        /// <summary>
        /// Zip up the book folder, excluding .pdf, .bloombookorder, .map, .bloompack, .db files.
        /// The resulting file will have a .bloom extension.
        /// </summary>
        /// <param name="srcFolderName"></param>
        /// <param name="destFileName"></param>
        internal void SaveAsBloomFile(string srcFolderName, string destFileName)
        {
            var excludedExtensions = new[] { ".pdf", ".bloombookorder", ".map", ".bloompack", ".db" };

            Logger.WriteEvent("Zipping up {0} ...", destFileName);
            var zipFile = new BloomZipFile(destFileName);

            zipFile.AddDirectoryContents(srcFolderName, excludedExtensions);
            Logger.WriteEvent("Saving {0} ...", destFileName);
            zipFile.Save();
            Logger.WriteEvent("Finished writing .bloom file.");
        }
Example #7
0
        private void AddCollectionSettings(BloomZipFile zip)
        {
            // When sending Book, add the project settings files as well.
            var collectionFolder = System.IO.Path.GetDirectoryName(Book.FolderPath);

            foreach (var file in Directory.GetFiles(collectionFolder, "*CollectionStyles.css"))
            {
                zip.AddTopLevelFile(file);
            }
            foreach (var file in Directory.GetFiles(collectionFolder, "*.bloomCollection"))
            {
                zip.AddTopLevelFile(file);
            }
        }
Example #8
0
        public void UploadSmokeTest()
        {
            using (var folder = new TemporaryFolder("Upload Smoke Test"))
            {
                File.WriteAllText(folder.Combine("hello there.txt"), "hello there");
                using (var bookZip = TempFile.WithFilenameInTempFolder("Upload Smoketest.zip"))
                {
                    var zip = new BloomZipFile(bookZip.Path);
                    zip.AddDirectory(folder.FolderPath);
                    zip.Save();

                    var progress = new StringBuilderProgress();
                    ProblemBookUploader.UploadBook(BloomS3Client.UnitTestBucketName, bookZip.Path, progress);
                    Assert.IsTrue(progress.Text.Contains("Success"), progress.Text);
                }
            }
        }
 private string MakeEmailableReportFile(bool includeBook, bool includeScreenshot, string userDesc, string diagnosticInfo)
 {
     try
     {
         var filename = ("Report " + DateTime.UtcNow.ToString("u") + ".zip").Replace(':', '.');
         filename = filename.SanitizeFilename('#');
         var emailZipPath = Path.Combine(Path.GetTempPath(), filename);
         var emailZipper  = new BloomZipFile(emailZipPath);
         using (var file = TempFile.WithFilenameInTempFolder("report.txt"))
         {
             using (var stream = RobustFile.CreateText(file.Path))
             {
                 stream.WriteLine(diagnosticInfo);
                 if (includeBook)
                 {
                     stream.WriteLine();
                     stream.WriteLine(
                         "REMEMBER: if the attached zip file appears empty, it may have non-ascii in the file names. Open with 7zip and you should see it.");
                 }
             }
             emailZipper.AddTopLevelFile(file.Path);
         }
         if (includeBook)
         {
             var bookZipPath = CreateBookZipFile("ProblemBook", userDesc);
             if (bookZipPath != null)
             {
                 emailZipper.AddTopLevelFile(bookZipPath);
             }
         }
         if (includeScreenshot && _screenshotTempFile != null && RobustFile.Exists(_screenshotTempFile.Path))
         {
             emailZipper.AddTopLevelFile(_screenshotTempFile.Path);
         }
         emailZipper.Save();
         return(emailZipPath);
     }
     catch (Exception error)
     {
         var msg = "***Error as ProblemReportApi attempted to zip up error information to email: " + error.Message;
         userDesc += Environment.NewLine + msg;
         Logger.WriteEvent(userDesc);
         return(null);
     }
 }
Example #10
0
        public void CreateZipFile_NonAsciiEntryNames()
        {
            // this test is to make sure non-ascii file names are being stored and retrieved correctly

            const string fileName     = "मराठी मैथिली संस्कृत हिन्.htm";
            const string fileContents = @"File contents.";

            using (var tempFile = TempFile.WithFilenameInTempFolder(fileName))
            {
                File.WriteAllText(tempFile.Path, fileContents);

                using (var bookZip = TempFile.WithExtension(".zip"))
                {
                    var zip = new BloomZipFile(bookZip.Path);
                    zip.AddTopLevelFile(tempFile.Path);
                    zip.Save();

                    using (var zip2 = new ZipFile(bookZip.Path))
                    {
                        foreach (ZipEntry zipEntry in zip2)
                        {
                            Console.Out.WriteLine(zipEntry.Name);
                            Assert.That(zipEntry.Name, Is.EqualTo(fileName));

                            using (var inStream = zip2.GetInputStream(zipEntry))
                            {
                                byte[] buffer = new byte[zipEntry.Size];
                                ICSharpCode.SharpZipLib.Core.StreamUtils.ReadFully(inStream, buffer);

                                var testStr = Encoding.Default.GetString(buffer);
                                Assert.That(testStr, Is.EqualTo(fileContents));
                            }
                        }
                    }
                }
            }
        }
Example #11
0
        /// <summary>
        /// Using YouTrackSharp here. We can't submit
        /// the report as if it were from this person, even if they have an account (well, not without
        /// asking them for credentials, which is just not gonna happen). So we submit with an
        /// account we created just for this purpose, "auto_report_creator".
        /// </summary>
        private bool SubmitToYouTrack()
        {
            try
            {
                ChangeState(State.Submitting);

                _youTrackConnection.Authenticate("auto_report_creator", "thisIsInOpenSourceCode");
                _issueManagement = new IssueManagement(_youTrackConnection);
                _youTrackIssue   = new Issue();
                _youTrackIssue.ProjectShortName = _youTrackProjectKey;
                _youTrackIssue.Type             = "Awaiting Classification";
                _youTrackIssue.Summary          = string.Format(Summary, _name.Text);
                _youTrackIssue.Description      = GetFullDescriptionContents(false);
                _youTrackIssueId = _issueManagement.CreateIssue(_youTrackIssue);

                // this could all be done in one go, but I'm doing it in stages so as to increase the
                // chance of success in bad internet situations
                if (_includeScreenshot.Checked)
                {
                    using (var file = TempFile.WithFilenameInTempFolder("screenshot.png"))
                    {
                        RobustImageIO.SaveImage(_screenshot, file.Path, ImageFormat.Png);
                        AddAttachment(file.Path);
                    }
                }

                if (Logger.Singleton != null)
                {
                    try
                    {
                        using (var logFile = GetLogFile())
                        {
                            AddAttachment(logFile.Path);
                        }
                    }
                    catch (Exception e)
                    {
                        _youTrackIssue.Description += System.Environment.NewLine + "***Got exception trying to attach log file: " + e.Message;
                        _issueManagement.UpdateIssue(_youTrackIssueId, _youTrackIssue.Summary, _youTrackIssue.Description);
                    }
                }


                if (_includeBook.Visible && _includeBook.Checked)                 // only Visible if Book is not null
                {
                    ChangeState(State.UploadingBook);
                    using (var bookZip = TempFile.WithFilenameInTempFolder(_youTrackIssueId + ".zip"))
                    {
                        var progress = new StatusProgress();
                        try
                        {
                            var zip = new BloomZipFile(bookZip.Path);
                            zip.AddDirectory(Book.FolderPath);
                            if (WantReaderInfo())
                            {
                                AddReaderInfo(zip);
                            }
                            AddCollectionSettings(zip);
                            zip.Save();
                        }
                        catch (Exception error)
                        {
                            _youTrackIssue.Description += System.Environment.NewLine + "***Error as ProblemReporterDialog attempted to zip up the book: " + error.Message;
                            _issueManagement.UpdateIssue(_youTrackIssueId, _youTrackIssue.Summary, _youTrackIssue.Description);
                            Logger.WriteEvent("*** Error as ProblemReporterDialog attempted to zip up the book. " + error.Message);
                            // if an error happens in the zipper, the zip file stays locked, so we just leak it
                            bookZip.Detach();
                            _shortErrorHtml += " Error Zipping Book ";
                            throw;
                        }

                        try
                        {
                            string url = ProblemBookUploader.UploadBook(BloomS3Client.ProblemBookUploadsBucketName, bookZip.Path,
                                                                        progress);
                            _youTrackIssue.Description += System.Environment.NewLine + url;
                            _issueManagement.UpdateIssue(_youTrackIssueId, _youTrackIssue.Summary, _youTrackIssue.Description);
                        }
                        catch (Exception error)
                        {
                            Logger.WriteError(progress.LastError, error);
                            _youTrackIssue.Description += System.Environment.NewLine + "***Got exception trying upload book: " + error.Message;
                            _issueManagement.UpdateIssue(_youTrackIssueId, _youTrackIssue.Summary, _youTrackIssue.Description);
                            _shortErrorHtml += " Uploading Book Failed ";
                        }
                    }
                }

                ChangeState(State.Success);
                return(true);
            }
            catch (Exception error)
            {
                Debug.Fail(error.Message);
                return(false);
            }
        }
Example #12
0
 /// <summary>
 /// Finish publishing an ePUB that has been staged, by zipping it into the desired final file.
 /// </summary>
 /// <param name="destinationEpubPath"></param>
 public void FinishEpub(string destinationEpubPath)
 {
     var zip = new BloomZipFile(destinationEpubPath);
     foreach (var file in Directory.GetFiles(StagingDirectory))
         zip.AddTopLevelFile(file);
     foreach (var dir in Directory.GetDirectories(StagingDirectory))
         zip.AddDirectory(dir);
     zip.Save();
 }
        /// <summary>
        /// Using YouTrackSharp here. We can't submit
        /// the report as if it were from this person, even if they have an account (well, not without
        /// asking them for credentials, which is just not gonna happen). So we submit with an
        /// account we created just for this purpose, "auto_report_creator".
        /// </summary>
        private bool SubmitToYouTrack()
        {
            try
            {
                ChangeState(State.Submitting);

                _youTrackConnection.Authenticate("auto_report_creator", "thisIsInOpenSourceCode");
                _issueManagement = new IssueManagement(_youTrackConnection);
                _youTrackIssue = new Issue();
                _youTrackIssue.ProjectShortName = _youTrackProjectKey;
                _youTrackIssue.Type = "Awaiting Classification";
                _youTrackIssue.Summary = string.Format(Summary,_name.Text);
                _youTrackIssue.Description = GetFullDescriptionContents(false);
                _youTrackIssueId = _issueManagement.CreateIssue(_youTrackIssue);

                // this could all be done in one go, but I'm doing it in stages so as to increase the
                // chance of success in bad internet situations
                if (_includeScreenshot.Checked)
                {
                    using (var file = TempFile.WithFilenameInTempFolder("screenshot.png"))
                    {
                        SIL.IO.RobustIO.SaveImage(_screenshot, file.Path, ImageFormat.Png);
                        AddAttachment(file.Path);
                    }
                }

                if (Logger.Singleton != null)
                {
                    try
                    {
                        using (var logFile = GetLogFile())
                        {
                            AddAttachment(logFile.Path);
                        }
                    }
                    catch (Exception e)
                    {
                        _youTrackIssue.Description += System.Environment.NewLine + "***Got exception trying to attach log file: " + e.Message;
                        _issueManagement.UpdateIssue(_youTrackIssueId, _youTrackIssue.Summary, _youTrackIssue.Description);
                    }
                }

                if (_includeBook.Visible && _includeBook.Checked) // only Visible if Book is not null
                {
                    ChangeState(State.UploadingBook);
                    using (var bookZip = TempFile.WithFilenameInTempFolder(_youTrackIssueId + ".zip"))
                    {
                        var progress = new StatusProgress();
                        try
                        {
                            var zip = new BloomZipFile(bookZip.Path);
                            zip.AddDirectory(Book.FolderPath);
                            zip.Save();
                        }
                        catch (Exception error)
                        {
                            _youTrackIssue.Description += System.Environment.NewLine + "***Error as ProblemReporterDialog attempted to zip up the book: " + error.Message;
                            _issueManagement.UpdateIssue(_youTrackIssueId, _youTrackIssue.Summary, _youTrackIssue.Description);
                            Logger.WriteEvent("*** Error as ProblemReporterDialog attempted to zip up the book. " + error.Message);
                            // if an error happens in the zipper, the zip file stays locked, so we just leak it
                            bookZip.Detach();
                            _shortErrorHtml += " Error Zipping Book ";
                            throw;
                        }

                        try
                        {
                            string url = ProblemBookUploader.UploadBook(BloomS3Client.ProblemBookUploadsBucketName, bookZip.Path,
                                progress);
                            _youTrackIssue.Description += System.Environment.NewLine + url;
                            _issueManagement.UpdateIssue(_youTrackIssueId, _youTrackIssue.Summary, _youTrackIssue.Description);
                        }
                        catch (Exception error)
                        {
                            Logger.WriteError(progress.LastError, error);
                            _youTrackIssue.Description += System.Environment.NewLine + "***Got exception trying upload book: " + error.Message;
                            _issueManagement.UpdateIssue(_youTrackIssueId, _youTrackIssue.Summary, _youTrackIssue.Description);
                            _shortErrorHtml += " Uploading Book Failed ";
                        }
                    }
                }

                ChangeState(State.Success);
                return true;
            }
            catch (Exception error)
            {
                Debug.Fail(error.Message);
                return false;
            }
        }
        /// <summary>
        /// If we are able to directly submit to YouTrack, we do that. But otherwise,
        /// this makes a zip file of everything we want to submit, in order to
        /// give the user a single thing they need to attach and send.
        /// </summary>
        private void MakeEmailableReportFile()
        {
            var filename = ("Report " + DateTime.UtcNow.ToString("u") + ".zip").Replace(':', '.');
            filename = filename.SanitizeFilename('#');
            var zipFile = TempFile.WithFilename(filename);
            _emailableReportFilePath = zipFile.Path;

            var zip = new BloomZipFile(_emailableReportFilePath);

            using (var file = TempFile.WithFilenameInTempFolder("report.txt"))
            {
                using (var stream = RobustFile.CreateText(file.Path))
                {
                    stream.WriteLine(GetFullDescriptionContents(false));

                    if (_includeBook.Visible && _includeBook.Checked) // only Visible if Book is not null
                    {
                        stream.WriteLine();
                        stream.WriteLine(
                            "REMEMBER: if the attached zip file appears empty, it may have non-ascii in the file names. Open with 7zip and you should see it.");
                    }
                }
                zip.AddTopLevelFile(file.Path);

                if (_includeBook.Visible && _includeBook.Checked) // only Visible if Book is not null
                {
                    zip.AddDirectory(Book.FolderPath);
                }
            }
            if (_includeScreenshot.Checked)
            {
                using (var file = TempFile.WithFilenameInTempFolder("screenshot.png"))
                {
                    SIL.IO.RobustIO.SaveImage(_screenshot, file.Path, ImageFormat.Png);
                    zip.AddTopLevelFile(file.Path);
                }
            }
            if (Logger.Singleton != null)
            {
                try
                {
                    using (var logFile = GetLogFile())
                    {
                        zip.AddTopLevelFile(logFile.Path);
                    }
                }
                catch (Exception)
                {
                    // just ignore
                }
            }
            zip.Save();
        }
 public void Dispose()
 {
     _screenshotTempFile?.Dispose();
     _bookZipFile = null;
     _bookZipFileTemp?.Dispose();
 }
Example #16
0
 public void Dispose()
 {
     _reportInfo.ScreenshotTempFile?.Dispose();
     _reportZipFile = null;
     _reportZipFileTemp?.Dispose();
 }