public async Task <byte[]> GetBlobBytesByBlobFileIdAsync(Guid blobFileId, cmEnums.BlobFileType blobFileType) { byte[] retVal = null; try { if (blobFileId == Guid.Empty) { return(retVal); } var blobFile = Repo.GetQueryable_BlobFile().Where(x => x.BlobFileId == blobFileId && x.BlobFileTypeId == (int)blobFileType).FirstOrDefault(); if (blobFile != null) { retVal = await AzureStorageManager.GetBlobBytesByPrimaryUriAsync(new Uri(blobFile.BlobUri)); } } catch (StorageException sex) { Log.Error($"Unable to retrieve blobFileId: {blobFileId.ToString()} in {nameof(GetBlobBytesByBlobFileIdAsync)}.", LogMessageType.Instance.Exception_Domain, sex); } return(retVal); }
public async Task <bool> DeleteFileFromStorageAsync(string blobName, cmEnums.BlobFileType containerType) { try { CloudBlockBlob blockBlob = GetBlockBlobReference(blobName, containerType); if (blockBlob != null) { blockBlob.DeleteIfExists(); } } catch (StorageException sex) { Log.Error($"Unable to delete blobName: {blobName.ToString()} in {nameof(DeleteFileFromStorageAsync)}.", LogMessageType.Instance.Exception_Domain, sex); } return(await Task.FromResult(true)); }
private CloudBlockBlob GetBlockBlobReference(string blobName, cmEnums.BlobFileType containerType) { CloudBlockBlob retVal = null; if (string.IsNullOrEmpty(blobName)) { return(null); } else { // Limitation: Azure Storage names must be all lowercase. blobName = blobName.ToLowerInvariant(); } if (containerType == cmEnums.BlobFileType.Original_Image) { retVal = _imageContainer.Value.GetBlockBlobReference(blobName); } else { retVal = _thumbnailContainer.Value.GetBlockBlobReference(blobName); } return(retVal); }
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); }
public async Task <string> UploadFileToStorageAsync(Stream fileStream, string blobName, cmEnums.BlobFileType containerType) { string retVal = null; try { CloudBlockBlob blockBlob = GetBlockBlobReference(blobName, containerType); if (blockBlob != null) { await blockBlob.UploadFromStreamAsync(fileStream); } retVal = blockBlob.StorageUri.PrimaryUri.ToString(); } catch (StorageException sex) { Log.Error($"Unable to delete blobName: {blobName.ToString()} in {nameof(DeleteFileFromStorageAsync)}.", LogMessageType.Instance.Exception_Domain, sex); } return(retVal); }