public override void SetModifiedTime(SyncQueueItem i, DateTime time) { var attr = _sftpc.GetAttributes(i.CommonPath); attr.LastWriteTime = time; _sftpc.SetAttributes(i.CommonPath, attr); }
public override void SetModifiedTime(SyncQueueItem i, DateTime time) { string command; var reply = new FtpReply(); var timeFormatted = time.ToString("yyyyMMddHHMMss"); if (_ftpc.Capabilities.HasFlag(FtpCapability.MFF)) { command = string.Format("MFF Modify={0}; {1}", timeFormatted, i.CommonPath); reply = _ftpc.Execute(command); } if (!reply.Success && _ftpc.Capabilities.HasFlag(FtpCapability.MFMT)) { command = string.Format("MFMT {0} {1}", timeFormatted, i.CommonPath); reply = _ftpc.Execute(command); } if (!reply.Success) { command = string.Format("SITE UTIME {0} {1}", timeFormatted, i.CommonPath); reply = _ftpc.Execute(command); } if (!reply.Success) { Log.Write(l.Error, "SetModTime failed, file: {0} msg: {1}", i.CommonPath, reply.ErrorMessage); } }
/// <summary> /// TransferProgressArgs constructor. /// </summary> /// <param name="transfered">bytes transferred</param> /// <param name="total">total bytes transferred</param> /// <param name="item">the transferred item</param> /// <param name="started">when the transfer started</param> public TransferProgressArgs(long transferred, long total, SyncQueueItem item, DateTime started) { Transfered = transferred; TotalTransferred = total; Item = item; StartedOn = started; }
/// <summary> /// Download 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 download</param> /// <returns>TransferStatus.Success on success, TransferStatus.Success on failure</returns> public async Task <TransferStatus> SafeDownload(SyncQueueItem i) { Notifications.ChangeTrayText(MessageType.Downloading, i.Item.Name); var temp = Path.GetTempFileName(); try { // download to a temp file... using (var file = File.OpenWrite(temp)) { await Download(i, file, TransferProgress); } if (Controller.TransferValidator.Validate(temp, i.Item)) { Controller.FolderWatcher.Pause(); Common.RecycleOrDeleteFile(i.LocalPath); File.Move(temp, i.LocalPath); Controller.FolderWatcher.Resume(); return(TransferStatus.Success); } } catch (Exception ex) { ex.LogException(); CheckWorkingDirectory(); } Common.RecycleOrDeleteFile(i.LocalPath); return(TransferStatus.Failure); }
public async Task <TransferStatus> SafeUpload(SyncQueueItem i) { Notifications.ChangeTrayText(MessageType.Uploading, i.Item.Name); var temp = Common._tempName(i.CommonPath, Controller.Account.TempFilePrefix); try { // upload to a temp file... using (var file = new FileStream(i.LocalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { await Upload(i, file, temp, TransferProgress); } } catch (Exception ex) { ex.LogException(); CheckWorkingDirectory(); return(TransferStatus.Failure); } if (Controller.TransferValidator.Validate(i.Item, temp)) { await Rename(temp, i.CommonPath); return(TransferStatus.Success); } await Remove(temp); return(TransferStatus.Failure); }
/// <summary> /// Creates the SyncQueueItem from the given data and adds it to the sync queue /// </summary> private void AddToQueue(FileSystemEventArgs e, ChangeAction action) { var isFile = Common.PathIsFile(e.FullPath); // ignore directory changes if (!isFile && action == ChangeAction.changed) { return; } var queueItem = new SyncQueueItem(controller) { Item = new ClientItem { Name = e.Name, FullPath = e.FullPath, Type = isFile ? ClientItemType.File : ClientItemType.Folder, Size = (isFile && action != ChangeAction.deleted) ? new FileInfo(e.FullPath).Length : 0x0, LastWriteTime = File.GetLastWriteTime(e.FullPath) }, SyncTo = SyncTo.Remote, ActionType = action }; if (action == ChangeAction.renamed) { var args = e as RenamedEventArgs; queueItem.Item.FullPath = args.OldFullPath; queueItem.Item.NewFullPath = args.FullPath; } // Send to the sync queue controller.SyncQueue.Add(queueItem); }
/// <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 override TransferStatus SafeUpload(SyncQueueItem i) { // is this the first time we check the files? //if (_controller.FileLog.IsEmpty()) //{ //TODO: allow user to select if the following should happen // skip synchronization if the file already exists and has the exact same size if (Exists(i.CommonPath) && SizeOf(i.CommonPath) == i.Item.Size) { Log.Write(l.Client, "File seems to be already synced (skipping): {0}", i.CommonPath); return(TransferStatus.Success); } //} Notifications.ChangeTrayText(MessageType.Uploading, i.Item.Name); var temp = Common._tempName(i.CommonPath, _controller.Account.TempFilePrefix); try { var startedOn = DateTime.Now; long transfered = 0; // upload to a temp file... using (var file = File.Open(i.LocalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) lock (ftpcLock) { _sftpc.UploadFile(file, temp, true, d => { ReportTransferProgress(new TransferProgressArgs((long)d - transfered, (long)d, i, startedOn)); transfered = (long)d; }); Notifications.ChangeTrayText(MessageType.Size, null, i.Item.Size); } } catch (Exception ex) { Common.LogError(ex); return(TransferStatus.Failure); } Thread.Sleep(2000); //wait syncing long size = SizeOf(temp); if (i.Item.Size == size) { if (Exists(i.CommonPath)) { Remove(i.CommonPath); } Rename(temp, i.CommonPath); return(TransferStatus.Success); } Remove(temp); return(TransferStatus.Failure); }
public virtual bool TryValidate(SyncQueueItem item, string remote) { if (Controller.Client.Exists(item.CommonPath)) { return(Validate(item.Item, item.CommonPath)); } else { return(false); } }
/// <summary> /// Download 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 download</param> /// <returns>TransferStatus.Success on success, TransferStatus.Success on failure</returns> public override TransferStatus SafeDownload(SyncQueueItem i) { Notifications.ChangeTrayText(MessageType.Downloading, i.Item.Name); var temp = Common._tempLocal(i.LocalPath, _controller.Account.TempFilePrefix); try { var startedOn = DateTime.Now; long transfered = 0; // download to a temp file... using (var f = new FileStream(temp, FileMode.Create, FileAccess.ReadWrite)) lock (ftpcLock) { _sftpc.DownloadFile(i.CommonPath, f, d => { ReportTransferProgress(new TransferProgressArgs((long)d - transfered, (long)d, i, startedOn)); transfered = (long)d; }); } } catch (Exception ex) { Common.LogError(ex); goto Finish; } if (i.Item.Size == new FileInfo(temp).Length) { _controller.FolderWatcher.Pause(); // Pause Watchers if (File.Exists(i.LocalPath)) #if __MonoCs__ { File.Delete(i.LocalPath); } #else { FileIO.FileSystem.DeleteFile(i.LocalPath, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); } #endif File.Move(temp, i.LocalPath); _controller.FolderWatcher.Resume(); // Resume Watchers return(TransferStatus.Success); } Finish: if (File.Exists(temp)) #if __MonoCs__ { File.Delete(temp); } #else { FileIO.FileSystem.DeleteFile(temp, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); } #endif return(TransferStatus.Failure); }
public override async Task Upload(SyncQueueItem i, Stream uploadStream, string path, IProgress <TransferProgress> progress) { var startedOn = DateTime.Now; long transfered = 0; await _sftpc.UploadAsync(uploadStream, path, d => { progress.Report(new TransferProgress((long)d, i, startedOn)); transfered = (long)d; ThrottleTransfer(Settings.General.UploadLimit, transfered, startedOn); }); }
public async Task <TransferStatus> SafeUpload(SyncQueueItem i) { // is this the first time we check the files? // TODO: this only works for top level files rn if (Controller.FileLog.IsEmpty()) { // TODO: allow user to select if the following should happen // skip synchronization if the file already exists and has the exact same size if (Exists(i.CommonPath) && SizeOf(i.CommonPath) == i.Item.Size) { Log.Write(l.Client, "File seems to be already synced (skipping): {0}", i.CommonPath); return(TransferStatus.Success); } } Notifications.ChangeTrayText(MessageType.Uploading, i.Item.Name); var temp = Common._tempName(i.CommonPath, Controller.Account.TempFilePrefix); try { // upload to a temp file... using (var file = new FileStream(i.LocalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { await Upload(i, file, temp, TransferProgress); } } catch (Exception ex) { ex.LogException(); CheckWorkingDirectory(); return(TransferStatus.Failure); } if (Controller.TransferValidator.Validate(i.Item, temp)) { if (Exists(i.CommonPath)) { Log.Write(l.Debug, $"Replacing remote file: [{i.CommonPath}]"); await Remove(i.CommonPath); } await Rename(temp, i.CommonPath); return(TransferStatus.Success); } await Remove(temp); return(TransferStatus.Failure); }
/// <summary> /// Puts the specified file in the File Log and saves to the config file /// </summary> public void PutFile(SyncQueueItem file) { Log.Write(l.Debug, "Putting file {0} to log", file.NewCommonPath); if (Contains(file.NewCommonPath)) Remove(file.NewCommonPath); Files.Add(new FileLogItem { CommonPath = file.NewCommonPath, Local = file.SyncTo == SyncTo.Remote ? file.Item.LastWriteTime : File.GetLastWriteTime(file.LocalPath), Remote = _controller.Client.GetLwtOf(file.NewCommonPath) }); Settings.SaveProfile(); Notifications.ChangeRecentList(); }
/// <summary> /// Puts the specified file in the File Log and saves to the config file /// </summary> public void PutFile(SyncQueueItem file) { Log.Write(l.Debug, "Putting file {0} to log", file.NewCommonPath); if (Contains(file.NewCommonPath)) { Remove(file.NewCommonPath); } Files.Add(new FileLogItem { CommonPath = file.NewCommonPath, Local = file.SyncTo == SyncTo.Remote ? file.Item.LastWriteTime : File.GetLastWriteTime(file.LocalPath), Remote = _controller.Client.TryGetModifiedTime(file.NewCommonPath) }); Settings.SaveProfile(); Notifications.ChangeRecentList(); }
/// <summary> /// Puts the specified file in the File Log and saves to the config file /// </summary> public void putFile(SyncQueueItem file) { Log.Write(l.Debug, "Putting file {0} to log", file.NewCommonPath); if (Contains(file.NewCommonPath)) { Remove(file.NewCommonPath); } Files.Add(new FileLogItem { CommonPath = file.NewCommonPath, Local = file.SyncTo == SyncTo.Remote ? file.Item.LastWriteTime : System.IO.File.GetLastWriteTime(file.LocalPath), Remote = controller.Client.GetLwtOf(file.NewCommonPath) }); FileLogChanged.SafeInvoke(null, EventArgs.Empty); Settings.SaveProfile(); }
public override void SetFilePermissions(SyncQueueItem i, short mode) { string command; var reply = new FtpReply(); if (_ftpc.Capabilities.HasFlag(FtpCapability.MFF)) { command = string.Format("MFF UNIX.mode={0}; {1}", mode, i.CommonPath); reply = _ftpc.Execute(command); } if (!reply.Success) { command = string.Format("SITE CHMOD {0} {1}", mode, i.CommonPath); reply = _ftpc.Execute(command); } if (!reply.Success) { Log.Write(l.Error, "chmod failed, file: {0} msg: {1}", i.CommonPath, reply.ErrorMessage); } }
public override async Task Upload(SyncQueueItem i, Stream uploadStream, string path, IProgress <TransferProgress> progress) { using (var s = await _ftpc.OpenWriteAsync(path)) { var startedOn = DateTime.Now; long transfered = 0; var buf = new byte[8192]; int read; while ((read = await uploadStream.ReadAsync(buf, 0, buf.Length)) > 0) { await s.WriteAsync(buf, 0, read); transfered += read; progress.Report(new TransferProgress(transfered, i, startedOn)); ThrottleTransfer(Settings.General.UploadLimit, transfered, startedOn); } } }
public override async Task Download(SyncQueueItem i, Stream fileStream, IProgress <TransferProgress> progress) { var path = Controller.AbsolutePath(i.CommonPath); using (var s = await _ftpc.OpenReadAsync(path)) { var startedOn = DateTime.Now; long transfered = 0; var buf = new byte[8192]; int read; while ((read = s.Read(buf, 0, buf.Length)) > 0) { await fileStream.WriteAsync(buf, 0, read); transfered += read; progress.Report(new TransferProgress(transfered, i, startedOn)); ThrottleTransfer(Settings.General.DownloadLimit, transfered, startedOn); } } }
/// <summary> /// Download 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 download</param> /// <returns>TransferStatus.Success on success, TransferStatus.Success on failure</returns> public TransferStatus SafeDownload(SyncQueueItem i) { Notifications.ChangeTrayText(MessageType.Downloading, i.Item.Name); var temp = Common._tempLocal(i.LocalPath, _controller.Account.TempFilePrefix); try { var startedOn = DateTime.Now; long transfered = 0; // download to a temp file... if (FTP) { const int bufferSize = 8192; var canResume = _ftpc.Capabilities.HasFlag(FtpCapability.REST); // Delete file if resume is not supported if (!canResume && File.Exists(temp)) { Log.Write(l.Client, "Temporary file {0} found, but resume not supported. Deleting...", Path.GetFileName(i.CommonPath)); File.Delete(temp); } using (var file = new StreamWriter(temp, true, new System.Text.UTF8Encoding(false, true), bufferSize)) using (var rem = _ftpc.OpenRead(i.CommonPath, file.BaseStream.Position)) { file.AutoFlush = true; if (file.BaseStream.Position > 0) { var fileInfo = new FileInfo(temp); Log.Write(l.Client, "Resuming download for file {0} at position {1}({2})", Path.GetFileName(i.CommonPath), file.BaseStream.Position, fileInfo.Length); } var buf = new byte[bufferSize]; int read; while ((read = rem.Read(buf, 0, buf.Length)) > 0) { file.BaseStream.Write(buf, 0, read); transfered += read; ReportTransferProgress(new TransferProgressArgs(read, transfered, i, startedOn)); ThrottleTransfer(Settings.General.DownloadLimit, transfered, startedOn); } } } else { using (var f = new FileStream(temp, FileMode.Create, FileAccess.ReadWrite)) _sftpc.DownloadFile(i.CommonPath, f, d => { ReportTransferProgress(new TransferProgressArgs((long)d - transfered, (long)d, i, startedOn)); transfered = (long)d; }); } } catch (Exception ex) { Common.LogError(ex); if (FTP) { CheckWorkingDirectory(); // Don't delete if timeout and resume is supported if (ex as System.TimeoutException != null && _ftpc.Capabilities.HasFlag(FtpCapability.REST)) { goto Return; } } goto Finish; } var tempFileInfo = new FileInfo(temp); if (i.Item.Size == tempFileInfo.Length) { _controller.FolderWatcher.Pause(); // Pause Watchers if (File.Exists(i.LocalPath)) #if __MonoCs__ { File.Delete(i.LocalPath); } #else { FileIO.FileSystem.DeleteFile(i.LocalPath, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); } #endif File.Move(temp, i.LocalPath); _controller.FolderWatcher.Resume(); // Resume Watchers return(TransferStatus.Success); } else { Log.Write(l.Client, "[Error] Size mismatch! {0}({1}) <> {2}({3}), deleting temp file...", i.Item.Name, i.Item.Size, tempFileInfo.Name, tempFileInfo.Length); } Finish: if (File.Exists(temp)) #if __MonoCs__ { File.Delete(temp); } #else { FileIO.FileSystem.DeleteFile(temp, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); } #endif Return: return(TransferStatus.Failure); }
/// <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) { using (Stream file = File.OpenRead(i.LocalPath), rem = _ftpc.OpenWrite(temp)) { var buf = new byte[8192]; int read; while ((read = file.Read(buf, 0, buf.Length)) > 0) { rem.Write(buf, 0, read); _transfered += read; ReportTransferProgress(new TransferProgressArgs(read, _transfered, i, _startedOn)); ThrottleTransfer(Settings.General.UploadLimit, _transfered, _startedOn); } } } 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; } }
public abstract Task Upload(SyncQueueItem i, Stream uploadStream, string path, IProgress <TransferProgress> progress);
public virtual TransferStatus SafeUpload(SyncQueueItem item) { throw new NotImplementedException(); }
/// <summary> /// Download 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 download</param> /// <returns>TransferStatus.Success on success, TransferStatus.Success on failure</returns> public TransferStatus SafeDownload(SyncQueueItem i) { Notifications.ChangeTrayText(MessageType.Downloading, i.Item.Name); string temp = Common._tempLocal(i.LocalPath); try { var _startedOn = DateTime.Now; long _transfered = 0; // download to a temp file... if (FTP) { using (Stream file = File.OpenWrite(temp), rem = _ftpc.OpenRead(i.CommonPath)) { var buf = new byte[8192]; int read; while ((read = rem.Read(buf, 0, buf.Length)) > 0) { file.Write(buf, 0, read); _transfered += read; ReportTransferProgress(new TransferProgressArgs(read, _transfered, i, _startedOn)); ThrottleTransfer(Settings.General.DownloadLimit, _transfered, _startedOn); } } } else using (var f = new FileStream(temp, FileMode.Create, FileAccess.ReadWrite)) _sftpc.DownloadFile(i.CommonPath, f, (d) => { ReportTransferProgress(new TransferProgressArgs((long) d-_transfered, (long) d, i, _startedOn)); _transfered = (long)d; }); } catch (Exception ex) { Common.LogError(ex); if (FTP) CheckWorkingDirectory(); goto Finish; } if (i.Item.Size == new FileInfo(temp).Length) { controller.FolderWatcher.Pause(); // Pause Watchers if (File.Exists(i.LocalPath)) #if __MonoCs__ File.Delete(i.LocalPath); #else FileIO.FileSystem.DeleteFile(i.LocalPath, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); #endif File.Move(temp, i.LocalPath); controller.FolderWatcher.Resume(); // Resume Watchers return TransferStatus.Success; } Finish: if (File.Exists(temp)) #if __MonoCs__ File.Delete(temp); #else FileIO.FileSystem.DeleteFile(temp, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); #endif return TransferStatus.Failure; }
/// <summary> /// Set the Creation Time of an item /// </summary> /// <param name="i">The item</param> /// <param name="time">The new Creation Time</param> public abstract void SetCreationTime(SyncQueueItem i, DateTime time);
/// <summary> /// Download 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 download</param> /// <returns>TransferStatus.Success on success, TransferStatus.Success on failure</returns> public static TransferStatus SafeDownload(SyncQueueItem i) { Notifications.ChangeTrayText(MessageType.Downloading, i.Item.Name); string temp = Common._tempLocal(i.LocalPath); try { if (FTP) { if (i.PathToFile.Contains(" ")) { string cd = WorkingDirectory; _ftpc.ChangeDirectoryMultiPath(i.PathToFile); _ftpc.GetFile(Common._name(i.CommonPath), temp, FileAction.Create); while (WorkingDirectory != cd) _ftpc.ChangeDirectoryMultiPath(".."); } else _ftpc.GetFile(i.CommonPath, temp, FileAction.Create); } else using (var f = new FileStream(temp, FileMode.Create, FileAccess.ReadWrite)) _sftpc.DownloadFile(i.CommonPath, f); } catch (Exception ex) { Common.LogError(ex); goto Finish; } if (i.Item.Size == new FileInfo(temp).Length) { Common.FolderWatcher.Pause(); // Pause Watchers if (File.Exists(i.LocalPath)) #if __MonoCs__ File.Delete(i.LocalPath); #else FileIO.FileSystem.DeleteFile(i.LocalPath, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); #endif File.Move(temp, i.LocalPath); Common.FolderWatcher.Resume(); // Resume Watchers return TransferStatus.Success; } Finish: if (File.Exists(temp)) #if __MonoCs__ File.Delete(temp); #else FileIO.FileSystem.DeleteFile(temp, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); #endif return TransferStatus.Failure; }
/// <summary> /// Creates the SyncQueueItem from the given data and adds it to the sync queue /// </summary> private void AddToQueue(FileSystemEventArgs e, ChangeAction action) { var isFile = Common.PathIsFile(e.FullPath); // ignore directory changes if (!isFile && action == ChangeAction.changed) return; var queueItem = new SyncQueueItem(_controller) { Item = new ClientItem { Name = e.Name, FullPath = e.FullPath, Type = isFile ? ClientItemType.File : ClientItemType.Folder, Size = (isFile && action != ChangeAction.deleted) ? new FileInfo(e.FullPath).Length : 0x0, LastWriteTime = File.GetLastWriteTime(e.FullPath) }, SyncTo = SyncTo.Remote, ActionType = action }; if (action == ChangeAction.renamed) { var args = e as RenamedEventArgs; if (args != null) { queueItem.Item.FullPath = args.OldFullPath; queueItem.Item.NewFullPath = args.FullPath; } } // Send to the sync queue _controller.SyncQueue.Add(queueItem); }
public override void SetFilePermissions(SyncQueueItem i, short mode) { _sftpc.ChangePermissions(i.CommonPath, mode); }
/// <summary> /// Download 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 download</param> /// <returns>TransferStatus.Success on success, TransferStatus.Success on failure</returns> public TransferStatus SafeDownload(SyncQueueItem i) { Notifications.ChangeTrayText(MessageType.Downloading, i.Item.Name); string temp = Common._tempLocal(i.LocalPath); try { var _startedOn = DateTime.Now; long _transfered = 0; // download 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; if (!cd.Equals(i.PathToFile)) { string path = i.PathToFile.StartsWithButNotEqual(cd + "/") ? i.PathToFile.Substring(cd.Length + 1) : i.PathToFile; _ftpc.ChangeDirectoryMultiPath(path); } _ftpc.GetFile(Common._name(i.CommonPath), temp, FileAction.Create); while (WorkingDirectory != cd) { _ftpc.ChangeDirectoryMultiPath(".."); } } else { _ftpc.GetFile(i.CommonPath, temp, FileAction.Create); } // Unsubscribe _ftpc.TransferProgress -= action; } else { using (var f = new FileStream(temp, FileMode.Create, FileAccess.ReadWrite)) _sftpc.DownloadFile(i.CommonPath, f, (d) => { ReportTransferProgress(new TransferProgressArgs((long)d - _transfered, (long)d, i, _startedOn)); _transfered = (long)d; }); } } catch (Exception ex) { Common.LogError(ex); if (FTP) { CheckWorkingDirectory(); } goto Finish; } if (i.Item.Size == new FileInfo(temp).Length) { controller.FolderWatcher.Pause(); // Pause Watchers if (File.Exists(i.LocalPath)) #if __MonoCs__ { File.Delete(i.LocalPath); } #else { FileIO.FileSystem.DeleteFile(i.LocalPath, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); } #endif File.Move(temp, i.LocalPath); controller.FolderWatcher.Resume(); // Resume Watchers return(TransferStatus.Success); } Finish: if (File.Exists(temp)) #if __MonoCs__ { File.Delete(temp); } #else { FileIO.FileSystem.DeleteFile(temp, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); } #endif return(TransferStatus.Failure); }
/// <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); } }
public override void SetCreationTime(SyncQueueItem i, DateTime time) { Log.Write(l.Warning, $"[Not Supported] On SFTP, cannot set creation time of {i.CommonPath}"); }
/// <summary> /// Change the permissions of the specified file /// </summary> /// <param name="i">The item to change the permissions of</param> /// <param name="mode">The new permissions in numeric notation</param> public abstract void SetFilePermissions(SyncQueueItem i, short mode);
/// <summary> /// Set the Last Modified Time of an item /// </summary> /// <param name="i">The item</param> /// <param name="time">The new Last Modified Time</param> public abstract void SetModifiedTime(SyncQueueItem i, DateTime time);
/// <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 static TransferStatus SafeUpload(SyncQueueItem i) { Notifications.ChangeTrayText(MessageType.Uploading, i.Item.Name); string temp = Common._tempName(i.CommonPath); try { //upload to a temp file... if (FTP) { if (i.PathToFile.Contains(" ")) { 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); } else using (var file = File.OpenRead(i.LocalPath)) _sftpc.UploadFile(file, temp, true); } catch { 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; } }
public abstract Task Download(SyncQueueItem i, Stream fileStream, IProgress <TransferProgress> progress);
/// <summary> /// Download 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 download</param> /// <returns>TransferStatus.Success on success, TransferStatus.Success on failure</returns> public TransferStatus SafeDownload(SyncQueueItem i) { Notifications.ChangeTrayText(MessageType.Downloading, i.Item.Name); string temp = Common._tempLocal(i.LocalPath, controller.Account.TempFilePrefix); try { var _startedOn = DateTime.Now; long _transfered = 0; // download to a temp file... if (FTP) { using (Stream file = File.OpenWrite(temp), rem = _ftpc.OpenRead(i.CommonPath)) { var buf = new byte[8192]; int read; while ((read = rem.Read(buf, 0, buf.Length)) > 0) { file.Write(buf, 0, read); _transfered += read; ReportTransferProgress(new TransferProgressArgs(read, _transfered, i, _startedOn)); ThrottleTransfer(Settings.General.DownloadLimit, _transfered, _startedOn); } } } else { using (var f = new FileStream(temp, FileMode.Create, FileAccess.ReadWrite)) _sftpc.DownloadFile(i.CommonPath, f, (d) => { ReportTransferProgress(new TransferProgressArgs((long)d - _transfered, (long)d, i, _startedOn)); _transfered = (long)d; }); } } catch (Exception ex) { Common.LogError(ex); if (FTP) { CheckWorkingDirectory(); } goto Finish; } if (i.Item.Size == new FileInfo(temp).Length) { controller.FolderWatcher.Pause(); // Pause Watchers if (File.Exists(i.LocalPath)) #if __MonoCs__ { File.Delete(i.LocalPath); } #else { FileIO.FileSystem.DeleteFile(i.LocalPath, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); } #endif File.Move(temp, i.LocalPath); controller.FolderWatcher.Resume(); // Resume Watchers return(TransferStatus.Success); } Finish: if (File.Exists(temp)) #if __MonoCs__ { File.Delete(temp); } #else { FileIO.FileSystem.DeleteFile(temp, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); } #endif return(TransferStatus.Failure); }
/// <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) { // is this the first time we check the files? if (controller.FileLog.isEmpty()) { //TODO: allow user to select if the following should happen // skip synchronization if the file already exists and has the exact same size if (this.Exists(i.CommonPath) && SizeOf(i.CommonPath) == i.Item.Size) { Log.Write(l.Client, "File seems to be already synced (skipping): {0}", i.CommonPath); return TransferStatus.Success; } } 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) { using (Stream file = File.OpenRead(i.LocalPath), rem = _ftpc.OpenWrite(temp)) { var buf = new byte[8192]; int read; while ((read = file.Read(buf, 0, buf.Length)) > 0) { rem.Write(buf, 0, read); _transfered += read; ReportTransferProgress(new TransferProgressArgs(read, _transfered, i, _startedOn)); ThrottleTransfer(Settings.General.UploadLimit, _transfered, _startedOn); } } } 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; } }
/// <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; } }
/// <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) { // is this the first time we check the files? if (controller.FileLog.isEmpty()) { //TODO: allow user to select if the following should happen // skip synchronization if the file already exists and has the exact same size if (this.Exists(i.CommonPath) && SizeOf(i.CommonPath) == i.Item.Size) { Log.Write(l.Client, "File seems to be already synced (skipping): {0}", i.CommonPath); return(TransferStatus.Success); } } Notifications.ChangeTrayText(MessageType.Uploading, i.Item.Name); string temp = Common._tempName(i.CommonPath, controller.Account.TempFilePrefix); try { var _startedOn = DateTime.Now; long _transfered = 0; // upload to a temp file... if (FTP) { using (Stream file = File.Open(i.LocalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), rem = _ftpc.OpenWrite(temp)) { var buf = new byte[8192]; int read; while ((read = file.Read(buf, 0, buf.Length)) > 0) { rem.Write(buf, 0, read); _transfered += read; ReportTransferProgress(new TransferProgressArgs(read, _transfered, i, _startedOn)); ThrottleTransfer(Settings.General.UploadLimit, _transfered, _startedOn); } } } else { using (var file = File.Open(i.LocalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) _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); } }
/// <summary> /// TransferProgress constructor. /// </summary> /// <param name="total">total bytes transferred</param> /// <param name="item">the transferred item</param> /// <param name="started">when the transfer started</param> public TransferProgress(long total, SyncQueueItem item, DateTime started) { TotalTransferred = total; Total = item.Item.Size; StartedOn = started; }
/// <summary> /// Puts the specified file in the File Log and saves to the config file /// </summary> public void putFile(SyncQueueItem file) { Log.Write(l.Debug, "Putting file {0} to log", file.NewCommonPath); if (Contains(file.NewCommonPath)) Remove(file.NewCommonPath); Files.Add(new FileLogItem { CommonPath = file.NewCommonPath, Local = file.SyncTo == SyncTo.Remote ? file.Item.LastWriteTime : System.IO.File.GetLastWriteTime(file.LocalPath), Remote = Client.GetLwtOf(file.NewCommonPath) //file.SyncTo == SyncTo.Local ? file.Item.LastWriteTime : Client.GetLwtOf(file.NewCommonPath) }); FileLogChanged(null, EventArgs.Empty); Settings.SaveProfile(); }
/// <summary> /// Download 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 download</param> /// <returns>TransferStatus.Success on success, TransferStatus.Success on failure</returns> public TransferStatus SafeDownload(SyncQueueItem i) { Notifications.ChangeTrayText(MessageType.Downloading, i.Item.Name); string temp = Common._tempLocal(i.LocalPath); try { var _startedOn = DateTime.Now; long _transfered = 0; // download 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; if (!cd.Equals(i.PathToFile)) { string path = i.PathToFile.StartsWithButNotEqual(cd + "/") ? i.PathToFile.Substring(cd.Length + 1) : i.PathToFile; _ftpc.ChangeDirectoryMultiPath(path); } _ftpc.GetFile(Common._name(i.CommonPath), temp, FileAction.Create); while (WorkingDirectory != cd) _ftpc.ChangeDirectoryMultiPath(".."); } else _ftpc.GetFile(i.CommonPath, temp, FileAction.Create); // Unsubscribe _ftpc.TransferProgress -= action; } else using (var f = new FileStream(temp, FileMode.Create, FileAccess.ReadWrite)) _sftpc.DownloadFile(i.CommonPath, f, (d) => { ReportTransferProgress(new TransferProgressArgs((long) d-_transfered, (long) d, i, _startedOn)); _transfered = (long)d; }); } catch (Exception ex) { Common.LogError(ex); if (FTP) CheckWorkingDirectory(); goto Finish; } if (i.Item.Size == new FileInfo(temp).Length) { controller.FolderWatcher.Pause(); // Pause Watchers if (File.Exists(i.LocalPath)) #if __MonoCs__ File.Delete(i.LocalPath); #else FileIO.FileSystem.DeleteFile(i.LocalPath, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); #endif File.Move(temp, i.LocalPath); controller.FolderWatcher.Resume(); // Resume Watchers return TransferStatus.Success; } Finish: if (File.Exists(temp)) #if __MonoCs__ File.Delete(temp); #else FileIO.FileSystem.DeleteFile(temp, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin); #endif return TransferStatus.Failure; }