private byte[] GetFileSegmentFromFtp(FtpParameters ftpParameters, string ftpFilename, long offset, bool eof, int segmentSize) { FtpWebRequest ftpWebRequest = GetFtpRequest(ftpParameters, ftpFilename); ftpWebRequest.ContentOffset = offset; ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile; FtpWebResponse response = GetFtpResponse(ftpWebRequest); Stream responseStream = response.GetResponseStream(); if (responseStream == null) { throw new ApplicationException("response.GetResponseStream() returned no stream"); } BinaryReader reader = new BinaryReader(responseStream); byte[] returnValue = reader.ReadBytes(segmentSize); if (!eof) { ftpWebRequest.Abort(); } reader.Close(); return(returnValue); }
public FtpResponseStatus AppendFile(FtpParameters ftpParameters, string ftpFilename, byte[] fileToSend) { FtpResponseStatus returnValue = new FtpResponseStatus(); returnValue.StatusType = StatusType.Succeeded; return(returnValue); }
// A quik test for the spell checker public bool DownloadFile(string localFilename, string serverFilename, FtpParameters ftpParameters, out string responseAsString) { #region check arguments if (String.IsNullOrEmpty(localFilename)) { responseAsString = "The local file name is not specified"; return(false); } string directoryName = Path.GetDirectoryName(localFilename); if (String.IsNullOrEmpty(directoryName) || !Directory.Exists(directoryName)) { responseAsString = string.Format("The directory {0} is invalid", directoryName); return(false); } #endregion bool returnValue; const int segmentSize = 16000; FtpResponseStatus response = null; FileStream fileStream = new FileStream(localFilename, FileMode.Create, FileAccess.ReadWrite, FileShare.None); BinaryWriter binaryWriter = new BinaryWriter(fileStream); try { byte[] segment; bool eof = false; int offset = 0; while (!eof) { IFTPAgent gatewayServiceClient = FTPAgentFactory.CreateAgent(_agentType); response = gatewayServiceClient.RetrieveSegment(ftpParameters, serverFilename, offset, segmentSize, out segment, out eof); if (response.StatusType != StatusType.Succeeded) { break; } binaryWriter.Write(segment); offset += segment.Length; } returnValue = eof; } catch (Exception e) // If the request can't be sent correctly, the procy client can generate various exceptions // for exeample if the chunk size is too big , wcf config issue, .... { response = new FtpResponseStatus(); response.StatusType = StatusType.OtherError; response.ErrorDescription = e.ToString(); returnValue = false; } finally { binaryWriter.Close(); } responseAsString = ResponseFormatter.Format(response); return(returnValue); }
public FtpResponseStatus DeleteIfExists(FtpParameters ftpParameters, string fileName, out bool existing) { FtpResponseStatus returnValue = new FtpResponseStatus(); returnValue.StatusType = StatusType.Succeeded; existing = true; return(returnValue); }
public FtpResponseStatus Append(FtpParameters ftpParameters, string ftpFilename, byte[] fileToSend) { #region Parameter validation if (ftpParameters == null) { throw new ArgumentNullException("ftpParameters"); } if (String.IsNullOrEmpty(ftpFilename)) { throw new ArgumentNullException("ftpFilename"); } if (fileToSend == null) { throw new ArgumentNullException("fileToSend"); } #endregion #region log4net _logger.DebugFormat("ftpParameters : {0}, ftpFilename : {1}, fileToSend : {2} ", ftpParameters, ftpFilename, fileToSend.Length); #endregion FtpResponseStatus returnValue = new FtpResponseStatus(); try { FtpWebRequest ftpWebRequest = GetFtpRequest(ftpParameters, ftpFilename); ftpWebRequest.Method = WebRequestMethods.Ftp.AppendFile; Stream stream = ftpWebRequest.GetRequestStream(); stream.Write(fileToSend, 0, fileToSend.Length); stream.Close(); FtpWebResponse ftpWebResponse = GetFtpResponse(ftpWebRequest); if (ftpWebResponse.StatusCode == FtpStatusCode.ClosingData) { returnValue.StatusType = StatusType.Succeeded; } else { returnValue.StatusType = StatusType.OtherError; returnValue.ErrorDescription = FtpGatewayServiceErrorMessage; #region log4net _logger.ErrorFormat("ftpWebResponse.StatusCode = {0}, .StatusDescription = {1}", ftpWebResponse.StatusCode, ftpWebResponse.StatusDescription); #endregion } ftpWebResponse.Close(); } catch (WebException webException) { returnValue = GetResponseStatusFromWebException(webException); } return(returnValue); }
public FtpResponseStatus NameListOfRemoteDirectory(FtpParameters ftpParameters, String filePath, out string[] fileList) { FtpResponseStatus returnValue = new FtpResponseStatus(); returnValue.StatusType = StatusType.Succeeded; fileList = new string[0]; return(returnValue); }
private FtpParameters GetFtpParameters() { FtpParameters ftpParameters = new FtpParameters(); ftpParameters.Address = _tbxAddress.Text; ftpParameters.User = _tbxUser.Text; ftpParameters.Password = _tbxPassword.Text; ftpParameters.Port = 21; return(ftpParameters); }
private static long GetFileLengthFromFtp(FtpParameters ftpParameters, string ftpFilename) { FtpWebRequest ftpWebRequest = GetFtpRequest(ftpParameters, ftpFilename); ftpWebRequest.Method = WebRequestMethods.Ftp.GetFileSize; FtpWebResponse response = GetFtpResponse(ftpWebRequest); long fileLength = response.ContentLength; response.Close(); return(fileLength); }
public FtpResponseStatus RetrieveSegment(FtpParameters ftpParameters, string ftpFilename, long offset, int segmentSize, out byte[] data, out bool eof) { FtpResponseStatus returnValue = new FtpResponseStatus(); returnValue.StatusType = StatusType.Succeeded; data = new byte[segmentSize]; data.Initialize(); eof = true; return(returnValue); }
public bool UploadFile(string localFilename, string serverFilename, FtpParameters ftpParameters, out string responseAsString) { if (!File.Exists(localFilename)) { responseAsString = string.Format("The file {0} does not exists", localFilename); return(false); } const int chunkSize = 16000; FileInfo fileToSendInfo = new FileInfo(localFilename); long fileSize = fileToSendInfo.Length; FtpResponseStatus lastResponse = null; FileStream fileStream = new FileStream(fileToSendInfo.FullName, FileMode.Open); bool returnValue; try { for (long currentIndex = 0; currentIndex < fileSize; currentIndex += chunkSize) { lastResponse = SendFileChunk(fileStream, chunkSize, ftpParameters, serverFilename); if (lastResponse.StatusType != StatusType.Succeeded) { break; } } responseAsString = ResponseFormatter.Format(lastResponse); } finally { fileStream.Close(); } if (lastResponse == null) { returnValue = false; } else { returnValue = lastResponse.StatusType == StatusType.Succeeded; } return(returnValue); }
/// <summary> /// See doc in the service interface. Basically, this is a 'wrapper' arrount the ftp nlst command /// </summary> /// <param name="ftpParameters"></param> /// <param name="fileList"></param> /// <returns></returns> public FtpResponseStatus NameListOfRemoteDirectory(FtpParameters ftpParameters, String filePath, out String[] fileList) { #region log4net _logger.DebugFormat("Parameters : ftpParameters={0}, filename={1}", ftpParameters, filePath); #endregion FtpResponseStatus returnValue = new FtpResponseStatus(); List <String> localFileList = new List <string>(); fileList = null; try { FtpWebRequest ftpWebRequest = GetFtpRequest(ftpParameters, filePath); ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory; FtpWebResponse response = GetFtpResponse(ftpWebRequest); StreamReader reader = new StreamReader(response.GetResponseStream()); string line = reader.ReadLine(); while (!String.IsNullOrEmpty(line)) { localFileList.Add(line); line = reader.ReadLine(); } reader.Close(); response.Close(); fileList = localFileList.ToArray(); returnValue.StatusType = StatusType.Succeeded; return(returnValue); } catch (WebException webException) { returnValue = GetResponseStatusFromWebException(webException); } return(returnValue); }
public FtpResponseStatus RetrieveSegment(FtpParameters ftpParameters, string ftpFilename, long offset, int segmentSize, out byte[] segment, out bool eof) { #region log4net _logger.DebugFormat("Parameters : ftpParameters={0}, segmentSize={1}", ftpParameters, segmentSize); #endregion FtpResponseStatus returnValue = new FtpResponseStatus(); try { long fileLength = GetFileLengthFromFtp(ftpParameters, ftpFilename); if (offset > fileLength) { returnValue.StatusType = StatusType.Succeeded; eof = true; segment = null; } else { long remainingLength = fileLength - offset; eof = remainingLength <= segmentSize; // Get the size of the chunk. // If this is the last chunk, the size maybe smaller than the requested chunk size. int realSegmentSize = eof ? Convert.ToInt32(remainingLength) : segmentSize; segment = GetFileSegmentFromFtp(ftpParameters, ftpFilename, offset, eof, realSegmentSize); } returnValue.StatusType = StatusType.Succeeded; } catch (WebException webException) { returnValue = GetResponseStatusFromWebException(webException); segment = null; eof = false; } return(returnValue); }
private FtpResponseStatus SendFileChunk(Stream fileStream, int chunkSize, FtpParameters ftpParameters, string serverFilename) { FtpResponseStatus returnValue; try { IFTPAgent ftpAgent = FTPAgentFactory.CreateAgent(_agentType); byte[] tempBuffer = new byte[chunkSize]; byte[] bytesToSend; int numberOfBytesTosend = fileStream.Read(tempBuffer, 0, chunkSize); if (numberOfBytesTosend < chunkSize) { bytesToSend = new byte[numberOfBytesTosend]; Array.Copy(tempBuffer, bytesToSend, numberOfBytesTosend); } else { bytesToSend = tempBuffer; } returnValue = ftpAgent.AppendFile(ftpParameters, serverFilename, bytesToSend); return(returnValue); } catch (Exception e) // If the request can't be sent correctly, the procy client can generate various exceptions // for exeample if the chunk size is too big , wcf config issue, .... { returnValue = new FtpResponseStatus(); returnValue.StatusType = StatusType.OtherError; returnValue.ErrorDescription = e.ToString(); } return(returnValue); }
/// <summary> /// Returns an FtpWebRequest initialized with the parameters specified int the FtpParmaters data contract /// </summary> /// <param name="ftpParameters"></param> /// <param name="serverFilename"></param> /// <returns></returns> private static FtpWebRequest GetFtpRequest(FtpParameters ftpParameters, string serverFilename) { UriBuilder uriBuilder = new UriBuilder(ftpParameters.Address); uriBuilder.Port = ftpParameters.Port; if (String.IsNullOrWhiteSpace(serverFilename)) { uriBuilder.Path = VirtualPathUtility.RemoveTrailingSlah(uriBuilder.Path); } else { uriBuilder.Path = VirtualPathUtility.AppendTrailingSlash(uriBuilder.Path) + VirtualPathUtility.RemoveFirstSlash(serverFilename); } FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(uriBuilder.Uri); ftpWebRequest.Credentials = new NetworkCredential(ftpParameters.User, ftpParameters.Password); ftpWebRequest.UsePassive = !ftpParameters.IsActive; ftpWebRequest.KeepAlive = true; ftpWebRequest.UseBinary = true; return(ftpWebRequest); }
public FtpResponseStatus RetrieveSegment(FtpParameters ftpParameters, string ftpFilename, long offset, int segmentSize, out byte[] data, out bool eof) { return(Client.RetrieveSegment(ftpParameters, ftpFilename, offset, segmentSize, out data, out eof)); }
public FtpResponseStatus DeleteIfExists(FtpParameters ftpParameters, String fileName, out bool existing) { return(Client.DeleteIfExists(ftpParameters, fileName, out existing)); }
public FtpResponseStatus AppendFile(FtpParameters ftpParameters, string ftpFilename, byte[] fileToSend) { return(Client.AppendFile(ftpParameters, ftpFilename, fileToSend)); }
public FtpResponseStatus DeleteIfExists(FtpParameters ftpParameters, string ftpFilename, out bool existing) { #region log4net _logger.DebugFormat("Parameters : ftpParameters={0}, ftpFilename={1}", ftpParameters, ftpFilename); #endregion FtpResponseStatus returnValue; string[] existingFiles; FtpResponseStatus intermediateResponse = NameListOfRemoteDirectory(ftpParameters, ftpFilename, out existingFiles); if (intermediateResponse.StatusType == StatusType.Succeeded) { if (Array.Exists(existingFiles, s => s == Path.GetFileName(ftpFilename))) { existing = true; FtpWebRequest ftpWebRequest = GetFtpRequest(ftpParameters, ftpFilename); ftpWebRequest.Method = WebRequestMethods.Ftp.DeleteFile; FtpWebResponse webResponse = GetFtpResponse(ftpWebRequest); if (webResponse.StatusCode == FtpStatusCode.FileActionOK) { returnValue = new FtpResponseStatus { StatusType = StatusType.Succeeded } } ; else { returnValue = new FtpResponseStatus { StatusType = StatusType.FtpError, FtpErrorCode = ((int)webResponse.StatusCode), ErrorDescription = webResponse.StatusDescription } }; } else { existing = false; returnValue = new FtpResponseStatus { StatusType = StatusType.Succeeded }; } } else { existing = false; if (intermediateResponse.FtpErrorCode == ((int)FtpStatusCode.ActionNotTakenFileUnavailable)) { returnValue = new FtpResponseStatus { StatusType = StatusType.Succeeded } } ; else { returnValue = intermediateResponse; } } #region log4net _logger.DebugFormat("returnValue = {0}, existing = {1}", returnValue, existing); #endregion return(returnValue); }
public FtpResponseStatus NameListOfRemoteDirectory(FtpParameters ftpParameters, String filePath, out String[] fileList) { return(Client.NameListOfRemoteDirectory(ftpParameters, filePath, out fileList)); }