Exemplo n.º 1
0
        public async Task <Guid> UpsertFursuitBadgeAsync(FursuitBadgeRegistration registration)
        {
            byte[] imageBytes = Convert.FromBase64String(registration.ImageContent);
            var    hash       = Hashing.ComputeHashSha1(imageBytes);

            await _sync.WaitAsync();

            try
            {
                var record = await _fursuitBadgeRepository.FindOneAsync(a => a.ExternalReference == registration.BadgeNo.ToString());

                if (record == null)
                {
                    record = new FursuitBadgeRecord();
                    record.NewId();

                    await _fursuitBadgeRepository.InsertOneAsync(record);
                }

                record.ExternalReference = registration.BadgeNo.ToString();
                record.OwnerUid          = $"RegSys:{_conventionSettings.ConventionNumber}:{registration.RegNo}";
                record.Gender            = registration.Gender;
                record.Name     = registration.Name;
                record.Species  = registration.Species;
                record.IsPublic = (registration.DontPublish == 0);
                record.WornBy   = registration.WornBy;
                record.Touch();

                var imageRecord = await _fursuitBadgeImageRepository.FindOneAsync(record.Id);

                if (imageRecord == null)
                {
                    imageRecord = new FursuitBadgeImageRecord
                    {
                        Id       = record.Id,
                        Width    = 240,
                        Height   = 320,
                        MimeType = "image/jpeg"
                    };
                    imageRecord.Touch();

                    await _fursuitBadgeImageRepository.InsertOneAsync(imageRecord);
                }

                if (imageRecord.SourceContentHashSha1 != hash)
                {
                    imageRecord.SourceContentHashSha1 = hash;

                    var image = Image.Load(imageBytes);

                    image.Resize(new ResizeOptions()
                    {
                        Mode    = ResizeMode.Max,
                        Size    = new Size(240, 320),
                        Sampler = new BicubicResampler()
                    });

                    var ms = new MemoryStream();
                    image.SaveAsJpeg(ms, new JpegEncoderOptions()
                    {
                        IgnoreMetadata = true, Quality = 85
                    });
                    imageRecord.SizeInBytes = ms.Length;
                    imageRecord.ImageBytes  = ms.ToArray();
                    ms.Dispose();

                    imageRecord.Touch();
                    await _fursuitBadgeImageRepository.ReplaceOneAsync(imageRecord);
                }

                await _fursuitBadgeRepository.ReplaceOneAsync(record);

                return(record.Id);
            }
            finally
            {
                _sync.Release();
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult> PostFursuitBadgeRegistrationAsync([FromBody] FursuitBadgeRegistration registration)
        {
            await _fursuitBadgeService.UpsertFursuitBadgeAsync(registration);

            return(Ok());
        }