Exemple #1
0
        public async Task <ApiResponse> Handle(UploadFileDemoCommand request, CancellationToken cancellationToken)
        {
            ApiResponse response = new ApiResponse();

            try
            {
                var    file     = request.fileData;
                string fileName = request.fileData.FileName;
                string ext      = Path.GetExtension(fileName).ToLower();

                // validate file type
                if (ext == ".doc" || ext == ".docx" || ext == ".txt" || ext == ".xlsx" || ext == ".pdf")
                {
                    // Auth
                    var webRootfilePath = _hostingEnvironment.WebRootPath;

                    var path = Path.Combine(webRootfilePath, "demo.xlsx");


                    string GoogleServiceAccountDirectory = Path.Combine(Directory.GetCurrentDirectory(), "GoogleCredentials/" + "credentials.json");
                    GoogleServiceAccountDirectory = GoogleServiceAccountDirectory.Replace(@"\", "/");

                    Console.WriteLine($"Linux GoogleServiceAccountDirectory : {GoogleServiceAccountDirectory}");

                    Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", GoogleServiceAccountDirectory);
                    using (Stream objStream = new FileStream(Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS"), FileMode.Open, FileAccess.Read))
                    {
                        Console.WriteLine($"obj stream:{"GOOGLE_APPLICATION_CREDENTIALS"}");
                        //UploadFile(StaticResource.BucketName, @"D:\poonam\newdoc.docx", "abc");

                        var intdata = GCBucket.UploadFile(Environment.GetEnvironmentVariable("GOOGLE_BUCKET_NAME"), path);
                    }

                    // Upload
                }
                else
                {
                    response.StatusCode = StaticResource.failStatusCode;
                    response.Message    = StaticResource.FileText;
                }
            }
            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = StaticResource.SomethingWrong + ex.Message;
                await Task.Delay(0);
            }
            return(response);
        }
        public async Task <ApiResponse> Handle(DownloadFileFromBucketCommand request, CancellationToken cancellationToken)
        {
            ApiResponse response = new ApiResponse();

            try
            {
                string bucketName = Environment.GetEnvironmentVariable("GOOGLE_BUCKET_NAME");
                response.data.SignedUrl = await GCBucket.GetSignedURL(bucketName, request.ObjectName);

                response.StatusCode = StaticResource.successStatusCode;
                response.Message    = StaticResource.SuccessText;
            }
            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = StaticResource.SomethingWrong + ex;
            }
            return(response);
        }
        public async Task <ApiResponse> Handle(DeleteActivityDocumentCommand request, CancellationToken cancellationToken)
        {
            ApiResponse response = new ApiResponse();

            try
            {
                string bucketName = Environment.GetEnvironmentVariable("GOOGLE_BUCKET_NAME");
                if (bucketName != null)
                {
                    // Get document
                    var documentDetail = await _dbContext.ActivityDocumentsDetail.FirstOrDefaultAsync(x => x.ActtivityDocumentId == request.activityDocumentId);

                    if (documentDetail != null)
                    {
                        if (await GCBucket.DeleteObject(bucketName, documentDetail.ActivityDocumentsFilePath))
                        {
                            documentDetail.IsDeleted    = true;
                            documentDetail.ModifiedById = request.ModifiedById;
                            documentDetail.ModifiedDate = request.ModifiedDate;
                            await _dbContext.SaveChangesAsync();

                            response.StatusCode = StaticResource.successStatusCode;
                            response.Message    = StaticResource.SuccessText;
                        }
                    }
                }
                else
                {
                    response.StatusCode = StaticResource.failStatusCode;
                    response.Message    = StaticResource.BucketNameNotFound;
                }
            }
            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = StaticResource.SomethingWrong + ex;
            }
            return(response);
        }
        public async Task <ApiResponse> Handle(UploadReviewDragAndDropCommand request, CancellationToken cancellationToken)
        {
            ApiResponse response = new ApiResponse();

            try
            {
                ProjectDetail projectDetail = await _dbContext.ProjectDetail.FirstOrDefaultAsync(x => x.ProjectId == request.ProjectId && x.IsDeleted == false);

                if (projectDetail == null)
                {
                    throw new Exception("Project Id not found");
                }
                string folderName = projectDetail.ProjectCode;
                //code to read credential from environment variables
                //Console.WriteLine("---------- Before Credential create path----------");
                string googleApplicationCredentail = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
                //Console.WriteLine($"*******************googleApplicationCredentail are:{googleApplicationCredentail}");
                if (googleApplicationCredentail == null)
                {
                    string GoogleServiceAccountDirectory = Path.Combine(Directory.GetCurrentDirectory(), "GoogleCredentials/" + "credentials.json");
                    //Console.WriteLine($"*********GoogleServiceAccountDirectory :{GoogleServiceAccountDirectory}");
                    Console.WriteLine($"------GoogleServiceAccountDirectory cred null-----");
                    GoogleServiceAccountDirectory = GoogleServiceAccountDirectory.Replace(@"\", "/");
                    Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", GoogleServiceAccountDirectory);
                }

                using (Stream objStream = new FileStream(Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS"), FileMode.Open, FileAccess.Read))
                {
                    //Console.WriteLine("--------- Environment Credential Read successfully----- ----------");

                    string BucketName = Environment.GetEnvironmentVariable("GOOGLE_BUCKET_NAME");
                    //Console.WriteLine($"BucketName:{BucketName}");
                    //Console.WriteLine("--------- check For upload----- ----------");
                    string folderWithProposalFile = StaticResource.ProjectsFolderName + "/" + folderName + "/" + request.FileName;

                    ApproveProjectDetails objRes = new ApproveProjectDetails();

                    try
                    {
                        // --------------------code to get response credential from environment variables.
                        string obj = await GCBucket.UploadOtherProposalDocuments(BucketName, folderWithProposalFile, request.file, request.FileName, request.ext);

                        objRes = await _dbContext.ApproveProjectDetails.FirstOrDefaultAsync(x => x.ProjectId == request.ProjectId && x.IsDeleted == false);

                        Console.WriteLine($"Final bucket response : {obj}");

                        if (obj != null)
                        {
                            if (objRes == null)
                            {
                                objRes = new ApproveProjectDetails
                                {
                                    ProjectId    = request.ProjectId,
                                    FileName     = request.FileName,
                                    FilePath     = obj,
                                    CommentText  = request.CommentText,
                                    UploadedFile = null,
                                    IsDeleted    = false,
                                    CreatedById  = request.CreatedById,
                                    IsApproved   = request.IsApproved,
                                    CreatedDate  = request.CreatedDate,
                                    //Review completion date for proposal report when proposal is completed as isapproved is true
                                    ReviewCompletionDate = DateTime.UtcNow
                                };
                                await _dbContext.ApproveProjectDetails.AddAsync(objRes);

                                await _dbContext.SaveChangesAsync();
                            }
                            else
                            {
                                objRes.ProjectId    = request.ProjectId;
                                objRes.FileName     = request.FileName;
                                objRes.CommentText  = request.CommentText;
                                objRes.FilePath     = obj;
                                objRes.UploadedFile = null;
                                objRes.IsDeleted    = false;
                                objRes.ModifiedDate = request.ModifiedDate;
                                objRes.ModifiedById = request.ModifiedById;
                                objRes.IsApproved   = request.IsApproved;
                                //Review completion date for proposal report when proposal is completed as isapproved is true
                                objRes.ReviewCompletionDate = DateTime.UtcNow;
                                await _dbContext.SaveChangesAsync();
                            }
                        }
                        else
                        {
                            throw new Exception("Failed to upload. Try again!");
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Upload using Environment variable failed");
                        Console.WriteLine($"--------------Using environment variable exception--: {ex}");
                    }

                    response.data.ApproveProjectDetails = objRes;
                    response.StatusCode = StaticResource.successStatusCode;
                    response.Message    = "Success";
                }
            }

            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = StaticResource.SomethingWrong + ex.Message;
            }
            return(response);
        }
Exemple #5
0
        public async Task <ApiResponse> Handle(UploadProjectActivityDocumentFileCommand request, CancellationToken cancellationToken)
        {
            ApiResponse response = new ApiResponse();

            try
            {
                ActivityDocumentDetailModel activityModel = new ActivityDocumentDetailModel();
                var projectDetail = await _dbContext.ProjectActivityDetail
                                    .Include(x => x.ProjectBudgetLineDetail.ProjectDetail)
                                    .FirstOrDefaultAsync(x => x.ActivityId == request.ActivityID && x.IsDeleted == false);

                string folderName = projectDetail?.ProjectBudgetLineDetail.ProjectDetail.ProjectCode;

                string googleApplicationCredentail = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
                if (googleApplicationCredentail == null)
                {
                    string GoogleServiceAccountDirectory = Path.Combine(Directory.GetCurrentDirectory(), "GoogleCredentials/" + "credentials.json");
                    GoogleServiceAccountDirectory = GoogleServiceAccountDirectory.Replace(@"\", "/");
                    Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", GoogleServiceAccountDirectory);
                }
                using (Stream objStream = new FileStream(Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS"), FileMode.Open, FileAccess.Read))
                {
                    string bucketName = Environment.GetEnvironmentVariable("GOOGLE_BUCKET_NAME");
                    if (bucketName != null)
                    {
                        ActivityDocumentsDetail docObj = new ActivityDocumentsDetail();

                        string folderWithProposalFile = StaticResource.ProjectsFolderName + "/" + folderName + "/" + StaticResource.ProjectActivityFolderName + "/" + request.FileName;
                        string uploadedFileResponse   = await GCBucket.UploadOtherProposalDocuments(bucketName, folderWithProposalFile, request.File, request.FileName, request.Ext);

                        if (!string.IsNullOrEmpty(uploadedFileResponse))
                        {
                            docObj.ActivityId = request.ActivityID;
                            docObj.ActivityDocumentsFilePath = uploadedFileResponse;
                            docObj.StatusId     = request.StatusID;
                            docObj.CreatedById  = request.CreatedById;
                            docObj.IsDeleted    = false;
                            docObj.MonitoringId = request.MonitoringID;
                            docObj.CreatedDate  = request.CreatedDate;

                            await _dbContext.ActivityDocumentsDetail.AddAsync(docObj);

                            await _dbContext.SaveChangesAsync();
                        }

                        ActivityDocumentDetailModel obj = new ActivityDocumentDetailModel
                        {
                            ActivityDocumentId        = docObj.ActtivityDocumentId,
                            ActivityDocumentsFilePath = docObj.ActivityDocumentsFilePath,
                            ActivityDocumentsFileName = docObj.ActivityDocumentsFilePath.Split('/').Last(),
                            StatusId   = docObj.StatusId,
                            ActivityId = docObj.ActivityId,
                        };

                        response.data.activityDocumnentDetail = obj;
                        response.StatusCode = StaticResource.successStatusCode;
                        response.Message    = StaticResource.SuccessText;
                    }
                    else
                    {
                        response.StatusCode = StaticResource.failStatusCode;
                        response.Message    = StaticResource.BucketNameNotFound;
                    }
                }
            }
            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = StaticResource.SomethingWrong + ex.Message;
            }
            return(response);
        }
Exemple #6
0
        public async Task <ApiResponse> Handle(UploadEDIProposalFileCommand request, CancellationToken cancellationToken)
        {
            ApiResponse response = new ApiResponse();

            try
            {
                long projectId = request.ProjectId;

                var folderDetail = await _dbContext.ProjectProposalDetail.FirstOrDefaultAsync(x => x.ProjectId == projectId && x.IsDeleted == false);

                //Console.WriteLine("------Before other file Credential path Upload----------");

                string googleApplicationCredentail = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
                //Console.WriteLine($"*******************googleApplicationCredentail are:{googleApplicationCredentail}");
                if (googleApplicationCredentail == null)
                {
                    string GoogleServiceAccountDirectory = Path.Combine(Directory.GetCurrentDirectory(), "GoogleCredentials/" + "credentials.json");
                    //Console.WriteLine($"*********GoogleServiceAccountDirectory :{GoogleServiceAccountDirectory}");
                    GoogleServiceAccountDirectory = GoogleServiceAccountDirectory.Replace(@"\", "/");
                    Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", GoogleServiceAccountDirectory);
                }

                GoogleCredentialModel result = new GoogleCredentialModel
                {
                    ApplicationName = StaticResource.ApplicationName,
                    BucketName      = StaticResource.BucketName,
                    EmailId         = StaticResource.EmailId,
                    ProjectId       = StaticResource.ProjectId,
                    Projects        = StaticResource.ProjectsFolderName,
                    HR         = StaticResource.HRFolderName,
                    Accounting = StaticResource.AccountingFolderName,
                    Store      = StaticResource.StoreFolderName
                };

                using (Stream objStream = new FileStream(Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS"), FileMode.Open, FileAccess.Read))
                {
                    var proposaldata = await _dbContext.ProjectProposalDetail.FirstOrDefaultAsync(x => x.ProjectId == projectId && x.IsDeleted == false);

                    string BucketName             = Environment.GetEnvironmentVariable("GOOGLE_BUCKET_NAME");
                    string folderWithProposalFile = StaticResource.ProjectsFolderName + "/" + folderDetail.FolderName + "/" + request.fileName;

                    //Console.WriteLine($"BucketName:{BucketName}");
                    string uploadedFileResponse = await GCBucket.UploadOtherProposalDocuments(BucketName, folderWithProposalFile, request.file, request.fileName, request.ext);

                    if (uploadedFileResponse != null)
                    {
                        ProjectProposalDetail proposaldetails = await _dbContext.ProjectProposalDetail.FirstOrDefaultAsync(x => x.ProjectId == projectId && x.IsDeleted == false);

                        if (proposaldetails == null)
                        {
                            proposaldetails = new ProjectProposalDetail();
                        }

                        if (request.ProposalType == "Proposal")
                        {
                            proposaldetails.FolderName           = folderDetail.FolderName;
                            proposaldetails.ProposalFileName     = request.fileName;
                            proposaldetails.ProposalWebLink      = uploadedFileResponse;
                            proposaldetails.ProjectId            = request.ProjectId;
                            proposaldetails.CreatedDate          = request.CreatedDate;
                            proposaldetails.IsDeleted            = false;
                            proposaldetails.CreatedById          = request.CreatedById;
                            response.data.ProposalWebLink        = uploadedFileResponse;
                            response.data.ProposalWebLinkExtType = request.ext;
                        }
                        else
                        {
                            if (request.ProposalType == "EOI")
                            {
                                proposaldetails.FolderName     = folderDetail.FolderName;
                                proposaldetails.EDIFileName    = request.fileName;
                                proposaldetails.EDIFileWebLink = uploadedFileResponse;
                                proposaldetails.EDIFileExtType = request.ext;
                                // response folder path
                                response.data.EDIWebLink        = uploadedFileResponse;
                                response.data.EDIWebLinkExtType = request.ext;
                            }
                            else if (request.ProposalType == "BUDGET")
                            {
                                proposaldetails.FolderName = folderDetail.FolderName;

                                proposaldetails.BudgetFileName    = request.fileName;
                                proposaldetails.BudgetFileWebLink = uploadedFileResponse;
                                proposaldetails.BudgetFileExtType = request.ext;

                                // response folder path
                                response.data.BudgetWebLink        = uploadedFileResponse;
                                response.data.BudgetWebLinkExtType = request.ext;
                            }
                            else if (request.ProposalType == "CONCEPT")
                            {
                                proposaldetails.FolderName          = folderDetail.FolderName;
                                proposaldetails.ConceptFileName     = request.fileName;
                                proposaldetails.ConceptFileWebLink  = uploadedFileResponse;
                                proposaldetails.ConceptFileExtType  = request.ext;
                                response.data.ConceptWebLink        = uploadedFileResponse;
                                response.data.ConceptWebLinkExtType = request.ext;
                            }
                            else if (request.ProposalType == "PRESENTATION")
                            {
                                proposaldetails.FolderName              = folderDetail.FolderName;
                                proposaldetails.PresentationFileName    = request.fileName;
                                proposaldetails.PresentationFileWebLink = uploadedFileResponse;
                                proposaldetails.PresentationExtType     = request.ext;

                                // response folder path
                                response.data.PresentationWebLink        = uploadedFileResponse;
                                response.data.PresentationWebLinkExtType = request.ext;
                            }
                            proposaldata.ProjectId    = request.ProjectId;
                            proposaldata.ModifiedDate = request.ModifiedDate;
                        }

                        if (proposaldetails.ProjectProposaldetailId == 0)
                        {
                            await _dbContext.ProjectProposalDetail.AddAsync(proposaldetails);

                            await _dbContext.SaveChangesAsync();
                        }
                        else
                        {
                            await _dbContext.SaveChangesAsync();
                        }

                        response.StatusCode = StaticResource.successStatusCode;
                        response.Message    = "Success";
                    }

                    else
                    {
                        throw new Exception("Failed to upload. Try again!");
                    }
                }
            }
            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = StaticResource.SomethingWrong + ex.Message;
            }
            return(response);
        }