Esempio n. 1
0
        public async Task <JsonResult> InsertAttachment([FromBody] List <AttachmentEntity> attachments, int headerId)
        {
            try
            {
                string        companyCode = HttpContext.User.Claims.First(c => c.Type == "CompanyCode").Value;
                HeaderEntity  header      = dapperHelper.ExecuteSP_Single <HeaderEntity>(Header.GET_BY_ID, new { @headerId = headerId, @companyId = 0 }).Result;
                CompanyEntity buyer       = dapperHelper.ExecuteSP_Single <CompanyEntity>(SpCompany.GET_BY_ID, new { @companyId = header.BuyerCompanyId }).Result;
                string        buyerCode   = buyer.CompanyCode;

                string rootPath = Directory.GetCurrentDirectory();
                rootPath = Path.GetFullPath(Path.Combine(rootPath, @"..\..\"));
                string savePath      = rootPath + "Attachments";         //Ftp
                string savePathAzure = buyerCode + "\\" + "attachments"; //Azure
                foreach (var attachment in attachments)
                {
                    string filePath      = savePath + "\\" + companyCode + "_" + headerId;              //Ftp
                    string filePathAzure = savePathAzure + "\\" + companyCode + "_" + headerId;         //Azure
//                    attachment.AttachmentPath = filePath + "_" + attachment.AttachmentDescription;//Ftp
                    attachment.AttachmentPath = filePathAzure + "_" + attachment.AttachmentDescription; //Azure
                }
                string createdBy = HttpContext.User.Identity.Name;
                await attachmentServices.InsertAttachment(attachments, createdBy, headerId);

                var result = 0;
                return(Json(result));
            }
            catch (Exception ex)
            {
                await errorServices.ErrorNotification(ex, HttpContext.User.Identity.Name, "", "");

                return(Json(ex));
            }
        }
Esempio n. 2
0
 public async Task UpdateHeaderWaybill(HeaderEntity header, string modifiedBy, int userId)
 {
     try
     {
         await headerRepository.UpdateHeaderWaybill(header, modifiedBy, userId);
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Esempio n. 3
0
 //Quote
 public async Task <int> Insert_Quote(HeaderEntity header, string createdBy, int userId, int companyId)
 {
     try
     {
         return(await headerRepository.Insert_Quote(header, createdBy, userId, companyId));
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Esempio n. 4
0
        public async Task <JsonResult> UploadFile(IFormCollection collection, int headerId)
        {
            try
            {
                string        companyCode = HttpContext.User.Claims.First(c => c.Type == "CompanyCode").Value;
                HeaderEntity  header      = dapperHelper.ExecuteSP_Single <HeaderEntity>(Header.GET_BY_ID, new { @headerId = headerId, @companyId = 0 }).Result;
                CompanyEntity buyer       = dapperHelper.ExecuteSP_Single <CompanyEntity>(SpCompany.GET_BY_ID, new { @companyId = header.BuyerCompanyId }).Result;
                string        buyerCode   = buyer.CompanyCode;


                //Azure
                // Retrieve storage account from connection string.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
                // Create the blob client.
                CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();

                // Create container. Name must be lower case.
                Console.WriteLine("Creating container...");
                CloudBlobContainer container = serviceClient.GetContainerReference(buyerCode.ToLower());
                container.CreateIfNotExistsAsync().Wait();



                List <AttachmentEntity> attachments = new List <AttachmentEntity>();

                string rootPath = Directory.GetCurrentDirectory();
                rootPath = Path.GetFullPath(Path.Combine(rootPath, @"..\..\"));

                string savePath = rootPath + "Attachments";//Ftp
                if (!System.IO.Directory.Exists(Path.Combine(savePath)))
                {
                    Directory.CreateDirectory(Path.Combine(savePath));
                }

                string savePathAzure = "attachments";//Azure

                var f = collection.Files;
                foreach (var file in f)
                {
                    AttachmentEntity attachment = new AttachmentEntity();

                    string filePath      = savePath + "\\" + companyCode + "_" + headerId;      //Ftp
                    string filePathAzure = savePathAzure + "\\" + companyCode + "_" + headerId; //Azure

                    // Get the name of the file to upload.
                    string fileName      = file.FileName; //Ftp
                    string fileNameAzure = file.FileName; //Azure

                    //// Create the path and file name to check for duplicates.
                    //string pathToCheck = filePath + "_" + fileName;
                    //// Create a temporary file name to use for checking duplicates.
                    //string tempfileName = "";
                    //// Check to see if a file already exists with the
                    //// same name as the file to upload.
                    //if (System.IO.File.Exists(pathToCheck))
                    //{
                    //    int counter = 2;
                    //    while (System.IO.File.Exists(pathToCheck))
                    //    {
                    //        // if a file with this name already exists,
                    //        // prefix the filename with a number.
                    //        tempfileName = Path.GetFileNameWithoutExtension(fileName) + '(' + counter.ToString() + ')' + Path.GetExtension(fileName);
                    //        pathToCheck = filePath + '_' + tempfileName;
                    //        counter++;
                    //    }
                    //    fileName = tempfileName;
                    //}

                    // Append the name of the file to upload to the path.
                    filePath      += "_" + fileName;      //Ftp
                    filePathAzure += "_" + fileNameAzure; //Azure

                    // Retrieve reference to a blob named "myblob".
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(filePathAzure.ToLower());

                    ////Upload file to API
                    //using (var stream = new FileStream(filePath, FileMode.Create))
                    //{
                    //    await file.CopyToAsync(stream);
                    //}

                    ////Upload file from API to Azure Storage
                    //using (var fileStream = System.IO.File.OpenRead(filePath))
                    //{
                    //    await blockBlob.UploadFromStreamAsync(fileStream);
                    //}

                    ////Delete file from API
                    //System.IO.File.Delete(filePath);

                    Stream temp;
                    temp = file.OpenReadStream();
                    await blockBlob.UploadFromStreamAsync(temp);

                    attachment.AttachmentDescription = fileName;
                    //attachment.AttachmentPath = filePath;
                    attachments.Add(attachment);
                }
                return(Json(attachments));
            }
            catch (Exception ex)
            {
                await errorServices.ErrorNotification(ex, HttpContext.User.Identity.Name, "", "");

                return(Json(ex));
            }
        }
Esempio n. 5
0
        public JsonResult DeleteFile([FromBody] List <AttachmentEntity> attachments, int headerId)
        {
            try
            {
                string companyCode = HttpContext.User.Claims.First(c => c.Type == "CompanyCode").Value;

                //Azure
                HeaderEntity  header    = dapperHelper.ExecuteSP_Single <HeaderEntity>(Header.GET_BY_ID, new { @headerId = headerId, @companyId = 0 }).Result;
                CompanyEntity buyer     = dapperHelper.ExecuteSP_Single <CompanyEntity>(SpCompany.GET_BY_ID, new { @companyId = header.BuyerCompanyId }).Result;
                string        buyerCode = buyer.CompanyCode;

                // Retrieve storage account from connection string.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
                // Create the blob client.
                CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
                // Retrieve the container.
                CloudBlobContainer container = serviceClient.GetContainerReference(buyerCode.ToLower());


                //Move deleted files to deleted folder
                string rootPath = Directory.GetCurrentDirectory();
                rootPath = Path.GetFullPath(Path.Combine(rootPath, @"..\..\"));
                string savePath        = rootPath + "Attachments";         //Ftp
                string savePathAzure   = "attachments";                    //Azure
                string deletePath      = rootPath + "Attachments_deleted"; //Ftp
                string deletePathAzure = "attachments_deleted";            //Azure
                int    filesDeleted    = 0;

                if (!System.IO.Directory.Exists(Path.Combine(savePath)))
                {
                    Directory.CreateDirectory(Path.Combine(savePath));
                }
                if (!System.IO.Directory.Exists(Path.Combine(deletePath)))
                {
                    Directory.CreateDirectory(Path.Combine(deletePath));
                }

                foreach (var item in attachments)
                {
                    if (item.Deleted)
                    {
                        try
                        {
                            string originalFilePath      = savePath + "\\" + companyCode + "_" + headerId + '_' + item.AttachmentDescription;        //Ftp
                            string deleteFilePath        = deletePath + "\\" + companyCode + "_" + headerId + '_' + item.AttachmentDescription;      //Ftp
                            string originalFilePathAzure = savePathAzure + "\\" + companyCode + "_" + headerId + '_' + item.AttachmentDescription;   //Azure
                            string deleteFilePathAzure   = deletePathAzure + "\\" + companyCode + "_" + headerId + '_' + item.AttachmentDescription; //Azure


                            //Azure
                            // Retrieve reference to a blob named "myblob".
                            CloudBlockBlob originalBlockBlob = container.GetBlockBlobReference(originalFilePathAzure.ToLower());
                            CloudBlockBlob deleteBlockBlob   = container.GetBlockBlobReference(deleteFilePathAzure.ToLower());

                            deleteBlockBlob.StartCopyAsync(originalBlockBlob);
                            originalBlockBlob.DeleteAsync();

                            //Ftp
                            //System.IO.File.Copy(originalFilePath, deleteFilePath, true);
                            //System.IO.File.Delete(originalFilePath);
                            //filesDeleted = filesDeleted++;
                        }
                        catch (Exception)
                        {//No files to delete
                        }
                    }
                }
                return(Json(filesDeleted));
            }
            catch (Exception ex)
            {
                errorServices.ErrorNotification(ex, HttpContext.User.Identity.Name, "", "").Wait();
                return(Json(ex));
            }
        }
Esempio n. 6
0
        public async Task <IActionResult> DownloadFile(int attachmentId)
        {
            try
            {
                AttachmentEntity attachment = await attachmentServices.GetAttachmentById(attachmentId);

                HeaderEntity  header    = dapperHelper.ExecuteSP_Single <HeaderEntity>(Header.GET_BY_ID, new { @headerId = attachment.HeaderId, @companyId = 0 }).Result;
                CompanyEntity buyer     = dapperHelper.ExecuteSP_Single <CompanyEntity>(SpCompany.GET_BY_ID, new { @companyId = header.BuyerCompanyId }).Result;
                string        buyerCode = buyer.CompanyCode;

                //Azure
                // Retrieve storage account from connection string.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
                // Create the blob client.
                CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
                // Retrieve the container.
                CloudBlobContainer container = serviceClient.GetContainerReference(buyerCode.ToLower());
                // Retrieve reference to a blob named "myblob".
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(attachment.AttachmentPath.Replace(buyerCode.ToUpper() + "\\", "").ToLower());

                byte[] bytes = { };
                await blockBlob.DownloadToFileAsync(Path.Combine(Path.GetTempPath(), attachment.AttachmentDescription), FileMode.Create);

                bytes = System.IO.File.ReadAllBytes(Path.Combine(Path.GetTempPath(), attachment.AttachmentDescription));
                System.IO.File.Delete(Path.Combine(Path.GetTempPath(), attachment.AttachmentDescription));

                return(new FileContentResult(bytes, "application/octet")
                {
                    FileDownloadName = attachment.AttachmentDescription
                });


                //Ftp
                //if (System.IO.File.Exists(Path.Combine(attachment.AttachmentPath)))
                //{
                //    byte[] bytes = System.IO.File.ReadAllBytes(Path.Combine(attachment.AttachmentPath));
                //    return new FileContentResult(bytes, "application/octet")
                //    {
                //        FileDownloadName = attachment.AttachmentDescription
                //    };
                //}
                //else
                //{
                //    WebClient client = new WebClient();
                //    client.Credentials = new NetworkCredential("nanotestsupplierapi\\nanoxxi93", "Azure53042");
                //    client.DownloadFile(
                //        Path.Combine("ftp://waws-prod-blu-099.ftp.azurewebsites.windows.net/", attachment.AttachmentPath.Replace("D:\\home\\", "")), Path.Combine(Path.GetTempPath(), attachment.AttachmentDescription));
                //    byte[] bytes = System.IO.File.ReadAllBytes(Path.Combine(Path.GetTempPath(), attachment.AttachmentDescription));
                //    System.IO.File.Delete(Path.Combine(Path.GetTempPath(), attachment.AttachmentDescription));
                //    return new FileContentResult(bytes, "application/octet")
                //    {
                //        FileDownloadName = attachment.AttachmentDescription
                //    };
                //}
            }
            catch (Exception ex)
            {
                await errorServices.ErrorNotification(ex, HttpContext.User.Identity.Name, "", "");

                return(Json(ex));
            }
        }