Пример #1
0
        public async Task WhenBrochureHasNoPageCount_SetsFromPdf()
        {
            // given
            var brochure = new Brochure
            {
                Id = new Uri(SourceId),
            };

            this.sourceRepository.GetBrochure(Arg.Any <Uri>())
            .Returns(Task.FromResult(brochure));
            this.httpMessageHandler.Expect(BlobUri)
            .Respond(KnownMimeTypes.Pdf, Resource.AsStream("Pdfs/mb1.pdf"));
            var pdfUploaded = new PdfUploaded
            {
                BlobUri  = BlobUri,
                Name     = Name,
                SourceId = SourceId,
            };

            // when
            await this.functions.Run(pdfUploaded);

            // then
            await this.persistence
            .Received(1)
            .SaveBrochure(Arg.Is <Brochure>(b => b.Pages == 9));
        }
Пример #2
0
        public async Task WhenBrochureHasNonLegacyImages_DoesNothing()
        {
            // given
            var brochure = new Brochure
            {
                Id     = new Uri(SourceId),
                Images =
                {
                    Members = new[] { new Image(), }
                }
            };

            this.sourceRepository.GetBrochure(Arg.Any <Uri>())
            .Returns(Task.FromResult(brochure));
            var pdfUploaded = new PdfUploaded
            {
                BlobUri  = BlobUri,
                Name     = Name,
                SourceId = SourceId,
            };

            // when
            await this.functions.Run(pdfUploaded);

            // then
            this.imageService.AddedImages.ShouldBe(0);
        }
Пример #3
0
        public async Task NotifyPdfUploaded <T>(T resource, string name)
            where T : Source
        {
            LogTo.Information("Notifying PDF upload for {0}", resource.Id);

            var id = this.matcher.Match <T>(resource.Id).Get <int>("id");

            var pdfUploaded = new PdfUploaded
            {
                BlobUri  = resource.Content.ContentUrl.ToString(),
                Name     = name,
                SourceId = resource.Id.ToString(),
            };

            await this.queue.AddMessage(PdfUploaded.Queue, pdfUploaded);

            await this.wishlistPersistence.MarkDone(id);
        }
Пример #4
0
        public async Task ConvertsCoverPage()
        {
            // given
            this.httpMessageHandler.Expect(BlobUri)
            .Respond(KnownMimeTypes.Pdf, Resource.AsStream("Pdfs/mb1.pdf"));
            var pdfUploaded = new PdfUploaded
            {
                BlobUri  = BlobUri,
                Name     = Name,
                SourceId = SourceId,
            };

            // when
            await this.functions.Run(pdfUploaded);

            // then
            this.imageService.AddedImages.ShouldBe(1);
            this.context.Received(1).SaveChangesAsync();
        }
Пример #5
0
        public async Task Run([QueueTrigger(PdfUploaded.Queue)] PdfUploaded pdf)
        {
            LogTo.Information($"Extracting pages from {pdf.Name}.pdf");

            var sourceUri = new Uri(pdf.SourceId);
            var source    = await this.sources.GetBrochure(sourceUri);

            if (source == null)
            {
                LogTo.Warning("Source {0} not found", sourceUri);
                return;
            }

            var sourceId = this.matcher.Match <Brochure>(sourceUri).Get <int>("id");

            if (source.HasNonLegacyImage)
            {
                LogTo.Warning("Source {0} already has images. Skipping.", sourceUri);
                return;
            }

            using (var pdfRequestStream = await this.Client.GetStreamAsync(pdf.BlobUri))
            {
                using (var pdfFile = File.Create(this.tempFile))
                {
                    await pdfRequestStream.CopyToAsync(pdfFile);
                }
            }

            var settings = new MagickReadSettings
            {
                Density    = new Density(300, 300),
                FrameCount = 1,
                FrameIndex = 0,
            };

            using (MagickImageCollection images = new MagickImageCollection())
            {
                using (var pdfContents = File.OpenRead(this.tempFile))
                {
                    images.Read(pdfContents, settings);

                    var image = images.First();
                    using (var imageStream = new MemoryStream())
                    {
                        image.Format = MagickFormat.Jpeg;
                        image.Write(imageStream);
                        imageStream.Seek(0, SeekOrigin.Begin);

                        await this.imageService.AddImage(sourceId, $"{sourceId}_cover", imageStream);

                        await this.sourcesContext.SaveChangesAsync();
                    }
                }
            }

            if (!source.Pages.HasValue)
            {
                LogTo.Information("Brochure has no page count. Setting page count from PDF");

                using (var pdfContents = File.OpenRead(this.tempFile))
                {
                    var pdfDoc = PdfReader.Open(pdfContents);
                    source.Pages = pdfDoc.PageCount;
                    await this.persistence.SaveBrochure(source);
                }
            }
        }