Ejemplo n.º 1
0
        public async Task <bool> UpdateTalentPhoto(string talentId, IFormFile file)
        {
            try
            {
                User updatedUser = await _userRepository.GetByIdAsync(talentId);

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

                // Create a new FormFile so we can change the name before we pass it into _fileService.
                // We don't want the file to be named as the uploader called it but rather named as the talent's id and the current date as ticks.
                DateTime updatedDate = DateTime.Now;
                string   name        = talentId + updatedDate.Ticks.ToString();
                file = new Microsoft.AspNetCore.Http.Internal.FormFile(file.OpenReadStream(), 0, file.Length, name, name);

                string url = await _fileService.SaveFile(file, FileType.ProfilePhoto);

                if (!String.IsNullOrWhiteSpace(url))
                {
                    if (!String.IsNullOrWhiteSpace(updatedUser.ProfilePhoto))
                    {
                        await _fileService.DeleteFile(updatedUser.ProfilePhoto, FileType.ProfilePhoto);
                    }

                    // Save the profile photo name in the user document.
                    updatedUser.ProfilePhoto    = file.Name;
                    updatedUser.ProfilePhotoUrl = url;
                    updatedUser.UpdatedOn       = updatedDate;

                    await _userRepository.Update(updatedUser);

                    return(true);
                }

                return(false);
            }
            catch
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        public async Task <bool> UpdateTalentPhoto(string talentId, IFormFile file)
        {
            try
            {
                User profile = await _userRepository.GetByIdAsync(talentId);

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

                DateTime updatedDate = DateTime.Now;
                string   name        = talentId + updatedDate.Ticks.ToString();
                file = new Microsoft.AspNetCore.Http.Internal.FormFile(file.OpenReadStream(), 0, file.Length, name, name);

                string newFileUrl = await _fileService.SaveFile(file, FileType.ProfilePhoto);

                if (!String.IsNullOrWhiteSpace(newFileUrl))
                {
                    if (!String.IsNullOrWhiteSpace(profile.ProfilePhoto))
                    {
                        await _fileService.DeleteFile(profile.ProfilePhoto, FileType.ProfilePhoto);
                    }

                    profile.ProfilePhoto    = file.Name;
                    profile.ProfilePhotoUrl = newFileUrl;
                    profile.UpdatedOn       = updatedDate;

                    await _userRepository.Update(profile);

                    return(true);
                }

                return(false);
            }
            catch
            {
                return(false);
            }
        }