private async Task SyncAllFilesFolders() { try { //*** Set Variables int intCount = 0, intSuccess = 0, intFailed = 0; Session["SyncAllNumbers"] = ""; string strFolderGUID = ""; string strFolderpath = ""; //*** Loop on All Objects on DropBox Grid View foreach (GridViewRow itemRow in grdVWDropBoxFilesFolderList.Rows) { strFolderpath = ""; //*** Refresh Counts intCount += 1; //*** set Session Variable (Shared Variable) Session["SyncAllNumbers"] = grdVWDropBoxFilesFolderList.Rows.Count.ToString() + "," + intCount.ToString() + "," + intSuccess.ToString() + "," + intFailed.ToString(); //*** Check on Drop Box Enity if (bool.Parse(((Label)itemRow.FindControl("lblisFolder")).Text)) //**** If Folder { //************************************ //**** Create Folder on ExactOnline //************************************ //**** Construct Exact Online Class ExactOnlineConnector objExactOnlineConnector = new ExactOnlineConnector(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientId"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientSecret"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineEndPoint"], new Uri(HttpContext.Current.Session["exactOnlineReturnBackURL"].ToString()), Session["ExactOnlineReturnCode"].ToString()); if (Application["ExactOnlineAccessToken"] != null) { objExactOnlineConnector.AccessToken = Application["ExactOnlineAccessToken"].ToString(); } strFolderGUID = objExactOnlineConnector.CreateDocumentFolder(((Label)itemRow.FindControl("lblFileName")).Text, Session["CurrentExactFolderGUID"].ToString()); if (strFolderGUID == "") { //*** If Error returned intFailed += 1; } else { intSuccess += 1; } } else //**** If File { //****************************************************************** //**** Get File Stream then upload it to ExactOnline & Flush //****************************************************************** //*** Construct Parent Folder Path String string strParentFolderpath = ""; if ((List <string>)Session["FolderPath"] != null) { foreach (var item in (List <string>)Session["FolderPath"]) { strParentFolderpath += "/" + item; } } string strPath = strParentFolderpath + "/" + ((Label)itemRow.FindControl("lblFileName")).Text; strFolderpath = strPath; //*** Create Folder Function Stream fnStreamResult = await DropBoxConnector.Download(Application["dropBoxClientObj"], strPath); if (DropBoxConnector.MsgError != "") //*** If error { intFailed += 1; } else { //************************************************************* //*** Convert File to Byte Array and upload it to Exact Online //************************************************************* //**** Construct Exact Online Class ExactOnlineConnector objExactOnlineConnector = new ExactOnlineConnector(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientId"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientSecret"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineEndPoint"], new Uri(HttpContext.Current.Session["exactOnlineReturnBackURL"].ToString()), Session["ExactOnlineReturnCode"].ToString()); if (Application["ExactOnlineAccessToken"] != null) { objExactOnlineConnector.AccessToken = Application["ExactOnlineAccessToken"].ToString(); } strFolderGUID = objExactOnlineConnector.CreateDocumentWithAttachment(((Label)itemRow.FindControl("lblFileName")).Text, Session["CurrentExactFolderGUID"].ToString(), Common.ConvertStreamtoByteArr(fnStreamResult)); if (strFolderGUID == "") { intFailed += 1; } else { intSuccess += 1; } } //****************************************************************** } //*** The Add to update record into DB if (bool.Parse(((Label)itemRow.FindControl("lblisFolder")).Text)) //*** If Folder { if ((List <string>)Session["FolderPath"] != null) { foreach (var item in (List <string>)Session["FolderPath"]) { strFolderpath += "/" + item; } } strFolderpath += "/" + ((Label)itemRow.FindControl("lblFileName")).Text; } FilesDocumentsEntities objFilesDocumentsEntities = new FilesDocumentsEntities(); //*** Check First if File Already exisit into DB DropBoxExactOnline objRecord = objFilesDocumentsEntities.DropBoxExactOnlines.Where(i => i.DropBoxPath == strFolderpath).FirstOrDefault(); if (objRecord != null) { //**** Update DB objRecord.DropBoxPath = strFolderpath; objRecord.ExactOnlineGUID = strFolderGUID; if (bool.Parse(((Label)itemRow.FindControl("lblisFolder")).Text)) { objRecord.isFile = 0; } else { objRecord.isFile = 1; } objFilesDocumentsEntities.SaveChanges(); } else { //*** add to DB DropBoxExactOnline objRecordNew = new DropBoxExactOnline(); objRecordNew.DropBoxPath = strFolderpath; objRecordNew.ExactOnlineGUID = strFolderGUID; if (bool.Parse(((Label)itemRow.FindControl("lblisFolder")).Text)) { objRecordNew.isFile = 0; } else { objRecordNew.isFile = 1; } objFilesDocumentsEntities.DropBoxExactOnlines.Add(objRecordNew); objFilesDocumentsEntities.SaveChanges(); } //******************************************************************************* //*** set Session Variable (Shared Variable) Session["SyncAllNumbers"] = grdVWDropBoxFilesFolderList.Rows.Count.ToString() + "," + intCount.ToString() + "," + intSuccess.ToString() + "," + intFailed.ToString(); } //*** Rebind Exact Online Grid ExactOnlineGridDataBind(Session["CurrentExactFolderGUID"].ToString()); } catch (Exception e) { lblExactOnlineMsg.Text = e.ToString(); //*** Show Error divExactOnlineAlert.Visible = true; } }
//*** Download Button protected async void lnkbtnDownload_Click(object sender, ImageClickEventArgs e) { //*** Construct Parent Folder Path String string strParentFolderpath = ""; if ((List <string>)Session["FolderPath"] != null) { foreach (var item in (List <string>)Session["FolderPath"]) { strParentFolderpath += "/" + item; } } //*** Loop on Items to see file is checked to download foreach (GridViewRow row in grdVWFilesFolderList.Rows) { if (((CheckBox)row.FindControl("chkItem")).Checked) { //*** Check if Selected is Folder if (bool.Parse(((Label)row.FindControl("lblisFolder")).Text)) { //*** Folder Already Exist lblDropBoxMsg.Text = "Only Files can be downloaded"; //*** Show Error with grid divDropBoxAlert.Visible = true; divFileGrid.Visible = true; //*** Exit from function return; } else { //*** Checked is Files string strPath = strParentFolderpath + "/" + ((Label)row.FindControl("lblFileName")).Text; //*** Create Folder Function Stream fnStreamResult = await DropBoxConnector.Download(Application["dropBoxClientObj"], strPath); if (DropBoxConnector.MsgError != "") //*** If error { lblDropBoxMsg.Text = DropBoxConnector.MsgError; //*** Show Error divDropBoxAlert.Visible = true; //*** Exit from function return; } else { //*** Save file to tmp Folder and direct user to it // Create a FileStream object to write a stream to a file using (var fileStream = new FileStream(HttpContext.Current.Request.PhysicalApplicationPath + System.Configuration.ConfigurationManager.AppSettings["downloadFolderRelPath"] + ((Label)row.FindControl("lblFileName")).Text, FileMode.Create, FileAccess.Write)) { fnStreamResult.CopyTo(fileStream); } //*** Then redirect to that file to download it DownloadFilePath = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host; if (HttpContext.Current.Request.Url.Port > 0) { DownloadFilePath += ":" + HttpContext.Current.Request.Url.Port.ToString(); } ; DownloadFilePath += "/" + System.Configuration.ConfigurationManager.AppSettings["downloadFolderRelPath"].Replace("\\", "/") + ((Label)row.FindControl("lblFileName")).Text; divFileGrid.Visible = true; pnlDownload.Visible = true; //*** Exit from function return; } } } } }
//***************************************************************** //*** Major Sync Function with Exact online store //***************************************************************** /// <summary> /// *** Sync Function which Sync all Modified files with Exact Online Store /// </summary> /// <param name="lstDropBoxFile">Dropbox files in List Format</param> /// <returns></returns> private async Task SyncAllFilesFolders(List <DropBoxFile> lstDropBoxFile) { //System.Threading.Thread.Sleep(30000); try { //*** Set Variables int intCount = 0, intSuccess = 0, intFailed = 0; Session["SyncAllNumbers"] = ""; string strExactFileGUID = ""; string strFolderpath = ""; string strParentFolderpath = ""; CloudStorageEntities objCloudStorageEntities = new CloudStorageEntities(); //**** Construct Exact Online Class ExactOnlineConnector objExactOnlineConnector = new ExactOnlineConnector(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientId"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientSecret"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineEndPoint"], new Uri(Session["exactOnlineReturnBackURL"].ToString()), Session["ExactOnlineReturnCode"].ToString()); //*** First Reset All "FileStillAlive" Flag into DB (from p in objCloudStorageEntities.DropBoxExactOnlines where p.Id >= 0 select p).ToList().ForEach(x => x.FileStillAlive = 0); objCloudStorageEntities.SaveChanges(); //*** Loop on All Objects on DropBox Grid View foreach (var DropBoxFile in lstDropBoxFile) { strFolderpath = ""; //*** Refresh Counts intCount += 1; //**** Check If File item exist into DB with same Modified Date or not //*** 1. Exist with Same Modified Date, Do Nothing //*** 2. Not Exist, So Add File to Exact Online and DB //*** 3. Exist with Different Modified Date, So Update File into Exact Online and Modified Date into DB DropBoxExactOnline objRecord = objCloudStorageEntities.DropBoxExactOnlines.Where(i => i.DropBoxPath == DropBoxFile.FileName).FirstOrDefault(); if (objRecord == null || (objRecord != null && DropBoxFile.ModificationDate != objRecord.DropBoxFileModifiedDate)) //*** Not Exist Or File Exist with Different Modification Date { //******************************************************************** //*** Add File to Exact Online //******************************************************************** //**** Get File Stream then upload it to ExactOnline & Flush //*** Construct Parent Folder Path String strParentFolderpath = ""; if ((List <string>)Session["FolderPath"] != null) { foreach (var item in (List <string>)Session["FolderPath"]) { strParentFolderpath += "/" + item; } } string strPath = strParentFolderpath + "/" + DropBoxFile.FileName; strFolderpath = strPath; //*** Create Folder Function Stream fnStreamResult = await DropBoxConnector.Download(Session["dropBoxClientObj"], strPath); if (DropBoxConnector.MsgError != "") //*** If error { intFailed += 1; } else { //************************************************************* //*** Convert File to Byte Array and upload it to Exact Online //************************************************************* if (Session["ExactOnlineAccessToken"] != null) { objExactOnlineConnector.AccessToken = Session["ExactOnlineAccessToken"].ToString(); } //**** Get Document Folder GUID Session["CurrentExactFolderGUID"] = string.Empty; //*** Root Folder //*** If File already exisit then Delete it first if (objRecord != null && DropBoxFile.ModificationDate != objRecord.DropBoxFileModifiedDate) { objExactOnlineConnector.DeleteDocument(objRecord.ExactOnlineGUID); //**** Call Delete Document } strExactFileGUID = objExactOnlineConnector.CreateDocumentWithAttachment(DropBoxFile.FileName, Session["CurrentExactFolderGUID"].ToString(), Common.ConvertStreamtoByteArr(fnStreamResult)); if (strExactFileGUID == "") { intFailed += 1; } else { intSuccess += 1; } } //****************************************************************** if (objRecord == null) { //*** add to DB DropBoxExactOnline objRecordNew = new DropBoxExactOnline(); objRecordNew.DropBoxPath = DropBoxFile.FileName; objRecordNew.DropBoxFileModifiedDate = DropBoxFile.ModificationDate; objRecordNew.ExactOnlineGUID = strExactFileGUID; objRecordNew.isFile = 1; objRecordNew.FileStillAlive = 1; objCloudStorageEntities.DropBoxExactOnlines.Add(objRecordNew); objCloudStorageEntities.SaveChanges(); } else { //**** Update DB objRecord.DropBoxFileModifiedDate = DropBoxFile.ModificationDate; objRecord.ExactOnlineGUID = strExactFileGUID; objRecord.FileStillAlive = 1; objCloudStorageEntities.SaveChanges(); } } //*** If File still exit and not changed if (objRecord != null && DropBoxFile.ModificationDate == objRecord.DropBoxFileModifiedDate) { objRecord.FileStillAlive = 1; objCloudStorageEntities.SaveChanges(); } //*** set Session Variable (Shared Variable) Session["SyncAllNumbers"] = lstDropBoxFile.Count.ToString() + "," + intCount.ToString() + "," + intSuccess.ToString() + "," + intFailed.ToString(); } //*** For Loop //*************************************************************************************** //*** Then Check For Not Alive Files to Delete from DB and Exact Online //*************************************************************************************** List <DropBoxExactOnline> lstFiles = objCloudStorageEntities.DropBoxExactOnlines.Where(item => item.FileStillAlive == 0).ToList(); foreach (var file in lstFiles) { //**************************************************************** //**** Delete File from Exact Online //**************************************************************** if (file.ExactOnlineGUID != "") { //*** Delete File on Exact Online also if (Session["ExactOnlineAccessToken"] != null) { objExactOnlineConnector.AccessToken = Session["ExactOnlineAccessToken"].ToString(); } //**** Call Delete Document objExactOnlineConnector.DeleteDocument(file.ExactOnlineGUID); } //*************************************************************************** //*** Delete From DB objCloudStorageEntities.DropBoxExactOnlines.Remove(file); } //*** For Loop //*** Submit Delete from DB objCloudStorageEntities.SaveChanges(); //*************************************************************************************** } catch (Exception e) { Session["lblDropBoxMsg"] = "SyncAllFilesFolders Error: " + e.ToString(); } }