Exemplo n.º 1
0
        public async Task <bool> Handle(BlobStorageRequest message, IOutboundPort <BlobUriResponse> outputPort)
        {
            // Trust user guid? Ideally only authenticated controllers are getting here.
            if (message.UserId == Guid.Empty)
            {
                return(false);
            }

            // compose blob name if necessary
            if (string.IsNullOrWhiteSpace(message.BlobName) && message.Mode == BlobStorageRequest.Operation.Write)
            {
                message.BlobName = $"{message.UserId}_{DateTime.UtcNow}_{Guid.NewGuid()}";
            }

            //  can't read a non-existent blob.
            if (string.IsNullOrWhiteSpace(message.BlobName))
            {
                return(false);
            }

            if (message.Mode == BlobStorageRequest.Operation.Write)
            {
                if (message.Content == null)
                {
                    return(false);                         // can't write with no content.
                }
                await _blobStore.WriteBlob(message.Category, message.BlobName, null, message.Content);
            }

            var uri = await _blobStore.GetBlobReadUri(message.Category, message.BlobName, 60, 60);

            // call through to blob store method.
            var response = new BlobUriResponse
            {
                Uri       = uri,
                ExpiresIn = 60
            };

            // send back response
            outputPort.Handle(response);
            return(true);
        }
Exemplo n.º 2
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);
        }