/// <summary> /// Upload to a temporary file. /// If the transfer is successful, replace the old file with the temporary one. /// If not, delete the temporary file. /// </summary> /// <param name="i">The item to upload</param> /// <returns>TransferStatus.Success on success, TransferStatus.Success on failure</returns> public TransferStatus SafeUpload(SyncQueueItem i) { Notifications.ChangeTrayText(MessageType.Uploading, i.Item.Name); string temp = Common._tempName(i.CommonPath); try { var _startedOn = DateTime.Now; long _transfered = 0; // upload to a temp file... if (FTP) { EventHandler <TransferProgressEventArgs> action = (o, e) => { _transfered += e.BytesTransferred; ReportTransferProgress(new TransferProgressArgs(e.BytesTransferred, _transfered, i, _startedOn)); }; _ftpc.TransferProgress += action; if (i.PathToFile.PathHasSpace()) { string cd = WorkingDirectory; _ftpc.ChangeDirectoryMultiPath(i.PathToFile); _ftpc.PutFile(i.LocalPath, Common._name(temp), FileAction.Create); while (WorkingDirectory != cd) { _ftpc.ChangeDirectoryMultiPath(".."); } } else { _ftpc.PutFile(i.LocalPath, temp, FileAction.Create); } // Unsubscribe _ftpc.TransferProgress -= action; } else { using (var file = File.OpenRead(i.LocalPath)) _sftpc.UploadFile(file, temp, true, (d) => { ReportTransferProgress(new TransferProgressArgs((long)d - _transfered, (long)d, i, _startedOn)); _transfered = (long)d; }); } } catch (Exception ex) { Common.LogError(ex); if (FTP) { CheckWorkingDirectory(); } return(TransferStatus.Failure); } if (i.Item.Size == SizeOf(temp)) { if (Exists(i.CommonPath)) { Remove(i.CommonPath); } Rename(temp, i.CommonPath); return(TransferStatus.Success); } else { Remove(temp); return(TransferStatus.Failure); } }