示例#1
0
 public bool GetFile(string sourcePath, string destinationPath, IFileTransferHandler handler, bool ifNewer)
 {
     if (ifNewer)
     {
         var modified = File.GetLastWriteTimeUtc(destinationPath);
         var remoteModified = DateTime.MinValue;
         try
         {
             remoteModified = Sftp.GetLastWriteTimeUtc(sourcePath);
         }
         catch (SftpPathNotFoundException)
         {
         }
         if (remoteModified <= modified)
         {
             if (handler != null)
             {
                 handler.FileUpToDate();
             }
             return false;
         }
     }
     using (var file = File.OpenWrite(destinationPath))
     {
         var result = Sftp.BeginDownloadFile(
             sourcePath,
             file,
             r => handler.TransferCompleted(),
             new object(),
             handler.IncrementBytesTransferred);
         Sftp.EndDownloadFile(result);
     }
     return true;
 }
示例#2
0
        public bool PutFile(IPurePath sourcePath, IPurePath destinationPath, IFileTransferHandler handler, bool ifNewer)
        {
            var filename = sourcePath.Filename;
            var source = Context.LocalEnv.CurrentDirectory.Join(sourcePath);
            var dest = Context.LocalEnv.CurrentDirectory.Join(destinationPath);

            if (handler != null)
            {
                handler.Filename = filename;
            }

            if (!File.Exists(source.ToString()))
            {
                if (handler != null)
                {
                    handler.FileDoesNotExist();
                }
                return false;
            }

            if (String.IsNullOrEmpty(dest.Filename) ||
                Directory.Exists(dest.ToString()))
            {
                dest = dest.WithFilename(filename);
            }

            var sourceStr = source.ToString();
            var destStr = dest.ToString();

            if (ifNewer && File.Exists(destStr) &&
                File.GetLastWriteTimeUtc(sourceStr) <= File.GetLastWriteTimeUtc(destStr))
            {
                if (handler != null)
                {
                    handler.FileUpToDate();
                }
                return false;
            }
            using (var file = File.OpenRead(sourceStr))
            {

                PutFile(file, destStr, handler);
            }
            return true;
        }
示例#3
0
        public bool PutFile(IPurePath sourcePath, IPurePath destinationPath, IFileTransferHandler handler, bool ifNewer)
        {
            var filename = sourcePath.Filename;
            var source = Context.LocalEnv.CurrentDirectory.Join(sourcePath);
            var dest = Context.RemoteEnv.CurrentDirectory.Join(destinationPath);

            if (handler != null)
            {
                handler.Filename = filename;
            }

            if (!File.Exists(source.ToString()))
            {
                if (handler != null)
                {
                    handler.FileDoesNotExist();
                }
                return false;
            }

            if (String.IsNullOrEmpty(dest.Filename) ||
                Sftp.GetAttributes(dest.ToString()).IsDirectory)
            {
                dest = dest.WithFilename(filename);
            }

            if (ifNewer)
            {
                var modified = File.GetLastWriteTimeUtc(source.ToString());
                var remoteModified = DateTime.MinValue;
                try
                {
                    remoteModified = Sftp.GetLastWriteTimeUtc(dest.ToString());
                }
                catch (SftpPathNotFoundException)
                {
                }
                if (modified <= remoteModified)
                {
                    if (handler != null)
                    {
                        handler.FileUpToDate();
                    }
                    return false;
                }
            }
            using (var file = File.OpenRead(source.ToString()))
            {
                PutFile(file, dest, handler);
            }
            return true;
        }