private static FileLocation GetFileLocationFromPhoto(IUserProfilePhoto photo, bool small) { var empty = photo as UserProfilePhotoEmpty; var full = photo as UserProfilePhoto; if (empty != null) { return(null); } else if (full != null) { var iFileLocation = small ? full.PhotoSmall : full.PhotoBig; var fileLocation = iFileLocation as FileLocation; if (fileLocation != null) { return(fileLocation); } else { // If the file location is empty, then we assume the user hasn't set a photo. // Fall-through } } return(null); }
public async Task <IUserProfilePhoto> GetUserProfilePhotoAsync(int userProfileId, cmEnums.BlobFileType blobFileType) { IUserProfilePhoto retVal = null; try { var userProfile = await Repo.Get_UserProfileAsync(userProfileId, 1); if (userProfile == null) { throw new InvalidOperationException($"Unable to find user profile in {nameof(GetUserProfilePhotoAsync)}() using userProfileId: {userProfileId}"); } if (!userProfile.PhotoBlobFileId.HasValue) { return(retVal); } var blobFiles = Repo.GetQueryable_BlobFile().Where(x => x.BlobFileId == userProfile.PhotoBlobFileId.Value || x.ParentBlobFileId == userProfile.PhotoBlobFileId.Value) .ToList().OrderBy(x => x.SizeInBytes); if (blobFiles.Count() > 0) { entCM.BlobFile bestmatchBlobFile = blobFiles.FirstOrDefault(); if (blobFileType == cmEnums.BlobFileType.Thumbnail_Image) { bestmatchBlobFile = blobFiles.FirstOrDefault(x => x.BlobFileTypeId == (int)cmEnums.BlobFileType.Thumbnail_Image); } else { bestmatchBlobFile = blobFiles.FirstOrDefault(x => x.BlobFileTypeId == (int)cmEnums.BlobFileType.Original_Image); } if (bestmatchBlobFile != null && !string.IsNullOrEmpty(bestmatchBlobFile.BlobUri)) { retVal = new UserProfilePhoto() { BlobFile = bestmatchBlobFile }; retVal.Data = await AzureStorageManager.GetBlobBytesByPrimaryUriAsync(new Uri(bestmatchBlobFile.BlobUri)); } } } catch (Exception ex) { Log.Error(ex.Message, LogMessageType.Instance.Exception_WebApi, ex, userName: userProfileId.ToString()); } return(retVal); }