public async Task UploadFileVerificationTest(string filename, bool isValidFileProvided, bool expectException) { // Arrange IFormFile file = null; UploadFileServiceModel uploadFileModel = new UploadFileServiceModel(); uploadFileModel.CheckSum = new CheckSumModel(); uploadFileModel.SoftwarePackageURL = "filename"; uploadFileModel.CheckSum.MD5 = "checkSum"; if (isValidFileProvided) { file = this.CreateSampleFile(filename, false); } this.mockStorage.Setup(x => x.UploadToBlobAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Stream>())) .ReturnsAsync(uploadFileModel); try { // Act var uploadedFileName = await this.controller.UploadFileAsync(file); // Assert Assert.False(expectException); Assert.Equal(filename, uploadedFileName.SoftwarePackageURL); Assert.Equal("checkSum", uploadedFileName.CheckSum.MD5); } catch (Exception) { Assert.True(expectException); } }
public async Task <UploadFileServiceModel> UploadFileAsync(IFormFile uploadedFile) { UploadFileServiceModel uploadFileModel = null; if (uploadedFile == null || uploadedFile.Length == 0 || string.IsNullOrEmpty(uploadedFile.FileName)) { throw new InvalidInputException("Uploaded file is missing or invalid."); } using (var stream = uploadedFile.OpenReadStream()) { var tenantId = this.GetTenantId(); uploadFileModel = await this.storage.UploadToBlobAsync(tenantId, uploadedFile.FileName, stream); } return(uploadFileModel); }
public async Task <UploadFileServiceModel> UploadToBlobAsync(string tenantId, string filename, Stream stream = null) { CloudStorageAccount storageAccount = null; CloudBlobContainer cloudBlobContainer = null; UploadFileServiceModel uploadFileModel = new UploadFileServiceModel(); string url = string.Empty; string md5CheckSum = string.Empty; string sha1CheckSum = string.Empty; string storageConnectionString = this.config.Global.StorageAccountConnectionString; string duration = this.config.Global.PackageSharedAccessExpiryTime; if (string.IsNullOrEmpty(tenantId)) { this.logger.LogError(new Exception("Tenant ID is blank, cannot create container without tenandId."), "Tenant ID is blank, cannot create container without tenandId."); return(null); } if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount)) { try { // Create the CloudBlobClient that represents the Blob storage endpoint for the storage account. CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); // Create a container cloudBlobContainer = cloudBlobClient.GetContainerReference($"{tenantId}-{SoftwarePackageStore}"); // Create the container if it does not already exist await cloudBlobContainer.CreateIfNotExistsAsync(); // Get a reference to the blob address, then upload the file to the blob. CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(filename); if (stream != null) { await cloudBlockBlob.UploadFromStreamAsync(stream); md5CheckSum = cloudBlockBlob.Properties.ContentMD5; using (var sha = SHA1.Create()) { stream.Position = 0; // Update position for computing hash var hash = sha.ComputeHash(stream); cloudBlockBlob.Metadata["SHA1"] = sha1CheckSum = Convert.ToBase64String(hash); } } else { this.logger.LogError(new Exception("Empty stream object in the UploadToBlob method."), "Empty stream object in the UploadToBlob method."); return(null); } url = Convert.ToString(this.GetBlobSasUri(cloudBlobClient, cloudBlobContainer.Name, filename, duration)); uploadFileModel.CheckSum = new CheckSumModel(); uploadFileModel.SoftwarePackageURL = url; uploadFileModel.CheckSum.MD5 = md5CheckSum; uploadFileModel.CheckSum.SHA1 = sha1CheckSum; return(uploadFileModel); } catch (StorageException ex) { this.logger.LogError(new Exception($"Exception in the UploadToBlob method- Message: {ex.Message} : Stack Trace - {ex.StackTrace.ToString()}"), $"Exception in the UploadToBlob method- Message: {ex.Message} : Stack Trace - {ex.StackTrace.ToString()}"); return(null); } } else { this.logger.LogError(new Exception("Error parsing CloudStorageAccount in UploadToBlob method"), "Error parsing CloudStorageAccount in UploadToBlob method"); return(null); } }