public void SendBackupDirectory(string directory, string remoteDirectory, DbOperations db, int devicePlanId, IPlatform platform, BackupLog bc) { string[] deviceInfo = Common.SecurityKey().Split('|'); BackupJobModel model = new BackupJobModel(); var result = Common.ApiAndSecretKey(); model.apiAccessKey = result.Key; model.apiSecretKey = result.Value; model.cpuId = deviceInfo[0]; model.diskVolume = deviceInfo[1]; model.macAddress = deviceInfo[2]; bc.apiAccessKey = model.apiAccessKey; bc.apiSecretKey = model.apiSecretKey; bc.cpuId = model.cpuId; bc.diskVolume = model.diskVolume; bc.macAddress = model.macAddress; List <CloudFile> cfList = db.GetFileList(devicePlanId, directory); DirectoryInfo d = new DirectoryInfo(directory); //Assuming Test is your Folder FileInfo[] Files = d.GetFiles(); //Getting Text files string str = ""; foreach (FileInfo file in Files) { CloudFile currentFile = cfList.Where(f => f.FullName == file.FullName && f.ResultStatus == Enum.ResultStatus.Success).FirstOrDefault(); if (currentFile != null) { if (file.LastWriteTime > currentFile.ProccessDate.Value && !String.IsNullOrEmpty(currentFile.CloudId)) { if (platform.CheckAccessTokenExpire()) { TokenResponse resp = ResetGoogleToken(devicePlanId); platform.UpdateAccessToken(resp.AccessToken, DateTime.Now.AddSeconds(resp.ExpiresInSeconds.Value)); } string fileId = platform.UpdateFile(file.Name, file.FullName, file.GetType().ToString(), remoteDirectory, currentFile.CloudId);// UpdateFile(file.Name, file.FullName, file.GetType().ToString(), accessToken, s, remoteDirectory, currentFile.CloudId); if (!String.IsNullOrEmpty(fileId) && !fileId.StartsWith("error-")) { currentFile.CloudId = fileId; bc.ProccessCount++; bc.UpdatedCount++; bc.TotalSize += GetFileSize(file); } if (!String.IsNullOrEmpty(fileId) && fileId.StartsWith("error-")) { currentFile.ErrorMessage = fileId.Replace("error-", ""); bc.FailedCount++; bc.ProccessCount++; } currentFile.Length = GetFileSize(file); currentFile.ProccessDate = DateTime.Now; if (!String.IsNullOrEmpty(fileId) && !fileId.StartsWith("error-")) { currentFile.ResultStatus = Enum.ResultStatus.Success; } else { currentFile.ResultStatus = Enum.ResultStatus.Error; } db.UpdateData(currentFile); } } else { if (platform.CheckAccessTokenExpire()) { TokenResponse resp = ResetGoogleToken(devicePlanId); platform.UpdateAccessToken(resp.AccessToken, DateTime.Now.AddSeconds(resp.ExpiresInSeconds.Value)); bc.ProccessCount++; } string fileId = platform.UploadFile(file.Name, file.FullName, file.GetType().ToString(), remoteDirectory); CloudFile ff = new CloudFile(); if (!String.IsNullOrEmpty(fileId) && !fileId.StartsWith("error-")) { ff.CloudId = fileId; } ff.DevicePlanId = devicePlanId; if (!String.IsNullOrEmpty(fileId) && fileId.StartsWith("error-")) { ff.ErrorMessage = fileId.Replace("error-", ""); } ff.Length = GetFileSize(file); ff.FullName = file.FullName; ff.ProccessDate = DateTime.Now; if (!String.IsNullOrEmpty(fileId) && !fileId.StartsWith("error-")) { ff.ResultStatus = Enum.ResultStatus.Success; bc.ProccessCount++; bc.CreateFileCount++; bc.TotalSize += GetFileSize(file); } else { ff.ResultStatus = Enum.ResultStatus.Error; bc.FailedCount++; bc.ProccessCount++; } ff.SubDirectory = file.DirectoryName; ff.Type = Enum.FileType.File; db.InsertData(ff); } } List <string> fileFullPaths = Files.Select(f => f.FullName).ToList(); foreach (var deleteFile in cfList.Where(f => !fileFullPaths.Contains(f.FullName) && f.Type == Enum.FileType.File && f.ResultStatus == Enum.ResultStatus.Success)) { if (!String.IsNullOrEmpty(deleteFile.CloudId)) { if (platform.CheckAccessTokenExpire()) { bc.ProccessCount++; TokenResponse resp = ResetGoogleToken(devicePlanId); platform.UpdateAccessToken(resp.AccessToken, DateTime.Now.AddSeconds(resp.ExpiresInSeconds.Value)); } platform.DeleteFile(deleteFile.CloudId); bc.ProccessCount++; bc.DeletedCount++; deleteFile.ProccessDate = DateTime.Now; db.DeleteData(deleteFile); } } foreach (var item in Directory.GetDirectories(directory)) { CloudFile currentFile = cfList.Where(f => f.SubDirectory + @"\" + f.FullName == item && f.ResultStatus == Enum.ResultStatus.Success).FirstOrDefault(); if (currentFile != null && currentFile.SubDirectory + @"\" + currentFile.FullName == item) { SendBackupDirectory(item, currentFile.CloudId, db, devicePlanId, platform, bc); } else { if (platform.CheckAccessTokenExpire()) { TokenResponse resp = ResetGoogleToken(devicePlanId); platform.UpdateAccessToken(resp.AccessToken, DateTime.Now.AddSeconds(resp.ExpiresInSeconds.Value)); bc.ProccessCount++; } DirectoryInfo df = new DirectoryInfo(item); string directoryId = platform.CreateDirectory(df.Name, remoteDirectory); CloudFile ff = new CloudFile(); if (!String.IsNullOrEmpty(directoryId) && !directoryId.StartsWith("error-")) { ff.CloudId = directoryId; } ff.DevicePlanId = devicePlanId; if (!String.IsNullOrEmpty(directoryId) && directoryId.StartsWith("error-")) { ff.ErrorMessage = directoryId.Replace("error-", ""); } ff.FullName = df.Name; ff.ProccessDate = DateTime.Now; if (!String.IsNullOrEmpty(directoryId) && !directoryId.StartsWith("error-")) { ff.ResultStatus = Enum.ResultStatus.Success; bc.CreateDirectoryCount++; bc.ProccessCount++; } else { ff.ResultStatus = Enum.ResultStatus.Error; bc.FailedCount++; bc.ProccessCount++; } ff.SubDirectory = directory; ff.Type = Enum.FileType.Directory; db.InsertData(ff); SendBackupDirectory(item, directoryId, db, devicePlanId, platform, bc); } } }
public void SendFile(string model) { BackupLog bc = new BackupLog(); string localDirectory = ""; string remoteDirectory = ""; string devicePlanId = ""; dynamic platformDetail = JObject.Parse(model); localDirectory = platformDetail.localDirectory; remoteDirectory = platformDetail.remoteDirectory; devicePlanId = platformDetail.devicePlanId; bc.DevicePlanId = Convert.ToInt32(devicePlanId); bc.CreateDirectoryCount = 0; bc.CreateFileCount = 0; bc.FailedCount = 0; bc.ProccessCount = 0; bc.TotalSize = 0; bc.DeletedCount = 0; bc.UpdatedCount = 0; DbOperations db = new DbOperations(); IPlatform platf = null; if (platformDetail.planType.ToString() == "1") { DriveService s = new Google.Apis.Drive.v3.DriveService(); var services = new DriveService(new BaseClientService.Initializer() { ApiKey = platformDetail.googleApiCode, // from https://console.developers.google.com (Public API access) ApplicationName = "ECloud Backup", }); platf = new PlatformGoogle(s, platformDetail.googleAccessToken.ToString(), platformDetail.googleApiCode.ToString(), Common.ConvertDatabaseDateTime(platformDetail.googleTokenExpired.ToString())); } else if (platformDetail.planType.ToString() == "2") { platf = new AmazonS3(platformDetail.apiAccessKey.ToString(), platformDetail.apiSecretKey.ToString(), platformDetail.region.ToString()); } DateTime dtStartBackup = DateTime.Now; SendBackupDirectory(localDirectory, remoteDirectory, db, Convert.ToInt32(devicePlanId), platf, bc); var list = db.GetErrorFileList(Convert.ToInt32(devicePlanId), dtStartBackup); List <string> errorList = new List <string>(); foreach (var k in list) { errorList.Add(k.FullName + " - " + k.ErrorMessage); } LogDeviceModel mdl = new LogDeviceModel { apiAccessKey = bc.apiAccessKey, apiSecretKey = bc.apiSecretKey, cpuId = bc.cpuId, deviceId = bc.deviceId, devicePlanId = Convert.ToInt32(devicePlanId), diskVolume = bc.diskVolume, errorList = errorList, macAddress = bc.macAddress }; IRestRequest request = new RestRequest("api/device/deviceplanid/{deviceplanid}/finish", Method.POST); request.RequestFormat = RestSharp.DataFormat.Json; request.AddParameter("deviceplanid", devicePlanId, ParameterType.UrlSegment); request.AddBody(bc); TaskCompletionSource <IRestResponse> taskCompletion = new TaskCompletionSource <IRestResponse>(); RestRequestAsyncHandle handle = client.ExecuteAsync(request, r => { taskCompletion.SetResult(r); }); IRestResponse response = taskCompletion.Task.Result; IRestRequest request1 = new RestRequest("api/device/log", Method.POST); request1.RequestFormat = RestSharp.DataFormat.Json; request1.AddBody(mdl); TaskCompletionSource <IRestResponse> taskCompletion1 = new TaskCompletionSource <IRestResponse>(); RestRequestAsyncHandle handle1 = client.ExecuteAsync(request1, r => { taskCompletion1.SetResult(r); }); IRestResponse response1 = taskCompletion1.Task.Result; }