Esempio n. 1
0
 private ProcessingConfiguration ToConfiguration(ConfigurationSnapshot snapshot)
 {
     return(ProcessingConfiguration.New(
                avoidDuplicates: snapshot.AvoidDuplicates,
                sizes: snapshot.Sizes.Select(s => new SizeConfiguration(
                                                 tag: Name.FromString(s.Tag).UnwrapOrThrow(),
                                                 maxWidth: s.MaxWidth.ToOption(),
                                                 maxHeight: s.MaxHeight.ToOption(),
                                                 crop: s.Crop != null
                 ? Option.Some(new CropConfiguration(
                                   aspectRatio: AspectRatio.FromString(s.Crop.AspectRatio).UnwrapOrThrow(),
                                   cropStrategy: (CropStrategy)Enum.Parse(typeof(CropStrategy), s.Crop.CropStrategy, true),
                                   backgroundColor: s.Crop.Color.ToOption().Map(s => new Color(s))
                                   )) : Option.None(),
                                                 quality: Quality.FromScalar(s.Quality).UnwrapOrThrow(),
                                                 format: ImageFormat.FromExtension(s.Format).UnwrapOrThrow()
                                                 )).ToArray()
                ).UnwrapOrThrow());
 }
        private static Result <Configuration.ProcessingConfiguration, Error> FromContract(Contracts.ProcessingConfiguration value)
        {
            var sizesResult = value.Sizes.Select(s =>
            {
                var cropResult = Result.Success <Option <Configuration.CropConfiguration>, Error>(Option.None());
                if (s.Crop != null)
                {
                    if (Enum.TryParse <CropStrategy>(s.Crop.CropStrategy, true, out var strategy))
                    {
                        var colorResult = Result.Success <Option <Color>, Error>(Option.None());
                        if (s.Crop.Color != null)
                        {
                            colorResult = Color.FromString(s.Crop.Color).MapSuccess(c => Option.Some(c));
                        }

                        cropResult = colorResult.AndThen(color => AspectRatio.FromString(s.Crop.AspectRatio)
                                                         .MapSuccess(ratio => Option.Some(new Configuration.CropConfiguration(ratio, strategy, color))));
                    }
                    else
                    {
                        cropResult = Result.Failure(Error.ValidationError($"Unknown crop strategy {s.Crop.CropStrategy}"));
                    }
                }

                return(cropResult
                       .AndThen(crop => Name.FromString(s.Tag)
                                .AndThen(tag => Quality.FromScalar(s.Quality)
                                         .AndThen(quality => ImageFormat.FromExtension(s.Format)
                                                  .MapSuccess(format => new Configuration.SizeConfiguration(
                                                                  tag,
                                                                  s.MaxWidth.ToOption(),
                                                                  s.MaxHeight.ToOption(),
                                                                  crop,
                                                                  quality,
                                                                  format))))));
            }).Railway();

            return(sizesResult.AndThen(sizes => Configuration.ProcessingConfiguration.New(
                                           value.AvoidDuplicates,
                                           sizes
                                           )));
        }
 private static Image ToAggregate(ImageSnapshot snapshot)
 {
     return(Image.New(
                new Id(snapshot.Id),
                snapshot.RowVersion,
                Instant.FromDateTimeUtc(snapshot.UploadedAt),
                snapshot.Meta?.Length > 0 ? Option.Some(snapshot.Meta) : Option.None(),
                new Color(snapshot.FillColor),
                new Color(snapshot.EdgeColor),
                Md5.FromByteArray(snapshot.Md5).UnwrapOrThrow(),
                snapshot.Sizes.Select(t => new ImageSize(
                                          Name.FromString(t.Tag).UnwrapOrThrow(),
                                          Resolution.FromScalar(t.Width, t.Height).UnwrapOrThrow(),
                                          AspectRatio.FromString(t.AspectRatio).UnwrapOrThrow(),
                                          t.CropStrategy != null ? Option.Some((CropStrategy)Enum.Parse(typeof(CropStrategy), t.CropStrategy, true)) : Option.None(),
                                          ImageFormat.FromExtension(t.ImageFormat).UnwrapOrThrow(),
                                          Quality.FromScalar(t.Quality).UnwrapOrThrow(),
                                          t.DuplicateOf != null ? Option.Some(Name.FromString(t.DuplicateOf).UnwrapOrThrow()) : Option.None()
                                          )).ToArray()
                ).UnwrapOrThrow());
 }