Example #1
0
        public async Task <IActionResult> GetPetImage(int id)
        {
            var request = new PetImageRequest
            {
                Update  = false,
                UserId  = _claimsCompat.ExtractFirstIdClaim(HttpContext.User),
                PetId   = id,
                Content = null
            };

            var port = new BasicPresenter <BlobUriResponse>();

            var success = await _imageUseCase.Handle(request, port);

            return((success) ? new RedirectResult(port.Response.Uri.ToString()) : NotFound());
        }
Example #2
0
        public async Task <IActionResult> UpdateProfileImage(int id)
        {
            // Ensure content type is supported
            // TODO: look at applying some sort of 'upstream' filter on this instead of checking here.
            //       (it'll probably help with preflight requests and the like)
            var supportedMimeTypes = new [] { "image/png", "image/jpeg" };

            if (!supportedMimeTypes.Any(x => x.Equals(Request.ContentType)))
            {
                return(new UnsupportedMediaTypeResult());
            }

            var request = new PetImageRequest
            {
                Update   = true,
                UserId   = _claimsCompat.ExtractFirstIdClaim(HttpContext.User),
                PetId    = id,
                Content  = Request.Body,
                MimeType = Request.ContentType
            };

            var port = new BasicPresenter <BlobUriResponse>();

            var success = await _imageUseCase.Handle(request, port);

            if (success)
            {
                return new OkObjectResult(port.Response)
                       {
                           StatusCode = 201
                       }
            }
            ;

            return(BadRequest());
        }
Example #3
0
        public async Task <bool> Handle(PetImageRequest message, IOutboundPort <BlobUriResponse> outputPort)
        {
            // verify that the user has access to the specified pet:
            var user = await _userStore.GetUserById(message.UserId);

            if (user == null)
            {
                return(false);
            }

            await _userStore.LoadPets(user);

            var pet = user.Pets.FirstOrDefault(p => p.Id == message.PetId);

            if (pet == null)
            {
                return(false);
            }

            // All pet images go to the image container/category.
            const string category = Constants.StorageCategories.Images;
            string       blobName; // set based on execution path.

            if (message.Update)
            {
                // Generate a new blob name (always) for pet images - ensures no conflict.
                blobName = $"{message.UserId}_{message.PetId}_{Guid.NewGuid()}";

                // upload the blob to storage.
                await _blobStore.WriteBlob(category, blobName, message.MimeType, message.Content);

                // associate a new blob entry to the pet
                var entity = _entityFactory.GetBlobRecordBuilder()
                             .SetCategory(category)
                             .SetName(blobName)
                             .Build();

                await _petStore.UpdateImage(pet, entity);
            }
            else
            {
                // load the current record from pet:
                await _petStore.LoadImageRecord(pet);

                blobName = pet.ProfileImage?.BlobName;
            }

            // check that there exists a blob associated to pet by this point.
            if (blobName == null)
            {
                return(false);
            }

            var response = new BlobUriResponse
            {
                Uri       = await _blobStore.GetBlobReadUri(category, blobName, 10, 30),
                ExpiresIn = 10
            };

            outputPort.Handle(response);
            return(true);
        }