public async Task <bool> InsertFile(string customerID, BlobType type = BlobType.Aadhaar)
        {
            IFormFile file = null;

            byte[] fileByteContent = null;
            if (Request.Form.Files.Count > 0)
            {
                file = Request.Form.Files[0];
            }
            else
            {
                var input = Request.Form.AsEnumerable();

                foreach (var keyvalue in input)
                {
                    if (keyvalue.Key.Equals("customerID"))
                    {
                        customerID = keyvalue.Value;
                    }
                    else if (keyvalue.Key.Equals("type"))
                    {
                        var type1 = keyvalue.Value.ToString();
                        type = BlobUtilities.GetBlobType(type1);
                    }
                    else
                    {
                        var byteData    = keyvalue.Value;
                        var fileContent = byteData.ToString();
                        fileByteContent = Encoding.UTF8.GetBytes(fileContent);
                    }
                }
            }
            try
            {
                if (CloudStorageAccount.TryParse(_azureBlobConfig.Value.ConnectionString, out CloudStorageAccount storageAccount))
                {
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                    CloudBlobContainer container = BlobUtilities.GetBlobContainer(blobClient, type, _azureBlobConfig);

                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobUtilities.GenerateUniqueAadhaarImageName(customerID, type));

                    if (file != null)
                    {
                        await blockBlob.UploadFromStreamAsync(file.OpenReadStream());
                    }
                    else
                    {
                        await blockBlob.UploadFromByteArrayAsync(fileByteContent, 0, fileByteContent.Length);
                    }

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 2
0
 public static CloudBlobContainer GetBlobContainer(CloudBlobClient cloudBlobClient, BlobType type, IOptions <AzureAadhaarBlobConfig> _azureBlobConfig)
 {
     return(type switch
     {
         BlobType.Aadhaar => cloudBlobClient.GetContainerReference(_azureBlobConfig.Value.FinancingAadhaarContainerName),
         BlobType.PASSPORTPHOTO => cloudBlobClient.GetContainerReference(_azureBlobConfig.Value.FinancingPANContainerName),
         BlobType.Signature => cloudBlobClient.GetContainerReference(_azureBlobConfig.Value.FinancingSignatureContainerName),
         _ => throw new Exception("Invalid BlobType"),
     });