Ejemplo n.º 1
0
        public async Task XRayXMLExpandRawMLNewVersionTest(Book book)
        {
            var xray = await _xrayService.CreateXRayAsync(book.Xml, book.Db, book.Guid, book.Asin, "com", true, _file, null, CancellationToken.None);

            xray.Unattended = true;
            var fakeMetadata = new Metadata();

            using var fs = new FileStream(book.Rawml, FileMode.Open);
            _xrayService.ExpandFromRawMl(xray, fakeMetadata, fs, false, true, true, 0, true, null, null, CancellationToken.None, false, false);
        }
Ejemplo n.º 2
0
        public async Task XRayXMLExpandRawMLNewVersionTest(Book book)
        {
            var xray = await _xrayService.CreateXRayAsync(book.Xml, book.Db, book.Guid, book.Asin, "com", true, _file, null, CancellationToken.None);

            xray.Unattended = true;
            // todo refactor this substitute
            using var fs = new FileStream(book.Rawml, FileMode.Open);
            var fakeMetadata = Substitute.For <IMetadata>();

            fakeMetadata.IsAzw3.Returns(false);
            fakeMetadata.GetRawMlStream().Returns(new MemoryStream(fs.ReadToEnd()));
            fs.Seek(0, SeekOrigin.Begin);
            _xrayService.ExpandFromRawMl(xray, fakeMetadata, fs, true, true, 0, true, null, null, CancellationToken.None, false, false);
        }
Ejemplo n.º 3
0
        public async Task XRayXMLSaveNewTest(Book book)
        {
            var xray = await _xrayService.CreateXRayAsync(book.Xml, book.Db, book.Guid, book.Asin, "com", true, _file, null, CancellationToken.None);

            xray.Unattended = true;
            _xrayService.ExportAndDisplayTerms(xray, xray.AliasPath, true, false);
            var fakeMetadata = new Metadata();

            _aliasesRepository.LoadAliasesForXRay(xray);
            using var fs = new FileStream(book.Rawml, FileMode.Open);
            _xrayService.ExpandFromRawMl(xray, fakeMetadata, fs, false, true, true, 0, true, null, null, CancellationToken.None, false, false);
            string filename = xray.XRayName();
            string outpath  = Path.Combine(Environment.CurrentDirectory, "out", filename);

            _xrayExporter.Export(xray, outpath, null, CancellationToken.None);
        }
Ejemplo n.º 4
0
        public async Task XRayXmlSaveOldTest(Book book)
        {
            var xray = await _xrayService.CreateXRayAsync(book.Xml, book.Db, book.Guid, book.Asin, "com", true, _file, null, CancellationToken.None);

            xray.Unattended = true;
            _xrayService.ExportAndDisplayTerms(xray, xray.AliasPath, true, false);
            using var fs = new FileStream(book.Bookpath, FileMode.Open, FileAccess.Read);
            var metadata = new Metadata(fs);

            _aliasesRepository.LoadAliasesForXRay(xray);
            using var bookFs = new FileStream(book.Rawml, FileMode.Open);
            _xrayService.ExpandFromRawMl(xray, metadata, bookFs, false, false, true, 0, true, null, null, CancellationToken.None, false, false);
            string filename = xray.XRayName();
            string outpath  = Path.Combine(Environment.CurrentDirectory, "out", filename);

            xray.CreatedAt = new DateTime(2019, 11, 2, 13, 19, 18, DateTimeKind.Utc);
            _xrayExporter.Export(xray, outpath, null, CancellationToken.None);
            FileAssert.AreEqual($"testfiles\\XRAY.entities.{book.Asin}_old.asc", outpath);
        }
