Ejemplo n.º 1
0
        public async Task <ImageData> ProcessUncached()
        {
            //Fetch all blobs simultaneously
            var blobs = await Task.WhenAll(
                allBlobs
                .Select(async b =>
                        (await b.GetBlob())));

            //Add all StreamSources
            List <InputWatermark> watermarks = null;

            if (appliedWatermarks != null)
            {
                watermarks = new List <InputWatermark>(appliedWatermarks.Count);
                watermarks.AddRange(
                    appliedWatermarks.Select((t, i) =>
                {
                    if (blobs[i + 1] == null)
                    {
                        throw new BlobMissingException(
                            $"Cannot locate watermark \"{t.Name}\" at virtual path \"{t.VirtualPath}\"");
                    }
                    return(new InputWatermark(
                               new StreamSource(blobs[i + 1].OpenRead(), true),
                               t.Watermark));
                }));
            }

            using var buildJob = new ImageJob();
            var jobResult = await buildJob.BuildCommandString(
                new StreamSource(blobs[0].OpenRead(), true),
                new BytesDestination(), CommandString, watermarks)
                            .Finish()
                            .SetSecurityOptions(options.JobSecurityOptions)
                            .InProcessAsync();

            var bytes = jobResult.First.TryGetBytes();

            if (!bytes.HasValue || bytes.Value.Count < 1)
            {
                throw new InvalidOperationException("Image job returned zero bytes.");
            }

            return(new ImageData
            {
                ContentType = jobResult.First.PreferredMimeType,
                FileExtension = jobResult.First.PreferredExtension,
                ResultBytes = bytes.Value
            });
        }
Ejemplo n.º 2
0
        public async Task TestBuildCommandString()
        {
            var imageBytes = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/TQBcNTh/AAAAAXRSTlPM0jRW/QAAAApJREFUeJxjYgAAAAYAAzY3fKgAAAAASUVORK5CYII=");

            // We wrap the job in a using() statement to free memory faster
            using (var b = new ImageJob())
            {
                var r = await b.BuildCommandString(
                    new BytesSource(imageBytes), // or new StreamSource(Stream stream, bool disposeStream)
                    new BytesDestination(),      // or new StreamDestination
                    "width=3&height=2&mode=stretch&scale=both&format=webp")
                        .Finish().InProcessAsync();

                Assert.Equal(3, r.First.Width);
                Assert.Equal("webp", r.First.PreferredExtension);
                Assert.True(r.First.TryGetBytes().HasValue);
            }
        }
Ejemplo n.º 3
0
        public async Task TestBuildCommandStringWithWatermarks()
        {
            var imageBytes = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/TQBcNTh/AAAAAXRSTlPM0jRW/QAAAApJREFUeJxjYgAAAAYAAzY3fKgAAAAASUVORK5CYII=");

            using (var b = new ImageJob())
            {
                var watermarks = new List <InputWatermark>();
                watermarks.Add(new InputWatermark(new BytesSource(imageBytes), new WatermarkOptions()));
                watermarks.Add(new InputWatermark(new BytesSource(imageBytes), new WatermarkOptions().SetGravity(new ConstraintGravity(100, 100))));

                var r = await b.BuildCommandString(
                    new BytesSource(imageBytes),
                    new BytesDestination(),
                    "width=3&height=2&mode=stretch&scale=both&format=webp", watermarks).Finish().InProcessAsync();

                Assert.Equal(3, r.First.Width);
                Assert.Equal("webp", r.First.PreferredExtension);
                Assert.True(r.First.TryGetBytes().HasValue);
            }
        }
Ejemplo n.º 4
0
        public async Task <ImageData> ProcessUncached()
        {
            //Fetch all blobs simultaneously
            var blobs = await Task.WhenAll(
                allBlobs
                .Select(BlobFetchResult.FromCache));

            try
            {
                //Add all
                List <InputWatermark> watermarks = null;
                if (appliedWatermarks != null)
                {
                    watermarks = new List <InputWatermark>(appliedWatermarks.Count);
                    watermarks.AddRange(
                        appliedWatermarks.Select((t, i) =>
                    {
                        if (blobs[i + 1] == null)
                        {
                            throw new BlobMissingException(
                                $"Cannot locate watermark \"{t.Name}\" at virtual path \"{t.VirtualPath}\"");
                        }
                        return(new InputWatermark(
                                   blobs[i + 1].GetBytesSource(),
                                   t.Watermark));
                    }));
                }

                using var buildJob = new ImageJob();
                var jobResult = await buildJob.BuildCommandString(
                    blobs[0].GetBytesSource(),
                    new BytesDestination(), CommandString, watermarks)
                                .Finish()
                                .SetSecurityOptions(options.JobSecurityOptions)
                                .InProcessAsync();

                GlobalPerf.Singleton.JobComplete(new ImageJobInstrumentation(jobResult)
                {
                    FinalCommandKeys = FinalQuery.Keys,
                    ImageDomain      = ImageDomain,
                    PageDomain       = PageDomain
                });

                // TryGetBytes returns the buffer from a regular MemoryStream, not a recycled one
                var resultBytes = jobResult.First.TryGetBytes();

                if (!resultBytes.HasValue || resultBytes.Value.Count < 1 || resultBytes.Value.Array == null)
                {
                    throw new InvalidOperationException("Image job returned zero bytes.");
                }

                return(new ImageData
                {
                    ContentType = jobResult.First.PreferredMimeType,
                    ResultBytes = resultBytes.Value
                });
            }
            finally
            {
                foreach (var b in blobs)
                {
                    b?.Dispose();
                }
            }
        }