public async Task PutAsync(string remotename, Stream input, CancellationToken cancelToken) { string remotePath = remotename; long streamLen; try { var ftpClient = CreateClient(); try { streamLen = input.Length; } catch (NotSupportedException) { streamLen = -1; } // Get the remote path remotePath = ""; if (!string.IsNullOrEmpty(remotename)) { // Append the filename remotePath += remotename; } var success = await ftpClient.UploadAsync(input, remotePath, FtpExists.Overwrite, createRemoteDir : false, token : cancelToken, progress : null).ConfigureAwait(false); if (!success) { throw new UserInformationException(string.Format(Strings.ErrorWriteFile, remotename), "AftpPutFailure"); } // check remote file size; matching file size indicates completion var sleepTime = 250; var maxVerifyMilliseconds = 5000; var m = maxVerifyMilliseconds; long remoteSize = 0; while (m > 0) { remoteSize = ftpClient.GetFileSize(remotePath); if (streamLen == remoteSize) { return; } m -= sleepTime; Thread.Sleep(sleepTime); } throw new UserInformationException(Strings.ListVerifySizeFailure(remotename, remoteSize, streamLen), "AftpListVerifySizeFailure"); } catch (FtpCommandException ex) { if (ex.Message == "Directory not found.") { throw new FolderMissingException(Strings.MissingFolderError(remotePath, ex.Message), ex); } throw; } }
public void Put(string remotename, System.IO.Stream input) { string remotePath = remotename; long streamLen = -1; try { var ftpClient = CreateClient(); try { streamLen = input.Length; } // ReSharper disable once EmptyGeneralCatchClause catch { } // Get the remote path remotePath = ""; if (!string.IsNullOrEmpty(remotename)) { // Append the filename remotePath += remotename; } using (var outputStream = ftpClient.OpenWrite(remotePath)) { try { CoreUtility.CopyStream(input, outputStream, true, _copybuffer); } finally { outputStream.Close(); } } if (_listVerify) { var fileEntries = List(remotename, true); foreach (var fileEntry in fileEntries) { if (fileEntry.Name.Equals(remotename) || fileEntry.Name.EndsWith("/" + remotename, StringComparison.Ordinal) || fileEntry.Name.EndsWith("\\" + remotename, StringComparison.Ordinal)) { if (fileEntry.Size < 0 || streamLen < 0 || fileEntry.Size == streamLen) { return; } throw new UserInformationException(Strings.ListVerifySizeFailure(remotename, fileEntry.Size, streamLen), "AftpListVerifySizeFailure"); } } throw new UserInformationException(Strings.ListVerifyFailure(remotename, fileEntries.Select(n => n.Name)), "AftpListVerifySizeFailure"); } } catch (FtpCommandException ex) { if (ex.Message == "Directory not found.") { throw new FolderMissingException(Strings.MissingFolderError(remotePath, ex.Message), ex); } throw; } }
private IEnumerable <IFileEntry> List(string filename, bool stripFile) { var list = new List <IFileEntry>(); string remotePath = filename; try { var ftpClient = CreateClient(); // Get the remote path var url = new Uri(this._url); remotePath = "/" + (url.AbsolutePath.EndsWith("/", StringComparison.Ordinal) ? url.AbsolutePath.Substring(0, url.AbsolutePath.Length - 1) : url.AbsolutePath); if (!string.IsNullOrEmpty(filename)) { if (!stripFile) { // Append the filename remotePath += filename; } else if (filename.Contains("/")) { remotePath += filename.Substring(0, filename.LastIndexOf("/", StringComparison.Ordinal)); } // else: stripping the filename in this case ignoring it } foreach (FtpListItem item in ftpClient.GetListing(remotePath, FtpListOption.Modify | FtpListOption.Size | FtpListOption.DerefLinks)) { switch (item.Type) { case FtpFileSystemObjectType.Directory: { if (item.Name == "." || item.Name == "..") { continue; } list.Add(new FileEntry(item.Name, -1, new DateTime(), item.Modified) { IsFolder = true, }); break; } case FtpFileSystemObjectType.File: { list.Add(new FileEntry(item.Name, item.Size, new DateTime(), item.Modified)); break; } case FtpFileSystemObjectType.Link: { if (item.Name == "." || item.Name == "..") { continue; } if (item.LinkObject != null) { switch (item.LinkObject.Type) { case FtpFileSystemObjectType.Directory: { if (item.Name == "." || item.Name == "..") { continue; } list.Add(new FileEntry(item.Name, -1, new DateTime(), item.Modified) { IsFolder = true, }); break; } case FtpFileSystemObjectType.File: { list.Add(new FileEntry(item.Name, item.Size, new DateTime(), item.Modified)); break; } } } break; } } } }// Message "Directory not found." string catch (FtpCommandException ex) { if (ex.Message == "Directory not found.") { throw new FolderMissingException(Strings.MissingFolderError(remotePath, ex.Message), ex); } throw; } return(list); }
public async Task PutAsync(string remotename, Stream input, CancellationToken cancelToken) { string remotePath = remotename; long streamLen; try { var ftpClient = CreateClient(); try { streamLen = input.Length; } catch (NotSupportedException) { streamLen = -1; } // Get the remote path remotePath = ""; if (!string.IsNullOrEmpty(remotename)) { // Append the filename remotePath += remotename; } var success = await ftpClient.UploadAsync(input, remotePath, FtpExists.Overwrite, createRemoteDir : false, token : cancelToken, progress : null).ConfigureAwait(false); if (!success) { throw new UserInformationException(string.Format(Strings.ErrorWriteFile, remotename), "AftpPutFailure"); } if (_listVerify) { var fileEntries = List(remotename, true); foreach (var fileEntry in fileEntries) { if (fileEntry.Name.Equals(remotename) || fileEntry.Name.EndsWith("/" + remotename, StringComparison.Ordinal) || fileEntry.Name.EndsWith("\\" + remotename, StringComparison.Ordinal)) { if (fileEntry.Size < 0 || streamLen < 0 || fileEntry.Size == streamLen) { return; } throw new UserInformationException(Strings.ListVerifySizeFailure(remotename, fileEntry.Size, streamLen), "AftpListVerifySizeFailure"); } } throw new UserInformationException(Strings.ListVerifyFailure(remotename, fileEntries.Select(n => n.Name)), "AftpListVerifySizeFailure"); } } catch (FtpCommandException ex) { if (ex.Message == "Directory not found.") { throw new FolderMissingException(Strings.MissingFolderError(remotePath, ex.Message), ex); } throw; } }