Ejemplo n.º 5
0
        public async Task XRayXMLSaveNewTest(Book book)
        {
            var xray = await _xrayService.CreateXRayAsync(book.Xml, book.Db, book.Guid, book.Asin, "com", true, _file, null, CancellationToken.None);

            xray.Unattended = true;
            _xrayService.ExportAndDisplayTerms(xray, _file, true, false);
            using var fs = new FileStream(book.Rawml, FileMode.Open);
            var fakeMetadata = Substitute.For <IMetadata>();

            fakeMetadata.IsAzw3.Returns(false);
            fakeMetadata.GetRawMlStream().Returns(new MemoryStream(fs.ReadToEnd()));
            fs.Seek(0, SeekOrigin.Begin);
            _aliasesRepository.LoadAliasesForXRay(xray);
            _xrayService.ExpandFromRawMl(xray, fakeMetadata, fs, true, true, 0, true, null, null, CancellationToken.None, false, false);
            var filename = _directoryService.GetArtifactFilename(ArtifactType.XRay, book.Asin, book.Db, book.Guid);
            var outpath  = Path.Combine(Environment.CurrentDirectory, "out", filename);

            _xrayExporter.Export(xray, outpath, null, CancellationToken.None);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Builds an X-Ray file from the parameters given and returns the path at which the file has been saved (or null if something failed)
        /// </summary>
        public async Task <string> BuildAsync([NotNull] Request request, CancellationToken cancellationToken)
        {
            using var metadata = await GetAndValidateMetadataAsync(request.BookPath, cancellationToken);

            if (metadata == null)
            {
                return(null);
            }

            var dataSource = string.IsNullOrEmpty(request.DataUrl) || request.DataUrl == SecondarySourceRoentgen.FakeUrl
                ? _secondaryDataSourceFactory.Get(SecondaryDataSourceFactory.Enum.Roentgen)
                : _secondaryDataSourceFactory.GetInferredSource(request.DataUrl);

            if (dataSource == null)
            {
                _logger.Log("Data source could not be determined from the given path.");
                return(null);
            }

            Core.XRay.XRay xray;
            try
            {
                xray = await _xrayService.CreateXRayAsync(request.DataUrl, metadata.DbName, metadata.UniqueId, metadata.Asin, request.AmazonTld ?? "com", request.IncludeTopics, dataSource, _progress, cancellationToken);

                if (xray.Terms.Count == 0)
                {
                    _logger.Log($"No terms were available on {dataSource.Name}, cancelling the build...");
                    return(null);
                }

                var aliasPath = _directoryService.GetAliasPath(xray.Asin);
                _xrayService.ExportAndDisplayTerms(xray, dataSource, false, request.SplitAliases);

                if (xray.Terms.Any(term => term.Aliases?.Count > 0))
                {
                    _logger.Log("Character aliases read from the XML file.");
                }
                else if (!File.Exists(aliasPath))
                {
                    _logger.Log("Aliases file not found.");
                }
                else
                {
                    _aliasesRepository.LoadAliasesForXRay(xray);
                    _logger.Log($"Character aliases read from {aliasPath}.");
                }

                _logger.Log("Initial X-Ray built, adding locations and chapters...");
                //Expand the X-Ray file from the unpacked mobi
                Task buildTask = metadata switch
                {
                    // ReSharper disable AccessToDisposedClosure
                    MobiMetadata _ => Task.Run(() => _xrayService.ExpandFromRawMl(xray, metadata, metadata.GetRawMlStream(), true, true, 25, true, null, _progress, cancellationToken, true, false), cancellationToken),
                    KfxContainer kfx => Task.Run(() => _kfxXrayService.AddLocations(xray, kfx, true, 25, _progress, cancellationToken), cancellationToken),
                    _ => throw new NotSupportedException()
                };
                await buildTask.ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                _logger.Log("Build canceled.");
                return(null);
            }
            catch (Exception ex)
            {
                _logger.Log($"An error occurred while building the X-Ray:\r\n{ex.Message}\r\n{ex.StackTrace}");
                return(null);
            }

            _logger.Log("Saving X-Ray to file...");
            var xrayPath = _directoryService.GetArtifactPath(ArtifactType.XRay, metadata, Path.GetFileNameWithoutExtension(request.BookPath), true);

            try
            {
                var xrayExporter = _xrayExporterFactory.Get(XRayExporterFactory.Enum.Sqlite);
                xrayExporter.Export(xray, xrayPath, _progress, cancellationToken);
            }
            catch (OperationCanceledException)
            {
                _logger.Log("Building canceled.");
                return(null);
            }
            catch (Exception ex)
            {
                // TODO: Add option to retry maybe?
                _logger.Log($"An error occurred while creating the X-Ray file. Is it opened in another program?\r\n{ex.Message}");
                return(null);
            }

            _logger.Log($"X-Ray file created successfully!\r\nSaved to {xrayPath}");

            return(xrayPath);
        }