コード例 #1
0
        public async Task <IActionResult> AddProfileImageAsync(Guid id)
        {
            IFormFileCollection file = Request.Form.Files;

            if (file.Count == 0)
            {
                return(BadRequest(new { message = $"Please upload at least 1 image file as profile picture to continue." }));
            }

            if (file.Count > 1)
            {
                return(BadRequest(new { message = "Only one image can be uploaded" }));
            }

            //Check if uploaded file is an image file or not
            if (!new string[] { "image/jpeg", "image/jpg", "image/png" }.Contains(file[0].ContentType))
            {
                return(BadRequest("Invalid format. The file is not an image. Only types .jpeg, .jpg or .png are allowed."));
            }

            ApplicationUser foundUser = await _userManager
                                        .Users
                                        .Include(x => x.ProfileImage)
                                        .Where(x => x.Id == id)
                                        .FirstOrDefaultAsync();


            //Delete the old photo from both Azure Blob and SQL
            if (foundUser.ProfileImage != null)
            {
                await _blobManager.DeleteFileFromBlobAsync(foundUser.ProfileImage.ContainerName, foundUser.ProfileImage.ResourceName);

                await _profileManager.DeleteProfileImageAsync(foundUser.ProfileImage.ProfileImageID);
            }

            string fileName  = ContentDispositionHeaderValue.Parse(file[0].ContentDisposition).FileName.Trim('"');
            string hostedURL = await _blobManager.UploadFileToBlobAsync(foundUser.Id.ToString(), fileName, file[0].OpenReadStream());

            foundUser.ProfileImage = new ProfileImage
            {
                ApplicationUser = foundUser,
                ContainerName   = foundUser.UserName,
                ProfileImageID  = Guid.NewGuid(),
                ProfileImageUrl = hostedURL,
                ResourceName    = fileName,
                UserID          = foundUser.Id
            };

            //Update the database
            await _userManager.UpdateAsync(foundUser);

            return(Ok(new { message = "Profile image successfully updated" }));
        }
コード例 #2
0
        public async Task DeleteVenueImageAsync(Guid VenueImageID)
        {
            try
            {
                VenueImage image = await _venueImageRepository.GetByIdAsync(VenueImageID);

                Guard.Against.NullItem <VenueImage>(image);
                await _blobService.DeleteFileFromBlobAsync(image.ContainerName, image.ResourceName);

                await _venueImageRepository.DeleteAsync(image);
            }
            catch (Exception)
            {
                throw;
            }
        }