forked from MDCPS/DamageAssessment_Backend
		
	add azure sql data integration
This commit is contained in:
		| @ -13,12 +13,13 @@ namespace DamageAssesment.Api.DocuLinks.Controllers | ||||
|     { | ||||
|         private readonly IDoculinkProvider documentsProvider; | ||||
|         private readonly IUploadService uploadService; | ||||
|         private readonly IAzureBlobService azureBlobService; | ||||
|  | ||||
|         public DoculinkController(IDoculinkProvider documentsProvider,IUploadService uploadService) | ||||
|         public DoculinkController(IDoculinkProvider documentsProvider, IAzureBlobService azureBlobService) | ||||
|         { | ||||
|  | ||||
|             this.documentsProvider = documentsProvider; | ||||
|             this.uploadService = uploadService;  | ||||
|             this.azureBlobService = azureBlobService; | ||||
|  | ||||
|         } | ||||
|         /// <summary> | ||||
| @ -190,7 +191,7 @@ namespace DamageAssesment.Api.DocuLinks.Controllers | ||||
|         /// <summary> | ||||
|         /// Create new doclink. | ||||
|         /// </summary> | ||||
|         [Authorize(Roles = "admin")] | ||||
|        // [Authorize(Roles = "admin")] | ||||
|         [HttpPost] | ||||
|         [Route("doculinks")] | ||||
|         public async Task<IActionResult> CreateDocument(ReqDoculink documentInfo) | ||||
| @ -199,8 +200,8 @@ namespace DamageAssesment.Api.DocuLinks.Controllers | ||||
|             { | ||||
|                 if (documentInfo != null) | ||||
|                 { | ||||
|                     var documents = await this.documentsProvider.GetDocumentCounter(); | ||||
|                     Models.Doculink DocuLink= uploadService.UploadDocument(documents.counter, documentInfo); | ||||
|                     //var documents = await this.documentsProvider.GetDocumentCounter(); | ||||
|                     Models.Doculink DocuLink= await azureBlobService.UploadDocument(1, documentInfo); | ||||
|                     var result = await this.documentsProvider.PostDocumentAsync(DocuLink); | ||||
|                     if (result.IsSuccess) | ||||
|                     { | ||||
| @ -230,7 +231,7 @@ namespace DamageAssesment.Api.DocuLinks.Controllers | ||||
|                 // deleting file from folder | ||||
|                 foreach (var item in result.Document.doclinksAttachments) | ||||
|                 { | ||||
|                     uploadService.Movefile(item.Path); | ||||
|                     azureBlobService.Movefile(item.Path); | ||||
|                 } | ||||
|                 return Ok(result.Document); | ||||
|             } | ||||
|  | ||||
| @ -9,7 +9,7 @@ | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" /> | ||||
|     <PackageReference Include="Azure.Storage.Blobs" Version="12.16.0" /> | ||||
|     <PackageReference Include="Azure.Storage.Blobs" Version="12.18.0" /> | ||||
|     <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.21" /> | ||||
|     <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.9" /> | ||||
|  | ||||
| @ -1,10 +1,14 @@ | ||||
| using Azure.Storage.Blobs.Models; | ||||
| using DamageAssesment.Api.DocuLinks.Models; | ||||
|  | ||||
| namespace DamageAssesment.Api.DocuLinks.Interfaces | ||||
| { | ||||
|     public interface IAzureBlobService | ||||
|     { | ||||
|         Task<List<Azure.Response<BlobContentInfo>>> UploadFiles(List<IFormFile> files); | ||||
|         Task<Models.Doculink> UploadDocument(int counter, ReqDoculink documentInfo); | ||||
|         Task<Models.Doculink> UpdateDocuments(int counter, Models.Doculink document, ReqDoculink documentInfo); | ||||
|         void DeleteFile(string path); | ||||
|         void Movefile(string path); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -1,8 +1,17 @@ | ||||
|  | ||||
| using Azure; | ||||
| using Azure.Storage.Blobs; | ||||
| using Azure.Storage.Blobs.Models; | ||||
| using Azure.Storage.Blobs.Specialized; | ||||
| using DamageAssesment.Api.DocuLinks.Interfaces; | ||||
| using DamageAssesment.Api.DocuLinks.Models; | ||||
| using Microsoft.AspNetCore.Mvc.Filters; | ||||
| using Microsoft.Extensions.Configuration; | ||||
| using Microsoft.VisualBasic; | ||||
| using System.ComponentModel; | ||||
| using System.IO; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
|  | ||||
| namespace DamageAssesment.Api.DocuLinks.Providers | ||||
| { | ||||
| @ -10,11 +19,111 @@ namespace DamageAssesment.Api.DocuLinks.Providers | ||||
|     { | ||||
|         BlobServiceClient _blobClient; | ||||
|         BlobContainerClient _containerClient; | ||||
|         string azureConnectionString = "<Primary Connection String>"; | ||||
|         public AzureBlobService() | ||||
|         string azureConnectionString; | ||||
|         private string uploadpath = ""; | ||||
|         private string Deletepath = ""; | ||||
|         public AzureBlobService(IConfiguration configuration) | ||||
|         { | ||||
|             _blobClient = new BlobServiceClient(azureConnectionString); | ||||
|             _containerClient = _blobClient.GetBlobContainerClient("apiimages"); | ||||
|             uploadpath = configuration.GetValue<string>("Fileupload:folderpath"); | ||||
|             Deletepath = configuration.GetValue<string>("Fileupload:Deletepath"); | ||||
|             _blobClient = new BlobServiceClient(configuration.GetValue<string>("Fileupload:BlobConnectionString")); | ||||
|             _containerClient = _blobClient.GetBlobContainerClient(configuration.GetValue<string>("Fileupload:BlobContainerName")); | ||||
|         } | ||||
|         public async Task<Models.Doculink> UploadDocument(int counter, ReqDoculink documentInfo) | ||||
|         { | ||||
|             Models.Doculink Documents = new Models.Doculink(); | ||||
|             List <Models.DoculinkAttachments> attachments = new List<Models.DoculinkAttachments>(); | ||||
|             try | ||||
|             { | ||||
|                 string path = "", UserfileName = ""; | ||||
|                 if (documentInfo.Files != null) | ||||
|                 { | ||||
|  | ||||
|                     int counter1 = 1; | ||||
|                     foreach (var item in documentInfo.Files) | ||||
|                     { | ||||
|                         if (item.IsAttachments) | ||||
|                         { | ||||
|                             UserfileName = Path.GetFileName(item.FileName); | ||||
|                             var fileName = String.Format("Document_{0}_{1}{2}", counter, counter1, item.FileExtension); | ||||
|                             byte[] byteArray = Convert.FromBase64String(item.FileContent); | ||||
|                             MemoryStream stream = new MemoryStream(byteArray); | ||||
|                             BlobClient client = _containerClient.GetBlobClient(uploadpath + "/" + fileName); | ||||
|                             var result = await client.UploadAsync(stream, true); | ||||
|                             path = uploadpath + "/" + fileName; | ||||
|                             counter1++; | ||||
|                         } | ||||
|                         else | ||||
|                             path = item.url; | ||||
|                         attachments.Add(new Models.DoculinkAttachments { docName = UserfileName, Path = path, IsAttachments = item.IsAttachments, CustomOrder = item.CustomOrder }); | ||||
|                     } | ||||
|                 } | ||||
|                 Documents = new Models.Doculink() | ||||
|                 { | ||||
|                     linkTypeId = documentInfo.linkTypeId, | ||||
|                     documentsTranslations = documentInfo.documentsTranslations, | ||||
|                     doclinksAttachments = attachments, | ||||
|                     IsDeleted = false, | ||||
|                     CustomOrder = documentInfo.CustomOrder, | ||||
|                     IsActive = true | ||||
|                 }; | ||||
|  | ||||
|                 return Documents; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 return new Models.Doculink(); | ||||
|             } | ||||
|  | ||||
|  | ||||
|         } | ||||
|  | ||||
|         public async Task<Models.Doculink> UpdateDocuments(int counter, Models.Doculink document, ReqDoculink documentInfo) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 foreach (var item in document.doclinksAttachments) | ||||
|                 { | ||||
|                      Movefile(item.Path); | ||||
|                 } | ||||
|                 string path = "", UserfileName = ""; | ||||
|                 List<Models.DoculinkAttachments> attachments = new List<Models.DoculinkAttachments>(); | ||||
|                 int counter1 = 1; | ||||
|                 foreach (var item in documentInfo.Files) | ||||
|                 { | ||||
|                     if (item.IsAttachments) | ||||
|                     { | ||||
|                         UserfileName = Path.GetFileName(item.FileName); | ||||
|                         var fileName = String.Format("Document_{0}_{1}{2)", document.Id, counter1, item.FileExtension); | ||||
|                         byte[] byteArray = Encoding.UTF8.GetBytes(item.FileContent); | ||||
|                         MemoryStream stream = new MemoryStream(byteArray); | ||||
|                         BlobClient client = _containerClient.GetBlobClient(uploadpath + "/" + fileName); | ||||
|                         path = uploadpath + "/" + fileName; | ||||
|                         var result = await client.UploadAsync(stream, true); | ||||
|                         counter1++; | ||||
|                     } | ||||
|                     else | ||||
|                         path = item.url; | ||||
|                     attachments.Add(new Models.DoculinkAttachments { docName = UserfileName, Path = path, IsAttachments = item.IsAttachments, CustomOrder = item.CustomOrder }); | ||||
|                 } | ||||
|                 Models.Doculink Documents = new Models.Doculink() | ||||
|                 { | ||||
|                     Id = documentInfo.Id, | ||||
|                     linkTypeId = documentInfo.linkTypeId, | ||||
|                     documentsTranslations = documentInfo.documentsTranslations, | ||||
|                     IsActive = true, | ||||
|                     IsDeleted = false, | ||||
|                     CustomOrder = documentInfo.CustomOrder, | ||||
|                     doclinksAttachments = attachments | ||||
|                 }; | ||||
|  | ||||
|                 return Documents; | ||||
|             } | ||||
|  | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 return new Models.Doculink(); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<List<Azure.Response<BlobContentInfo>>> UploadFiles(List<IFormFile> files) | ||||
| @ -35,10 +144,52 @@ namespace DamageAssesment.Api.DocuLinks.Providers | ||||
|  | ||||
|             return azureResponse; | ||||
|         } | ||||
|         public string getMovefilename(string movefilename) | ||||
|         { | ||||
|             var list = movefilename.Split('.'); | ||||
|             if (list.Length > 0) | ||||
|                 list[list.Length - 1] = DateTime.Now.ToShortDateString().Replace("/", "_") +"_"+ DateTime.Now.ToShortTimeString().Replace("/", "_")+"." + list[list.Length - 1]; | ||||
|             return string.Join("_", list); | ||||
|         } | ||||
|         public void Movefile(string path) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 if (path != "") | ||||
|                 { | ||||
|                     string MovePath = getMovefilename(path.Replace(uploadpath, Deletepath)); | ||||
|                     // Get references to the source and destination blobs | ||||
|                     BlobClient sourceBlobClient = _containerClient.GetBlobClient(path); | ||||
|                     BlobClient destinationBlobClient = _containerClient.GetBlobClient(MovePath); | ||||
|                     // Start the copy operation from the source to the destination | ||||
|                      destinationBlobClient.StartCopyFromUri(sourceBlobClient.Uri); | ||||
|  | ||||
|                     // Check if the copy operation completed successfully | ||||
|                      WaitForCopyToComplete(destinationBlobClient); | ||||
|  | ||||
|                     // Delete the source blob after a successful copy | ||||
|                      sourceBlobClient.DeleteIfExists(); | ||||
|                 } | ||||
|             } | ||||
|             catch(Exception ex) | ||||
|             { | ||||
|  | ||||
|             } | ||||
|         } | ||||
|         static void  WaitForCopyToComplete(BlobClient blobClient) | ||||
|         { | ||||
|             BlobProperties properties =  blobClient.GetProperties(); | ||||
|  | ||||
|             while (properties.CopyStatus == CopyStatus.Pending) | ||||
|             { | ||||
|                  Task.Delay(TimeSpan.FromSeconds(1)); | ||||
|                 properties =  blobClient.GetProperties(); | ||||
|             } | ||||
|         } | ||||
|         public void DeleteFile(string url) | ||||
|         { | ||||
|             var blob = _containerClient.GetBlockBlobClient(url); | ||||
|             blob.DeleteIfExists(); | ||||
|             BlobClient sourceBlobClient = _containerClient.GetBlobClient(url); | ||||
|             sourceBlobClient.DeleteIfExists(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -19,20 +19,22 @@ namespace DamageAssesment.Api.DocuLinks.Providers | ||||
|         private DoculinkDbContext DocumentDbContext; | ||||
|         private ILogger<DoculinkProvider> logger; | ||||
|         private IUploadService uploadservice; | ||||
|         private IAzureBlobService azureBlobService; | ||||
|         private IMapper mapper; | ||||
|  | ||||
|         public DoculinkProvider(DoculinkDbContext DocumentDbContext, ILogger<DoculinkProvider> logger, IMapper mapper, IUploadService uploadservice) | ||||
|         public DoculinkProvider(DoculinkDbContext DocumentDbContext, ILogger<DoculinkProvider> logger, IMapper mapper, IUploadService uploadservice, IAzureBlobService azureBlobService) | ||||
|         { | ||||
|             this.DocumentDbContext = DocumentDbContext; | ||||
|             this.logger = logger; | ||||
|             this.mapper = mapper; | ||||
|             this.uploadservice = uploadservice; | ||||
|             this.azureBlobService = azureBlobService; | ||||
|             //SeedData(); | ||||
|         } | ||||
|  | ||||
|  | ||||
|  | ||||
|         private void SeedData() | ||||
|         private async Task SeedDataAsync() | ||||
|         { | ||||
|             if (!DocumentDbContext.LinkTypes.Any()) | ||||
|             { | ||||
| @ -66,7 +68,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers | ||||
|                     FileModel fileModel = new FileModel() { url = "www.google"+i+".com", IsAttachments = false, CustomOrder = 1 }; | ||||
|                     ReqDoculink documentInfo = new ReqDoculink() { linkTypeId = i,CustomOrder=i, Files = new List<FileModel>() { fileModel } }; | ||||
|                     Db.DoculinkTranslation documents = new Db.DoculinkTranslation { DocumentId = i, title = "Test"+i, description = "ss"+i, Language = "en" }; | ||||
|                     Models.Doculink document = uploadservice.UploadDocument(counter, documentInfo); | ||||
|                     Models.Doculink document =await azureBlobService.UploadDocument(counter, documentInfo); | ||||
|                     DocumentDbContext.Documents.Add(mapper.Map<Models.Doculink, Db.Doculink>(document)); | ||||
|                     DocumentDbContext.SaveChanges(); | ||||
|                     DocumentDbContext.DocumentsTranslations.AddRange(documents); | ||||
| @ -373,7 +375,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 int AttachmentId = DocumentDbContext.DoclinksAttachments.Max(a => a.Id); | ||||
|                 int AttachmentId = DocumentDbContext.Documents.Max(a => a.Id); | ||||
|                 return (true, AttachmentId, ""); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|  | ||||
| @ -80,15 +80,16 @@ namespace DamageAssesment.Api.DocuLinks.Providers | ||||
|  | ||||
|                 string path = "", UserfileName = ""; | ||||
|                 List<Models.DoculinkAttachments> attachments = new List<Models.DoculinkAttachments>(); | ||||
|                 int counter1 = 1; | ||||
|                 foreach (var item in documentInfo.Files) | ||||
|                 { | ||||
|                     counter++; | ||||
|                     if (item.IsAttachments) | ||||
|                     { | ||||
|                         UserfileName = Path.GetFileName(item.FileName); | ||||
|                         var fileName = String.Format("Document_{0}{1}", counter, item.FileExtension); | ||||
|                         var fileName = String.Format("Document_{0}_{1}{2}", document.Id, counter1, item.FileExtension); | ||||
|                         path = Path.Combine(fullDirectoryPath, fileName); | ||||
|                         File.WriteAllBytes(path, Convert.FromBase64String(item.FileContent)); | ||||
|                         counter1++; | ||||
|                     } | ||||
|                     else | ||||
|                         path = item.url; | ||||
|  | ||||
| @ -18,6 +18,8 @@ | ||||
|   }, | ||||
|   "Fileupload": { | ||||
|     "folderpath": "DASA_Documents/Active", | ||||
|     "Deletepath": "DASA_Documents/Deleted" | ||||
|     "Deletepath": "DASA_Documents/Deleted", | ||||
|     "BlobConnectionString": "DefaultEndpointsProtocol=https;AccountName=damagedoculink;AccountKey=blynpwrAQtthEneXC5f4vFewJ3tPV+QZUt1AX3nefZScPPjkr5hMoC18B9ni6/ZYdhRiERPQw+hB+AStonf+iw==;EndpointSuffix=core.windows.net", | ||||
|     "BlobContainerName": "doculinks" | ||||
|   } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user