/// <summary>Upload a local file to the host. The remote file will be named 'remote_filename' on the host</summary> public void Upload(string local_filepath, string remote_filename, ResumeBehaviour resume = ResumeBehaviour.DontResume) { Trace(string.Format("Uploading: {0} to {1}/{2}/{3}", local_filepath, Settings.RemoteHost, Settings.RemotePath, remote_filename)); Reply reply; // Check the local filepath exists first if (!Path_.FileExists(local_filepath)) { throw new FileNotFoundException("Upload failed", local_filepath); } // Upload the file using (var input = new FileStream(local_filepath, FileMode.Open, FileAccess.Read)) { if (resume != ResumeBehaviour.DontResume) { // Some servers may not support resumed uploading. long offset = RemoteFileSize(remote_filename); if (SendCommand("REST " + offset).Code == Status.PendingFurtherInfo) { Trace("Resuming file upload from offset " + offset); input.Seek(offset, SeekOrigin.Begin); } else if (resume == ResumeBehaviour.Resume) { // The caller specifically wanted resuming but it's // not supported, throw so they can choose what to do. Trace("Resuming not supported. Aborting."); throw new NotSupportedException("Resumed upload not supported by server"); } else { Trace("Resuming not supported. Restarting."); input.Seek(0, SeekOrigin.Begin); } } // Store the file reply = SendCommand("STOR " + remote_filename); if (reply.Code != Status.DataConnectionAlreadyOpen && reply.Code != Status.DataConnectionOpenning) { throw new IOException(reply.Message); } // Write the file to the data channel using (BinaryWriter w = new BinaryWriter(ConnectDataStream())) input.CopyTo(w.BaseStream); } // Confirm done reply = ReadReply(); if (reply.Code != Status.DataConnectionClosing && reply.Code != Status.RequestedActionOkay) { throw new IOException(reply.Message); } }
/// <summary>Download a remote file to a local filepath. The local file will be created, appended, or overwritten, but the path must exist.</summary> public void Download(string remote_filename, string local_filepath, ResumeBehaviour resume = ResumeBehaviour.DontResume) { Trace(string.Format("Downloading: {0} from {1}/{2} to {3}", remote_filename, Settings.RemoteHost, Settings.RemotePath, local_filepath)); Reply reply; // If the local filepath is a directory, then append the remote filename as the local filename if (Directory.Exists(local_filepath)) { local_filepath = Path.Combine(local_filepath, remote_filename); } // Download the file using (FileStream output = new FileStream(local_filepath, FileMode.OpenOrCreate, FileAccess.Write)) { long offset = output.Length; if (resume != ResumeBehaviour.DontResume && offset != 0) { // Some servers may not support resuming. if (SendCommand("REST " + offset).Code == Status.PendingFurtherInfo) { Trace("Resuming download at offset " + offset); output.Seek(offset, SeekOrigin.Begin); } else if (resume == ResumeBehaviour.Resume) { // The caller specifically wanted resuming but it's // not supported, throw so they can choose what to do. Trace("Resuming not supported. Aborting."); throw new NotSupportedException("Resumed download not supported by server"); } else { Trace("Resuming not supported. Restarting."); output.Seek(0, SeekOrigin.Begin); } } // Retrieve the file reply = SendCommand("RETR " + remote_filename); if (reply.Code != Status.DataConnectionOpenning && reply.Code != Status.DataConnectionAlreadyOpen) { throw new IOException(reply.Message); } // Read the file data from the data channel using (BinaryReader r = new BinaryReader(ConnectDataStream())) r.BaseStream.CopyTo(output); } // Confirm done reply = ReadReply(); if (reply.Code != Status.DataConnectionClosing && reply.Code != Status.RequestedActionOkay) { throw new IOException(reply.Message); } }
/// <summary>Upload a local file to the host. The remote file will have the same name on the host as the local file.</summary> public void Upload(string local_filepath, ResumeBehaviour resume = ResumeBehaviour.DontResume) { Upload(local_filepath, Path.GetFileName(local_filepath), resume); }
/// <summary>Download a remote file to the local directory, keeping the same file name.</summary> public void Download(string remote_filename, ResumeBehaviour resume = ResumeBehaviour.DontResume) { Download(remote_filename, @".\" + remote_filename, resume); }